#!/bin/bash
#
# This is an sdm plugin for: lxde
#
# 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="|lhmouse|lxde-config|user|wayfire-ini|wf-panel-pi|"
rqdargs=""
assetdir="$SDMPT/etc/sdm/assets/lxde"

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"
    if [ "$lxde__config" != "" ]
    then
	IFS="," read -a largs <<< "$lxde__config"
	for lx in "${largs[@]}"
	do
	    IFS=":=" read lxname lxfname <<< "$lx"
	    case "${lxname,,}" in
		bookmarks|libfm|pcmanfm|lxterminal)
		    mkdir -p $assetdir/$lxname/$username
		    if [ -f $lxfname ]
		    then
			logtoboth "> Plugin $pfx: Copy '$lxfname' to $assetdir/$lxname/$username"
			cp -a $lxfname $assetdir/$lxname/$username
		    else
			logtobothex "? Plugin $pfx: lxde-config type '$lxname' file '$lxfname' not found"
		    fi
		    ;;
		*) logtobothex "? Plugin $pfx: lxde-config item '$lxname' is unknown"
		   ;;
	    esac
	done
    fi
    if [ "$wayfire__ini" != "" ]
    then
	if [ -f $wayfire__ini ]
	then
	    mkdir -p $assetdir/wayfire-ini/$username
	    logtoboth "> Plugin $pfx: copy '$wayfire__ini' to $assetdir/wayfire-ini/$username"
	    cp -a $wayfire__ini $assetdir/wayfire-ini/$username/wayfire.ini
	else
	    logtobothex "? Plugin $pfx: wayfire-ini file '$wayfire__ini' not found"
	fi
    fi
    if [ "$wf__panel__pi" != "" ]
    then
	if [ -f $wf__panel__pi ]
	then
	    mkdir -p $assetdir/wf-panel-pi/$username
	    logtoboth "> Plugin $pfx: copy '$wf__panel__pi' to $assetdir/wf-panel-pi/$username"
	    cp -a $wf__panel__pi $assetdir/wf-panel-pi/$username/wf-panel-pi.ini
	else
	    logtobothex "? Plugin $pfx: wf-panel-pi file '$wf__panel__pi' 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"
    #
    # INSERT your Plugin Phase 1 customization stuff here
    # In Phase 1 all references to directories in the image can be direct
    #
    #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"
    fi
    #logfreespace "at start of Plugin $pfx Phase post-install"

    if [ -f /usr/bin/startlxde-pi ]
    then
	logtoboth "> Plugin $pfx: Special LXDE-Pi handling"
	gx=$(getfilegroup /home/$dstuser)
	mkdir -p /home/$dstuser/.config
	if [ -f $assetdir/wf-panel-pi/$srcuser/wf-panel-pi.ini ]
	then
	    logtoboth "> Plugin $pfx: Copy $assetdir/wf-panel-pi/$srcuser/wf-panel-pi.ini to /home/$dstuser/.config"
	    cp -a $assetdir/wf-panel-pi/$srcuser/wf-panel-pi.ini /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
	if [ -f $assetdir/wayfire-ini/$srcuser/wayfire.ini ]
	then
	    logtoboth "> Plugin $pfx: Copy $assetdir/wayfire-ini/$srcuser/wayfire.ini to /home/$dstuser/.config"
	    cp -a $assetdir/wayfire-ini/$srcuser/wayfire.ini /home/$dstuser/.config
	elif [ -d /etc/wayfire ]
	then
	    cp /etc/wayfire/template.ini /home/$dstuser/.config/wayfire.ini
	fi
	if [ -f /home/$dstuser/.config/wayfire.ini ]
	then
	    logtoboth "> Plugin $pfx: Set protection on /home/$dstuser/.config/wayfire.ini"
	    setfileownmode /home/$dstuser/.config/wayfire.ini 644 $dstuser:$gx
	    if [ "$keymap" != "" ]
	    then
		logtoboth "> Plugin $pfx: Set keymap '$keymap' in /home/$dstuser/.config/wayfire.ini"
		sed -i "s/xkb_layout.*=.*/xkb_layout=$keymap/" /home/$dstuser/.config/wayfire.ini
	    fi
	fi
	# Set left-handed mouse if requested
	if [ -v lhmouse ]
	then
	    logtoboth "> Plugin $pfx: Enable left-handed mouse for user '$dstuser'"
	    if [ -f /home/$dstuser/.config/wayfire.ini ]
	    then
		sed -i '/xkb_layout.*=.*/a left_handed_mode=true' /home/$dstuser/.config/wayfire.ini
	    fi
	    dirx="/home/$dstuser/.config/lxsession/LXDE-pi"
	    mkdir -p $dirx
	    cp -a /etc/xdg/lxsession/LXDE-pi/desktop.conf $dirx
	    setfileownmode $dirx/desktop.conf 644 $dstuser:$gx
	    sed -i "s/LeftHanded=0/LeftHanded=1/" $dirx/desktop.conf
	fi
	# Copy LXDE config files if requested
	if [ "$lxde__config" != "" ]
	then
	    for lx in libfm pcmanfm lxterminal bookmarks
	    do
		dirx="/home/$dstuser/.config/$lx"
		if compgen -G "$assetdir/$lx/$srcuser/*" > /dev/null
		then
		    case "$lx" in
			libfm|lxterminal)
			    logtoboth "> Plugin $pfx: Copy '$lx' config file to $dirx/$lx.conf"
			    mkdir -p $dirx
			    cp -a $assetdir/$lx/$srcuser/* $dirx/$lx.conf
			    ;;
			pcmanfm)
			    logtoboth "> Plugin $pfx: Copy '$lx' config file to $dirx/LXDE-pi/$lx.conf"
			    mkdir -p $dirx/LXDE-pi
			    cp -a $assetdir/$lx/$srcuser/* $dirx/LXDE-pi/$lx.conf
			    ;;
			bookmarks)
			    #logtoboth "> Plugin $pfx: Copy '$lx' to /home/$dstuser/.config/gtk-3.0/bookmarks"
			    #mkdir -p /home/$dstuser/.config/gtk-3.0
			    #cp -a $assetdir/$lx/$srcuser/* /home/$dstuser/.config/gtk-3.0/bookmarks
			    logtoboth "> Plugin $pfx: Copy '$lx' to /home/$dstuser/.gtk-bookmarks"
			    cp -a $assetdir/$lx/$srcuser/* /home/$dstuser/.gtk-bookmarks
			    ;;
		    esac
		fi
	    done
	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: LXDE not installed on this system"
    fi

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