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

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

function do_runscript() {
    #
    # $1: Phase to run
    local thisscript="$1" thisphase="$2"
    local line runphase rundir script user stdout stderr savehost
    local xdo="" xo="root" xu="" xdir="$assetdir"
    IFS="|" read script runphase rundir user stdout stderr < $assetdir/$thisscript-info
    if [[ "$runphase" == "$thisphase" ]]
    then
	savehost=$(hostname)
	if [ "$rundir" != "" ]
	then
	    logtoboth "> Plugin $pfx: Create directory $rundir"
	    mkdir -p $rundir
	    setfileownmode $rundir 0755 $xo
	    xdir="$rundir"
	else
	    rundir=$assetdir
	fi
	[ "$stdout" == "" ] && stdout="$xdir/$script.out"
	[ "$stderr" == "" ] && stderr="$xdir/$script.error"
	if [ "$user" != "" ]
	then
	    [ "$(getent passwd $user)" == "" ] && logtobothex "? Plugin $pfx: User '$user' not found"
	    xu="as user $user"
	    xdo="sudo -u $user"
	    xo="$user"
	    hostname $thishost
	fi
	cp $assetdir/$script /tmp
	[ "$user" != "" ] && setfileownmode /tmp/$script 0755 $xo
	logtoboth "> Plugin $pfx: Run script $script $xu"
	echo "[ Run script $script at $(date +"%Y-%m-%d") ]" >>$stdout
	echo "" >> $stdout
	echo "[ Run script $script at $(date +"%Y-%m-%d") ]" >>$stderr
	echo "" >> $stderr
	pushd $rundir >/dev/null
	${xdo} /tmp/$script $thisphase >>$stdout 2>>$stderr || logtobothex "? Plugin $pfx: Script $script exited with error"
	popd >/dev/null
	hostname $savehost
    fi
}

# $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
allargs="$2"
loadparams
vldargs="|dir|runphase|script|user|stdout|stderr|"
rqdargs="|script|"
assetdir="$SDMPT/etc/sdm/assets/runscript"

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 "$allargs" "$vldargs" "$rqdargs" || exit
    plugin_printkeys
    [ "$runphase" == "" ] && runphase="1"
    [[ "1|post-install" =~ "$runphase" ]] || logtobothex "? Plugin $pfx: Phase '$runphase' not recognized"
    mkdir -p $assetdir
    if [ -f $script ]
    then
	sbn=$(basename $script)
	cp -a $script $assetdir/${sbn}
	chmod 755 $assetdir/$sbn
	logtoboth "> Plugin $pfx: Script '$sbn' will be run during Phase '$runphase'"
	echo "${sbn}|$runphase|$dir|$user|$stdout|$stderr|" >> $assetdir/$sbn-info
    else
	logtobothex "? Plugin $pfx: Script file '$script' not found"
    fi
    logtoboth "* Plugin $pfx: Complete Phase 0"
elif [ "$phase" == "1" ]
then
    #
    # Phase 1 (in nspawn)
    #
    logtoboth "* Plugin $pfx: Start Phase 1"
    plugin_getargs $pfx "$allargs" "$vldargs" "$rqdargs"
    plugin_printkeys
    do_runscript $(basename $script) "$phase"
    logtoboth "* Plugin $pfx: Complete Phase 1"
else
    #
    # Plugin Post-install edits
    #
    logtoboth "* Plugin $pfx: Start Phase post-install"
    plugin_getargs $pfx "$allargs" "$vldargs" "$rqdargs"
    plugin_printkeys
    do_runscript $(basename $script) "$phase"
    logtoboth "* Plugin $pfx: Complete Phase post-install"
fi
