#!/bin/busybox ash
# (c) Robert Shingledecker 2009-2012
. /etc/init.d/tc-functions
useBusybox
PATH=`echo "$PATH" | sed 's|:/etc/sysconfig/tcedir/ondemand||g'`

selectList() {
	TMPFILE=`mktemp`	
	for F in `ls ${TCEDIR}/${DIRNAME}/*${TYPE}`; do
		ITEM=`basename "$F" .sce`
		ADDITEM=1
		
#		Exclude onboot items from select for on demand.
		[ "$TYPE" == "sce" ] && if grep -qw "${ITEM%.sce}" "$TCEDIR"/"$ONBOOTNAME"; then ADDITEM=0; fi
		
#		Exclude current ondemand items from select
		[ -e "$TCEDIR"/ondemand/"$ITEM" ] && ADDITEM=0
		
		[ "$ADDITEM" == "1" ] &&  sudo echo "$ITEM" >> "$TMPFILE"
		sudo chown "$TCUSER":staff "$TMPFILE" > /dev/null 2>&1
	done
	
	sort -f "$TMPFILE"
	rm -f "$TMPFILE"
}

if [ "$1" == "-h" ] || [ "$1" == "--help" ]; then
	echo
	echo "${YELLOW}sce-ondemand - Manage ondemand SCE(s).${NORMAL} Adding an SCE to ondemand creates a"
	echo "               script in /tce/ondemand and updates the Window Manager's"
	echo "               ondemand menu, which provides a convenient way to load"
	echo "               less frequently used SCE(s) after boot."
	echo " "
	echo "Usage:"
	echo " "
	echo "${YELLOW}"sce-ondemand SCE"${NORMAL}      Add SCE to ondemand, update menus."
	echo "${YELLOW}"sce-ondemand -c"${NORMAL}       Display current ondemand SCE(s)."
	echo "${YELLOW}"sce-ondemand -e SCE"${NORMAL}   Load and execute any SCE, do not add to ondemand."
	echo "${YELLOW}"sce-ondemand -l"${NORMAL}       List available SCE(s) to add to ondemand, those"
        echo "                      not yet in sceboot.lst or ondemand."
	echo "${YELLOW}"sce-ondemand -r SCE"${NORMAL}   Remove SCE from ondemand, update menus."
	echo
exit 1
fi


TCUSER=`cat /etc/sysconfig/tcuser`
TCEDIR=/etc/sysconfig/tcedir
[ -d "$TCEDIR" ] || exit 1
WBAR_ICON_DIR="/usr/local/tce.icons"
FREEDESKTOP=/usr/local/share/applications
2>/dev/null read DESKTOP < /etc/sysconfig/desktop

LOCALDIR=/home/"$TCUSER"/.local/share/applications

[ -d /mnt/test ] || sudo mkdir -p /mnt/test

while getopts cler OPTION
do
	case ${OPTION} in
		c) CURRITEMS=1 ;;
		l) LISTITEMS=1 ;;
		e) EXECITEM=1 ;;
		r) REMOVEITEM=1 ;;
		*) echo "Run  sce-ondemand --help  for usage information."
                   exit 1 ;;
	esac
done

shift `expr $OPTIND - 1`

TYPE="sce"

ONBOOTNAME="$(getbootparam lst 2>/dev/null)"
if [ -z "$ONBOOTNAME" ]; then
	ONBOOTNAME="sceboot.lst"
fi

DIRNAME="sce"

if [ "$LISTITEMS" ]; then
	selectList
	echo " "
        echo "The above SCE(s) are available to add to ondemand."
	exit 0
fi

if [ "$CURRITEMS" ]; then
	if [ ! "$(ls -A "$TCEDIR"/ondemand)" ]; then
	echo " "
	echo "No SCE(s) currently in ondemand."
	exit 0
	fi
	for F in `ls "$TCEDIR"/ondemand/*`
	do
		grep -l ."$TYPE" "$F" | awk 'BEGIN{FS="/"}{print $NF}'
	done
	echo " "
        echo "The above SCE(s) are current ondemand items."
	exit 0
fi

if [ "$REMOVEITEM" ]; then
	[ -z "$1" ] && exit 1
	TYPE="${1##*.}"
	APPNAME="${1%.sce}"
	
	TARGET="$TCEDIR"/ondemand/"$APPNAME"
	if [ -e "$TARGET" ]; then
		rm -f  "$TARGET"
		[ $(which "$DESKTOP"_ondemand) ] && sudo su "$TCUSER" -c ""$DESKTOP"_ondemand" 2>/dev/null
		[ $(which "$DESKTOP"_restart) ] && sudo su "$TCUSER" -c ""$DESKTOP"_restart" 2>/dev/null
	fi
	exit 0
fi

if  [ -n "$1" ]; then
	EXTN="$1"
	APPNAME=`basename "$EXTN" .sce`
	if [ "$APPNAME" == "$EXTN" ]
	then
		EXTN="$EXTN".sce
	fi
	if [ ! -f "$TCEDIR"/"$DIRNAME"/"$EXTN" ]; then
		echo "$EXTN does not exist in your SCE directory, exiting.."
		exit 1
	fi
else
	exit 1
fi

if [ "$EXECITEM" ]; then
	TYPE="${1##*.}"
	unset FROMWHERE
	COMMAND="sce-load"
	INSTALLED=/usr/local/tce.installed
	if [ ! -d /tmp/tcloop/"$APPNAME" ] || ! grep "^$APPNAME": /tmp/.debinstalled > /dev/null 2>&1; then
		shift 1 && sudo su "$TCUSER" -c ""${COMMAND}" "$APPNAME""
	else 
		echo -e "\007"
	fi
	if [ -e /usr/share/applications/"$APPNAME".desktop ]; then
		shift 1 && launchApp "$APPNAME" "$@"
	elif [ "$(which $APPNAME)" ]; then
		shift 1 && exec "$APPNAME" "$@"
	else
		echo
		if [ -d /tmp/.X11* ]; then
			#xmessage "Neither freedesktop file nor "$APPNAME" executable exists.  Please invoke from terminal or menu."
			echo -e "\007"
			exit 1
		else
			echo "/usr/share/applications/"$APPNAME".desktop does not exist"
			echo "nor does "$APPNAME" exist in the system PATH."
			echo "Please instead invoke from regular menu or terminal."
			exit 1
		fi
	fi
	exit 0
fi

# Arrive here if no flags were specified.
# Create ondemand item....
# Generic shell script for OnDemand by all WMs
[ -d "$TCEDIR"/ondemand ] || sudo mkdir -p "$TCEDIR"/ondemand > /dev/null 2>&1
[ -d "$TCEDIR"/ondemand ] && sudo chown -R "$TCUSER":staff "$TCEDIR"/ondemand > /dev/null 2>&1
FILE="$TCEDIR"/ondemand/"$APPNAME"
sudo echo '#!/bin/sh' > "$FILE"
sudo echo "ondemand -e $APPNAME.sce" >> "$FILE"
sudo chmod +x "$FILE" > /dev/null 2>&1
sudo chown "$TCUSER":staff "$FILE" > /dev/null 2>&1

# Optional as flwm does not need a make_ondemand.
[ $(which ${DESKTOP}_ondemand) ] && sudo su "$TCUSER" -c "${DESKTOP}_ondemand "$APPNAME""

if [ $(which ${DESKTOP}_restart) ]; then
	sudo su "$TCUSER" -c "${DESKTOP}_restart"
fi
