#!/bin/bash
#
# This is an sdm plugin for: disables
#
# 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="|bluetooth|piwiz|triggerhappy|wifi|"
rqdargs=""

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
    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" || exit
    #logfreespace "at start of Plugin $pfx Phase 1"
    #
    #
    #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"
    #
    dbls="$1|"
    IFS="|" read -a dlist <<< "$vldargs"
    for ditem in "${dlist[@]}"
    do
	if [ -v $ditem ]
	then
	    case "$ditem" in
		bluetooth)
		    fndis="/etc/sdm/0piboot/050-disable-bt.sh"
		    if [ -f $fndis ]
		    then
			logtoboth "% Plugin $pfx: Bluetooth already disabled"
		    else
			logtoboth "> Plugin $pfx: Set Bluetooth to disable in sdm FirstBoot"
			cat > $fndis <<EOF
#!/bin/bash
blfile="blacklist-sdm-bluetooth.conf"
logger "sdm FirstBoot: Disable bluetooth using /etc/modprobe.d/\$blfile"
printf "blacklist btbcm\nblacklist hci_uart\n" > /etc/modprobe.d/\$blfile
systemctl disable hciuart
EOF
		    fi
		    ;;
		piwiz)
		    logtoboth "> Plugin $pfx: Disable running piwiz and userconfig on first system boot"
		    [ -f /etc/xdg/autostart/piwiz.desktop ] && mv /etc/xdg/autostart/piwiz.desktop /etc/xdg/autostart/.piwiz.desktop.sdm
		    systemctl disable userconfig.service > /dev/null 2>&1
		    systemctl mask    userconfig.service > /dev/null 2>&1
		    getent passwd rpi-first-boot-wizard > /dev/null && userdel -r rpi-first-boot-wizard > /dev/null 2>&1
		    [ -f /etc/systemd/system/getty@tty1.service.d/autologin.conf ] && mv /etc/systemd/system/getty@tty1.service.d/autologin.conf /etc/systemd/system/getty@tty1.service.d/.autologin.conf.sdm
		    rm -f /etc/sudoers.d/010_wiz-nopasswd
		    rm -f /etc/xdg/autostart/deluser.desktop
		    ;;
		triggerhappy)
		    logtoboth "> Plugin $pfx: Disable triggerhappy service"
		    systemctl disable triggerhappy.service
		    systemctl disable triggerhappy.socket
		    #Eliminate thd.socket errors from udev
		    [ -f /lib/udev/rules.d/60-triggerhappy.rules ] && mv /lib/udev/rules.d/60-triggerhappy.rules /lib/udev/rules.d/.60-triggerhappy-sdm.rules
		    [ -f /usr/sbin/thd ] && mv /usr/sbin/thd /usr/sbin/thd.sdm
		    ;;
		wifi)
		    fndis="/etc/sdm/0piboot/055-disable-wifi.sh"
		    if [ -f $fndis ]
		    then
			logtoboth "% Plugin $pfx: WiFi already disabled"
		    else
			logtoboth "> Plugin $pfx: Set WiFi to disable in sdm FirstBoot"
			cat > $fndis <<EOF
#!/bin/bash
blfile="blacklist-sdm-wifi.conf"
logger "sdm FirstBoot: Disable wlan0 using /etc/modprobe.d/\$blfile"
printf "blacklist brcmfmac\nblacklist brcmutil\n" > /etc/modprobe.d/\$blfile
EOF
		    fi
		    ;;
	    esac
	fi
    done
    #
    #logfreespace "at end of $pfx Custom Phase post-install"
    logtoboth "* Plugin $pfx: Complete Phase post-install"
fi
