#!/bin/dash
# start rox-desktop
# Copyright (C) James Budiono 2018, 2019
#
# Environment parameters:
# - DONT_KILL - don't attempt to kill before launching
# - ROX_DEBUG - capture rox debug log, launch as foreground process
# - INIT_DESKTOP - called by xinitrc, prepare desktop for launching
# - WAIT_READY - wait until rox is ready to serve its RPC

### conf
TEMPLATE_DRIVE_ICON_ROOT=/tmp/fatdog-drive-icons # no $USER here - we want to delete all leftover icons
MASTER_PINBOARD_FILE=/etc/xdg/rox.sourceforge.net/ROX-Filer/PuppyPin
PINBOARD_FILE=$HOME/.config/rox.sourceforge.net/ROX-Filer/PuppyPin
GLOBICON_FILE=$HOME/.config/rox.sourceforge.net/ROX-Filer/globicons
ROX_READY_CHECK=0.2

[ "$BOOTSTATE_PATH" ] && [ -e "$BOOTSTATE_PATH" ] &&
. $BOOTSTATE_PATH 	# TMPFS_MOUNT, SAVEFILE_MOUNT

### init ROX desktop
init_desktop() {
	# 1. Create pinboard file if it does not exist
	if [ ! -e $PINBOARD_FILE ]; then
		mkdir -p ${PINBOARD_FILE%/*}
		cp $MASTER_PINBOARD_FILE $PINBOARD_FILE
	fi

	# 2. Cleanup stuff from previous invocations
	# 2.a remove stale drive icons
	sed -i -e "\%$TEMPLATE_DRIVE_ICON_ROOT% d" $PINBOARD_FILE
	sed -i -e "\%$TEMPLATE_DRIVE_ICON_ROOT%,\%</rule>% d" $GLOBICON_FILE

	# 2.b remove all save-* buttons, calc rightmost location
	save2flash="$(grep -m1 save2flash $PINBOARD_FILE)"
	save2session="$(grep -m1 save2session $PINBOARD_FILE)"
	sed -i -e '/save2flash/ d; /save2session/ d' $PINBOARD_FILE
	p=$(xdpyinfo); p=${p#*dimensions:}
	p=$((${p%%x*}-96)) # 96 pixels from right

	# 3. RAM-layer/first-boot setup
	if [ "$TMPFS_MOUNT" ]; then
		if [ -z "$SAVEFILE_MOUNT" ]; then
			# RAM layer but no savefile - assume first boot
			true # /usr/sbin/background_reshape
		else
			# RAM layer and with savefile - enable our RAM-save layer button
			! [ "$save2flash" ] && save2flash='<icon x="'$p'" y="70" label="'"$(gettext 'Save RAM layer')"'">/usr/sbin/save2flash</icon>'
			sed -i -e "s_</pinboard>_$save2flash\\n</pinboard>_" $PINBOARD_FILE
		fi
	fi

	# 4. Add multisession save button if multisession mode
	if [ "$MULTI_DEVICE" ]; then
		! [ "$save2session" ] && save2session='<icon x="'$p'" y="160" label="'"$(gettext 'Save session')"'">/usr/sbin/save2session</icon>'
		sed -i -e "s_</pinboard>_$save2session\\n</pinboard>_" $PINBOARD_FILE
	fi
}

### get version - used to test readyness
rox_get_version() {
	rox -R << EOF
<?xml version="1.0"?>
<env:Envelope xmlns:env="http://www.w3.org/2001/12/soap-envelope">
 <env:Body xmlns="http://rox.sourceforge.net/SOAP/ROX-Filer">
  <Version/>
 </env:Body>
</env:Envelope>
EOF
}


### main
cd $HOME

# options
unset init debug dontkill waitready
[ "$INIT_DESKTOP" ] && init=true dontkill=yes waitready=yes
[ "$ROX_DEBUG" ]  && debug=true
[ "$DONT_KILL" ]  && dontkill=true
[ "$WAIT_READY" ] && waitready=true
unset DONT_KILL ROX_DEBUG INIT_DESKTOP WAIT_READY

# init
[ "$dontkill" ] || killall ROX-Filer # kill running instance first
[ "$init" ] && init_desktop

# launch
if [ -f /usr/bin/rox-debug ] || [ "$debug" ]; then
	mkdir -p /tmp/rox; chmod 1777 /tmp/rox
	rox "$@" -n -p $PINBOARD_FILE >> /tmp/rox/debug-$USER.$XSESSION_ID.log 2>&1 &
else
	rox "$@" -p $PINBOARD_FILE > /dev/null 2>&1
fi

# wait until ready
if [ "$waitready" ]; then
	while ! rox_get_version  | grep -q VersionResponse; do
		sleep $ROX_READY_CHECK
	done
fi

# done
