#!/bin/bash
#
# This is an sdm plugin for: quietness
#
# 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="|consoleblank|quiet|noquiet|splash|nosplash|plymouth|noplymouth|"
rqdargs=""
[ -d $SDMPT/boot/firmware ] && cfgdir="$SDMPT/boot/firmware" || cfgdir="$SDMPT/boot"

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
    #plugin_dbgprint "This is how to do a Plugin Debug printout"      # Will only be printed if --plugin-debug specified
    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"
    #
    cat > /etc/sdm/0piboot/090-reset-quietness.sh <<EOF
#!/bin/bash
EOF
    if [ -v consoleblank ]
    then
	[ "$consoleblank" == "" ] && consoleblank=300
	logtoboth "> Plugin $pfx: Set console blank timeout to $consoleblank seconds"
	[ -f $cfgdir/cmdline.txt ] && sed -i "s/root=/consoleblank=$consoleblank root=/" $cfgdir/cmdline.txt
    fi
    if [ -v quiet ]
    then
	logtoboth "> Plugin $pfx: Enable quiet in $cfgdir/cmdline.txt"
	if [ -f $cfgdir/cmdline.txt ]
	then
	    sed -i "s/ quiet//g" $cfgdir/cmdline.txt
	    sed -i "s/rootwait/rootwait quiet/g" $cfgdir/cmdline.txt
	fi
    fi
    
    if [ -v noquiet ]
    then
	if grep -qs quiet $cfgdir/cmdline.txt
	then
	    logtoboth "> Plugin $pfx: Disable quiet in $cfgdir/cmdline.txt"
	    [ -f $cfgdir/cmdline.txt ] && sed -i "s/ quiet//g" $cfgdir/cmdline.txt
	    if [ "$noquiet" != "keep" ]
	    then
		cat >> /etc/sdm/0piboot/090-reset-quietness.sh <<EOF
logger "sdm FirstBoot: Re-add 'quiet' to $cfgdir/cmdline.txt"
sed -i "s/ quiet//g" $cfgdir/cmdline.txt
sed -i "s/rootwait/rootwait quiet/g" $cfgdir/cmdline.txt
EOF
	    fi
	fi
    fi
    
    if [ -v splash ]
    then
	logtoboth "> Plugin $pfx: Enable splash in $cfgdir/cmdline.txt"
	if [ -f $cfgdir/cmdline.txt ]
	then
	    sed -i "s/splash //g" $cfgdir/cmdline.txt
	    sed -i "s/rootwait/rootwait splash/g" $cfgdir/cmdline.txt
	fi
    fi
    
    if [ -v nosplash ]
    then
	if grep -qs splash $cfgdir/cmdline.txt
	then
	    logtoboth "> Plugin $pfx: Disable splash in $cfgdir/cmdline.txt"
	    sed -i "s/splash //g" $cfgdir/cmdline.txt
	    if [ "$nosplash" != "keep" ]
	    then
		cat >> /etc/sdm/0piboot/090-reset-quietness.sh <<EOF
logger "sdm FirstBoot: Re-add 'splash' to $cfgdir/cmdline.txt"
sed -i "s/ splash//g" $cfgdir/cmdline.txt
sed -i "s/rootwait/rootwait splash/g" $cfgdir/cmdline.txt
EOF
	    fi
	fi
    fi
    
    if [ -v plymouth ]
    then
	logtoboth "> Plugin $pfx: Enable Plymouth NYI"
    fi
    
    if [ -v noplymouth ]
    then
	if [ -f /etc/systemd/system/plymouth-start.service ]
	then
	    logtoboth "> Plugin $pfx: Disable Plymouth for first system boot"
	    #[ -f /etc/systemd/system/plymouth-start.service ] && mv /etc/systemd/system/plymouth-start.service /etc/systemd/system/.sdm.plymouth-start.service 
	    for svc in plymouth-start plymouth-read-write plymouth-quit plymouth-quit-wait plymouth-reboot
	    do
		systemctl mask $svc >/dev/null 2>&1
	    done
	    cat >> /etc/sdm/0piboot/090-reset-quietness.sh <<EOF
logger "sdm FirstBoot: Re-enable Plymouth services"
for svc in plymouth-start plymouth-read-write plymouth-quit plymouth-quit-wait plymouth-reboot
do
    systemctl unmask $svc >/dev/null 2>&1
done
#[ -f /etc/systemd/system/.sdm.plymouth-start.service ] && mv /etc/systemd/system/.sdm.plymouth-start.service /etc/systemd/system/plymouth-start.service 
EOF
	fi
    fi    
    #
    #logfreespace "at end of $pfx Phase 1"
    logtoboth "> Plugin $pfx: Final cmdline.txt: $(cat $cfgdir/cmdline.txt)"
    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
