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

function stuffconfig() {
    #
    # Copies all the files gleaned from the config:/path/dir option
    #
    local  assetconfig="$assetdir/$srcuser/all-config" config="/home/$dstuser/.config"

    for f in autostart environment menu.xml rc.xml shutdown themerc
    do
	if [ -f $assetconfig/$f ]
	then
	    logtoboth "> Plugin $pfx: Copy labwc config '$f' from $assetconfig to $config/labwc"
	    cp -a $assetconfig/$f $config/labwc
	fi
    done

    for f in bookmarks desktop-items libfm pcmanfm lxterminal wf-panel-pi
    do
	case $f in
            bookmarks)
		if [ -f $assetconfig/bookmarks ]
		then
		    logtoboth "> Plugin $pfx: Copy 'bookmarks' from $assetconfig/bookmarks to /home/$dstuser/.gtk-bookmarks"
		    cp -a $assetconfig/bookmarks /home/$dstuser/.gtk-bookmarks
		fi
		;;
            pcmanfm)
		if [ -f $assetconfig/$f.conf ]
		then
		    logtoboth "> Plugin $pfx: Copy '$f.conf' from $assetconfig to $config/pcmanfm/LXDE-pi"
		    mkdir -p $config/pcmanfm/LXDE-pi
		    cp -a $assetconfig/$f.conf $config/$f/LXDE-pi
		fi
		;;
            desktop-items)
		if [ "$(compgen -G "$assetconfig/desktop-items*.conf")" != "" ]
		then
		    logtoboth "> Plugin $pfx: Copy 'desktop-items*' from $assetconfig to $config/pcmanfm/LXDE-pi"
		    mkdir -p $config/pcmanfm/LXDE-pi
		    cp -a $assetconfig/desktop-items*.conf $config/pcmanfm/LXDE-pi
		fi
		;;
            libfm|lxterminal)
		if [ -f $assetconfig/$f.conf ]
		then
		    logtoboth "> Plugin $pfx: Copy '$f.conf' from $assetdirconfig to $config/$f"
		    mkdir -p $config/$f
		    cp -a $assetconfig/$f.conf $config/$f
		fi
		;;
            wf-panel-pi)
		if [ -f $assetconfig/wf-panel-pi.ini ]
		then
		    logtoboth "> Plugin $pfx: Copy 'wf-panel-pi' from $assetconfig/wf-panel-pi.ini to $config"
		    cp -a $assetconfig/wf-panel-pi.ini $config
		fi
		;;
	esac
    done
}

function addelmtoxml() {
    # path to enclosing element
    # element name to add
    # value
    #
    local encpath="$1" newelk="$2" newval="$3" rcxml="/home/$dstuser/.config/labwc/rc.xml"

    IFS="/" read e0 e1 e2 <<< "$encpath"

    if [ ! -f $rcxml ]
    then
	cat > $rcxml <<EOF
<openbox_config xmlns="http://openbox.org/3.4/rc">
<libinput>
  <device category="default">
  </device>
</libinput>
</openbox_config>
EOF
	cat > /dev/null <<EOF
<?xml version="1.0"?>

<!--
  For a complete set of options with comments, see /usr/share/doc/rc.xml.all
-->

<labwc_config/>
EOF
    fi
    tmpf=$(mktemp)
    logtoboth "> Plugin $pfx: Add/Update labwc rc.xml '$encpath' '$newelk' value '$newval'"
    rm -f $tmpf
    cat > $tmpf <<EOF
#!/bin/bash
xmlstarlet ed -L -s $e1 -t elem -n $e2 -v '' -d '/$e1/$e2[position() != 1]' -s /$e1/$e2 -t elem -n $newelk -v "$newval" $rcxml
EOF
    chmod 755 $tmpf
    $tmpf
    rm -f $tmpf
}
 
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
# not done: keymap lhmouse numlock
vldargs="|all-config|app-config|kanshi|lhmouse|labwc-config|numlock|user|wf-panel-pi|"
rqdargs=""
assetdir="$SDMPT/etc/sdm/assets/labwc"

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
    plugin_printkeys
    [ "$user" != "" ] && username=$user || username="default"
    mkdir -p $assetdir/$username

    if [ "$all__config" != "" ]
    then
	if [ -d $all__config ]
	then
	    logtoboth "> Plugin $pfx: Copy config directory '$all__config' to $assetdir/$username/all-config"
	    cp -ar $all__config $assetdir/$username/all-config
	else
	    logtobothex "? Plugin $pfx: all-config directory '$all__config' not found or not a directory"
	fi
    fi

    if [ "$app__config" != "" ]
    then
	IFS="," read -a largs <<< "$app__config"
	for lx in "${largs[@]}"
	do
	    IFS=":=" read lxname lxfname <<< "$lx"
	    case "${lxname,,}" in
		bookmarks|desktop-items|libfm|pcmanfm|lxterminal)
		    if [ -f $lxfname ]
		    then
			logtoboth "> Plugin $pfx: Copy '$lxfname' to $assetdir/$username"
			cp -a $lxfname $assetdir/$username
		    else
			logtobothex "? Plugin $pfx: app-config type '$lxname' file '$lxfname' not found"
		    fi
		    ;;
		*) logtobothex "? Plugin $pfx: app-config item '$lxname' is unknown"
		   ;;
	    esac
	done
    fi

    if [ "$labwc__config" != "" ]
    then
	IFS="," read -a largs <<< "$labwc__config"
	for lc in "${largs[@]}"
	do
	    IFS=":=" read lcname lcfname <<< "$lc"
	    case "${lcname,,}" in
		autostart|environment|menu|rc|shutdown|themerc)
		    if [ -f $lcfname ]
		    then
			logtoboth "> Plugin $pfx: Copy '$lcfname' to $assetdir/$username/$lcname"
			cp -a $lcfname $assetdir/$username
		    else
			logtobothex "? Plugin $pfx: labwc-config type '$lcname' file '$lcfname' not found"
		    fi
		    ;;
		*) logtobothex "? Plugin $pfx: labwc-config item '$lcname' is unknown"
		   ;;
	    esac
	done
    fi

    if [ "$wf__panel__pi" != "" ]
    then
	if [ -f $wf__panel__pi ]
	then
	    logtoboth "> Plugin $pfx: Copy '$wf__panel__pi' to $assetdir/$username/wf-panel-pi.ini"
	    cp -a $wf__panel__pi $assetdir/$username/wf-panel-pi.ini
	else
	    logtobothex "? Plugin $pfx: wf-panel-pi file '$wf__panel__pi' not found"
	fi
    fi

    if [ "$kanshi" != "" ]
    then
	if [ -f "$kanshi" ]
	then
	    logtoboth "> Plugin $pfx: Copy '$kanshi' to $assetdir/$username/kanshi.conf"
	    cp $kanshi $assetdir/$username/kanshi.conf
	else
	    logtobothex "? Plugin $pfx: kanshi file '$kanshi' not found"
	fi
    fi
    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 [ -v lhmouse ] || [ -v numlock ]
    then
	logtoboth "> Plugin $pfx: Install xmlstarlet"
	installpkgsif xmlstarlet
    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"
    [ "$user" != "" ] && srcuser=$user || srcuser="default"
    dstuser=$user
    [ "$dstuser" == "" ] && dstuser=$myuser
    if [ "$dstuser" == "" ]
    then
	logtobothex "? Plugin $pfx: No users created with user plugin and no 'user' argument"
    fi
    gx=$(getfilegroup /home/$dstuser)

    #logfreespace "at start of Plugin $pfx Phase post-install"

    if [ "$(type -p labwc)" != "" ]
    then
	[ -d /home/$dstuser/.config/labwc ] || logtoboth "> Plugin $pfx: labwc is installed; Configure user '$dstuser'"
	mkdir -p /home/$dstuser/.config/labwc
	wfppasset="$assetdir/$srcuser/wf-panel-pi.ini"
	if [ -f $wfppasset ]
	then
	    logtoboth "> Plugin $pfx: Copy $wfppasset to /home/$dstuser/.config"
	    cp -a $wfppasset /home/$dstuser/.config
	    logtoboth "> Plugin $pfx: Set protection on /home/$dstuser/.config/wf-panel-pi.ini"
	    setfileownmode /home/$dstuser/.config/wf-panel-pi.ini 644 $dstuser:$gx
	fi

	# Copy all-config files
	if [ "$all__config" != "" ]
	then
            logtoboth "> Plugin $pfx: Process 'all-config' directory '$assetdir/$srcuser/all-config'"
            stuffconfig
	fi

	# Copy labwc-config files
	if [ "$labwc__config" != "" ]
	then
	    dirx="/home/$dstuser/.config/labwc"
	    IFS="," read -a largs <<< "$labwc__config"
	    for lc in "${largs[@]}"
	    do
		IFS=":=" read lcname lcfname <<< "$lc"
		case "${lcname,,}" in
		    autostart|environment|menu|rc|shutdown|themerc)
			logtoboth "> Plugin $pfx: Copy '$lcname' file '$(basename $lcfname)' to $dirx"
			cp $assetdir/$srcuser/$(basename $lcfname) $dirx
			;;
		esac
	    done
	fi

	# Copy app config files if requested
	if [ "$app__config" != "" ]
	then
	    dirx="/home/$dstuser/.config"
	    IFS="," read -a largs <<< "$app__config"
	    for lc in "${largs[@]}"
	    do
		IFS=":=" read lcname lcfname <<< "$lc"
		case "${lcname,,}" in
		    libfm|lxterminal)
			logtoboth "> Plugin $pfx: Copy '$lcname' config to $dirx/$lcname.conf"
			mkdir -p $dirx/$lcname
			cp -a $assetdir/$srcuser/$(basename $lcfname) $dirx/$lcname/$lcname.conf
			;;
		    pcmanfm|desktop-items)
			dirp=$dirx/pcmanfm
			logtoboth "> Plugin $pfx: Copy '$lcname' config to $dirp/LXDE-pi"
			mkdir -p $dirp/LXDE-pi
			[ "$lcname" == "pcmanfm" ] && cp -a $assetdir/$srcuser/$(basename $lcfname) $dirp/LXDE-pi
			[ "$lcname" == "desktop-items" ] && cp -a $assetdir/$srcuser/desktop-items-* $dirp/LXDE-pi
			;;
		    bookmarks)
			logtoboth "> Plugin $pfx: Copy '$lcname' to /home/$dstuser/.gtk-bookmarks"
			cp -a $assetdir/$srcuser/$(basename $lcfname) /home/$dstuser/.gtk-bookmarks
			chown $dstuser:$gx /home/$dstuser/.gtk-bookmarks
			;;
		esac
	    done
	fi

	# Set left-handed mouse if requested
	if [ -v lhmouse ]
	then
	    logtoboth "> Plugin $pfx: Enable left-handed mouse for user '$dstuser'"
	    addelmtoxml /openbox_config/libinput leftHanded yes
	fi

	# Set numlock if requested
	if [ -v numlock ]
	then
	    [ "$numlock" == "" ] && numlock=off
	    logtoboth "> Plugin $pfx: Configure numlock to '$numlock' for user '$dstuser'"
	    addelmtoxml /openbox_config/keyboard numlock $numlock
	fi

	if [ -f $assetdir/$srcuser/kanshi.conf ]
	then
	    mkdir -p /home/$dstuser/.config/kanshi
	    logtoboth "> Plugin $pfx: Copy kanshi config file from $assetdir/$srcuser/kanshi.conf to /home/$dstuser/.config/kanshi/config"
	    cp $assetdir/$srcuser/kanshi.conf /home/$dstuser/.config/kanshi/config
	fi

	logtoboth "> Plugin $pfx: Set owner to '$dstuser:$gx' and protection to 700 on '/home/$dstuser/.config' directory tree"
	chown -R $dstuser:$gx /home/$dstuser/.config
	chmod 700 /home/$dstuser/.config
    else
	logtoboth "% Plugin $pfx: labwc is not installed on this system"
    fi

    #logfreespace "at end of $pfx Custom Phase post-install"
    logtoboth "* Plugin $pfx: Complete Phase post-install"
fi
exit
