#!/bin/bash
#
# This is an sdm plugin for: wificonfig
#
# wificonfig runs a Captive Portal at system First Boot to gather and test WiFi Credentials
#
# 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"
vldargs="|apssid|apip|country|defaults|facility|retries|timeout|wifilog|"
rqdargs=""
loadparams
assetdir="$SDMPT/etc/sdm/wificonfig"

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
    # Check keys validity: defaults, country, retries, timeout
    mkdir -p $assetdir
    if [ "$defaults" != "" ]
    then
	if [ -f $defaults ]
	then
	    logtoboth "> Plugin $pfx: Copy defaults file '$defaults' to IMG $assetdir/wificonfig-defaults"
	    cp $defaults $assetdir/wificonfig-defaults
	else
	    logtoboth "% Plugin $pfx: Defaults file '$defaults' not found; ignoring"
	fi
    fi
    mustfix=0
    if [ "$country" != "" ]
    then
	! (ckwificountry ${country^^}) && logtoboth "% Plugin $pfx: Unrecognized Country key value '$country'" && mustfix=1
    fi
    if [ "$retries" != "" ]
    then
	[ "$((retries))" == 0 ] && logtoboth "% Plugin $pfx: Invalid retries key value '$retries'" && mustfix=1
    fi
    if [ "$timeout" != "" ]
    then
	[ "$((timeout))" == 0 ] && logtoboth "% Plugin $pfx: Invalid timeout key value '$timeout'" && mustfix=1
    fi
    [ $mustfix -eq 1 ] && logtoboth "? Plugin $pfx: Above key value errors MUST be corrected to ensure proper operation"
    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"

    #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"
    # Undo sdm disable wifi if done
    [ -f /etc/sdm/0piboot/055-disable-wifi.sh ] && mv /etc/sdm/0piboot/055-disable-wifi.sh /etc/sdm/0piboot/.055-disable-wifi.sh-wificonfig
    # Configure for FirstBoot: Set loadlocal="plugin" and create plugin-wificonfig.sh
    switches=""
    [ "$apssid" != "" ] && switches="$switches --apssid $apssid"
    [ "$apip" != "" ] && switches="$switches --apip $apip"
    [ "$country" != "" ] && switches="$switches --country $country"
    if [ "$defaults" != "" ]
    then
	[ -f $assetdir/wificonfig-defaults ] && switches="$switches --defaults $assetdir/wificonfig-defaults"
    fi
    [ "$facility" != "" ] && switches="$switches --facility $facility"
    [ "$retries" != "" ] && switches="$switches --retries $retries"
    [ "$timeout" != "" ] && switches="$switches --timeout $timeout"
    [ "$wifilog" == "" ] && wifilog="$assetdir/wifi-config.log"
    [ "$wifilog" != "" ] && dbgout=">$wifilog 2>&1" || dbgout=""
    writeconfig
    cat > /etc/sdm/xpiboot/020-plugin-wificonfig.sh <<EOF
#!/bin/bash
source /etc/sdm/sdm-readparams
logger "sdm FirstBoot: Run plugin-configured Captive Portal"
rfkill unblock wlan
bootlog "Start Captive Portal to obtain WiFi SSID and Password"
write_console "Start Captive Portal to obtain WiFi SSID and Password"
write_console0 ""
write_console0 "Watch the ACT light"
write_console0 "   ----  Access Point not started"
write_console0 "   -.--  Access Point started"
write_console0 "   --.-  WiFi connection test in progress"
write_console0 "   ....  Connection test results ready"
write_console0 "   ...-  Restoring network configuration to exit"
$sdmdir/sdm-cportal --sdm $switches $dbgout
EOF
    logtoboth "* Plugin $pfx: Complete Phase post-install"
fi
