#!/bin/dash
# launch desktop
# Copyright (C) James Budiono 2012, 2013, 2015, 2019
# License: GNU GPL Version 3 or later
#
# previously named rc.Xservices (in Puppy this is /usr/sbin/delayedrun)
# cleaned up and re-written from scratch for Fatdog64 600
# called by xinitrc
#

### configuration
[ "$BOOTSTATE_PATH" ] && [ -e "$BOOTSTATE_PATH" ] &&
. $BOOTSTATE_PATH 	# SAVEFILE_MOUNT

STARTUP_DELAY=5 		# in seconds
TRAYTEST_TIMEOUT=5      # timeout waiting for panel, in seconds

SYSTEM_XDESKTOP=/etc/X11/xdesktop
USER_XDESKTOP=$FATDOG_STATE_DIR/xdesktop
USE_DRIVE_ICON=yes		# if blank, don't start drive icons

SYSTEM_STARTUP_DIR=/etc/xdg/Startup
USER_STARTUP_DIR=$HOME/Startup
GAP_DELAY=0.2           # delay between starting subsequent files

EVENTMANAGER_CONFIG=/etc/eventmanager
[ -e $EVENTMANAGER_CONFIG ] &&
. $EVENTMANAGER_CONFIG # STARTUP_DELAY, USE_DRIVE_ICON, TRAYTEST_TIMEOUT (override)



######### functions ##########

launch_desktop() {
	if [ -e $USER_XDESKTOP ] && [ -x $USER_XDESKTOP ]; then
		$USER_XDESKTOP &   # user desktop overrides system
	elif [ -e $SYSTEM_XDESKTOP ] && [ -x $SYSTEM_XDESKTOP ]; then
		$SYSTEM_XDESKTOP & # system desktop
	else
		# default desktop: ROX desktop
		INIT_DESKTOP=yes rox-desktop
		if [ $USE_DRIVE_ICON ]; then
			fatdog-drive-icon-frontend.sh &
		fi
	fi
}

launch_startup_files() {
	# make sure X and core X apps (window manager, panels, etc) have started and settled
	sleep $STARTUP_DELAY # in seconds

	### Startup files
	# wait for tray readiness before launching everything else
	trayTest $TRAYTEST_TIMEOUT ## && gtrayTest $TRAYTEST_TIMEOUT 

	# run global Startup files 
	if [ -d $SYSTEM_STARTUP_DIR ]; then
		for p in $SYSTEM_STARTUP_DIR/*; do
			if [ -x $p ]; then
			$p & sleep $GAP_DELAY
			fi
		done
	fi

	# run per-user Startup files 
	if [ -d $USER_STARTUP_DIR ]; then
		for p in $USER_STARTUP_DIR/*; do
			if [ -x $p -a ! -d $p ]; then
			$p & sleep $GAP_DELAY
			fi
		done
	fi	
}


######### main ##########

launch_desktop
launch_startup_files &

