#!/bin/bash
#
# This is an sdm plugin for: apps
#
# The plugin is called three times: for Phase 0, Phase 1, and post-install.
#

function loadparams() {
    source $SDMPT/etc/sdm/sdm-readparams
}

# $1 is the phase: "0", "1", or "post-install"
# $2 is the argument list: arg1=val1|arg2=val2|arg3=val3| ...
#
# Main code for the Plugin
#
phase=$1
pfx="$(basename $0)"     #For messages
args="$2"
loadparams
vldargs="|apps|name|remove|"
rqdargs=""
assetdir="$SDMPT/etc/sdm/assets/apps"
sdatefmt="%Y-%m-%d-%H:%M:%S.%N"
if [ "$phase" == "0" ]
then
    #
    # In Phase 0 all references to directories in the image must be preceded by $SDMPT
    #
    logtoboth "* Plugin $pfx: Start Phase 0"
    plugin_getargs $pfx "$args" "$vldargs" "$rqdargs" || exit
    #
    # Print the keys found (example usage). plugin_getargs returns the list of found keys in $foundkeys
    #
    plugin_printkeys

    if [[ "$apps" == "" ]] && [[ "$remove" == "" ]]
    then
	logtobothex "? Plugin $pfx: Neither 'apps' nor 'remove' specified; Nothing to do"
    fi
    [ "$name" == "" ] && name="default"
    mkdir -p $assetdir
    if [ "$apps" != "" ]
    then
        appfile=$(findappfile "$apps")
        exitiferr "$appfile"
	apps=$(getapplist "$appfile")
	if [ -f $assetdir/$name ]
	then
	    read oapps < $assetdir/$name
	    apps="$oapps $apps"
	    fd=$(date -d "$(stat --printf "%y" $assetdir/$name)" +$sdatefmt)
	    logtoboth "> Plugin $pfx: Save prior 'apps' list '$name' as '$name-$fd'"
	    mv $assetdir/$name $assetdir/$name-$fd
	fi
	echo "$apps" >| $assetdir/$name
        logtoboth "> Plugin $pfx [$name]: Saved 'apps' list: $apps"
    fi
    if [ "$remove" != "" ]
    then
	removefile=$(findappfile "$remove")
	exitiferr "$removefile"
	removes=$(getapplist "$removefile")
	if [ -f $assetdir/$name-removes ]
	then
	    read oapps < $assetdir/$name-removes
	    removes="$oapps $removes"
	    fd=$(date -d "$(stat --printf "%y" $assetdir/$name-removes)" +$sdatefmt)
	    logtoboth "> Plugin $pfx: Save prior 'removes' list '$name-removes' as '$name-removes-$fd'"
	    mv $assetdir/$name-removes $assetdir/$name-removes-$fd
	fi
	echo "$removes" >| $assetdir/$name-removes
        logtoboth "> Plugin $pfx [$name]: Saved 'remove' list: $removes"
    fi

    logtoboth "* Plugin $pfx: Complete Phase 0"

elif [ "$phase" == "1" ]
then
    #
    # Phase 1 (in nspawn)
    #
    logtoboth "* Plugin $pfx: Start Phase 1"
    plugin_getargs $pfx "$args" "$vldargs" "$rqdargs"
    [ "$name" == "" ] && name="default"
    if [ -f $assetdir/$name-removes ]
    then
	logfreespace "at start of [$name] Application Removes"
	read removes < $assetdir/$name-removes
	logtoboth "> Plugin $pfx: Remove application(s)"
        IFS=" " read -a alist <<< "$removes"
        for a in "${alist[@]}"
        do
            if ispkginstalled $a
	    then
		logtoboth "  -- $a"
		doapt "remove --yes $a" $showapt
	    else
		logtoboth "  -- $a (not installed)"
	    fi
        done
	fd=$(date -d "$(stat --printf "%y" $assetdir/$name-removes)" +$sdatefmt)
	logtoboth "> Plugin $pfx: Save completed 'removes' list '$name-removes' as '$name-removes-$fd'"
	mv $assetdir/$name-removes $assetdir/$name-removes-$fd
    fi
    if [ -f $assetdir/$name ]
    then
	logfreespace "at start of [$name] Application Installs"
	read svapps < $assetdir/$name
	if [[ "$virtmode" == "chroot" ]] && [[ "$svapps" =~ "qemu-user-static" ]]
	then
            svapps=${svapps/qemu-user-static}
            deferqemu
	fi
	doinstalls "$svapps" "[$name] Application Installs" || exit $?
	fd=$(date -d "$(stat --printf "%y" $assetdir/$name)" +$sdatefmt)
	logtoboth "> Plugin $pfx: Save completed 'apps' list '$name' as '$name-$fd'"
	mv $assetdir/$name $assetdir/$name-$fd
    fi

    logfreespace "at end of $pfx [$name] Phase 1"
    logtoboth "* Plugin $pfx: Complete Phase 1"
else
    #
    # Plugin Post-install edits
    #
    logtoboth "* Plugin $pfx: Start Phase post-install"
    #plugin_getargs $pfx "$args" "$vldargs" "$rqdargs"
    #logfreespace "at start of Plugin $pfx Phase post-install"
    #
    #logfreespace "at end of $pfx Custom Phase post-install"
    logtoboth "* Plugin $pfx: Complete Phase post-install"
fi
