#!/bin/bash
#
# This is an sdm plugin for: raspiconfig
#
# 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="|audio|audioconf|blanking|boot_behavior|boot_behaviour|boot_order|boot_splash|boot_wait|camera|composite|glamor|gldriver|i2c|memory_split|legacy|net_names|onewire|overclock|overscan|pixdub|pi4video|powerled|leds|overlayfs|rgpio|serial|spi|xcompmgr|vnc_resolution|"
# These take 2 args, need more research: overscan_kms, fan, boot_rom, resolution, proxy
rqdargs=""
immedargs="|serial|"
#
# The settings are all stored in auto-1piboot and run when the system first boots
# Some (many?) of the settings require that the system be up and running on the target hardware
# Rather than sort out which is which, everything gets deferred except for 'serial', which may be interesting even during first boot
# ** Need to update 1piboot/1piboot.conf whenever this changes
#

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
    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"
    #logfreespace "at start of Plugin $pfx Phase 1"
    #
    if [ "$foundkeys" != "" ]
    then
	logtoboth "> Plugin $pfx: Processing settings..."
	IFS="|" read -a fargs <<< "$foundkeys"
	for c in "${fargs[@]}"
	do
	    # The construct ${!c} gets the value of the variable 'pointed to' by contents of $c
	    value="${!c}"
	    chyp=${c//__/-}    # Convert double underscore back to hyphen
	    [ "$value" == "" ] && value="0"
            # Remove any old entry for this key and write the new one
            if ! grep "$chyp=$value" $SDMPT/etc/sdm/auto-1piboot.conf > /dev/null 2>&1
            then
		if [[ "$immedargs" =~ "$chyp" ]]
		then
		    logtoboth "> Plugin $pfx: Set ${chyp} ${value} immediately"
		    doconfigitem ${chyp} ${value} logtoboth
		else
		    case "$chyp" in
			overlayfs)
			    logtoboth "> Plugin $pfx: Install overlayroot for overlayfs"
			    installpkgsif overlayroot
			    [ "$value" == "0" ] && value="ro"
			    ;;
		    esac
                    logtoboth "> Plugin $pfx: Set ${chyp} '${value}' to be enabled during sdm FirstBoot"
		    if [[ "$chyp" =~ "boot_behav" ]]
		    then
			setdelayedbbh "$value"
		    else
			sed -i "/^$chyp=/d" $SDMPT/etc/sdm/auto-1piboot.conf
			echo "$chyp=$value" >> $SDMPT/etc/sdm/auto-1piboot.conf
		    fi
		fi
            fi
        done
    fi
    #
    #logfreespace "at end of $pfx 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"
    #
    # INSERT Your Plugin's post-install code here
    # In Phase post-install all references to directories in the image can be direct
    #
    #logfreespace "at end of $pfx Custom Phase post-install"
    logtoboth "* Plugin $pfx: Complete Phase post-install"
fi
