#!/etc/tsharc/busybox ash
CONFIGDIRECTORY="/etc/tsharc"
PATH="/etc/tsharc"

alias chmod="${CONFIGDIRECTORY}/busybox chmod"
alias ps="${CONFIGDIRECTORY}/busybox ps"
alias grep="${CONFIGDIRECTORY}/busybox grep"
alias sleep="${CONFIGDIRECTORY}/busybox sleep"
alias sed="${CONFIGDIRECTORY}/busybox sed"
alias id="${CONFIGDIRECTORY}/busybox id"

DAEMON=/usr/bin/tsharcd
CPCONSOLE=/usr/bin/hlincal
TSHARCSCONF=${CONFIGDIRECTORY}/tsharcs.conf
tsharcd_args="--daemon"
tsharcd_output="/dev/null"

# Comment next line out to disable logging to file
#tsharcd_output="/var/log/tsharc/tsharc.log"
# Uncomment next line to enable logging to screen
#tsharcd_output="/dev/stdout"
# tsharcd_output="/tmp/tsharcdoutput"
#tsharc_output=""


checkForX()
{
	# Is X running?
	if [ "" = "$(ps -A | grep "X" | sed -e '/grep/,/grep/d')" ]; then
		CURRENTID=0
		COUNT=$($CPCONSOLE IOCTL_GET_FEATURE 255 FEATURE_GET_CONTROLLER_COUNT)
		while [ "$CURRENTID" != "$COUNT" ];
		do
			echo "boot.tsharc: Temporarily disabling X-windows TSHARC daemon functionality for ID: $CURRENTID (since X not running)"
	
			$CPCONSOLE IOCTL_SET_FEATURE $CURRENTID FEATURE_MOUSE_ENABLE 0
			CURRENTID=$((CURRENTID + 1))
		done;
	
	
	else
		CURRENTID=0
		COUNT=$($CPCONSOLE IOCTL_GET_FEATURE 255 FEATURE_GET_CONTROLLER_COUNT)

		echo
		while [ "$CURRENTID" != "$COUNT" ];
		do
			echo "boot.tsharc: Enabling X-windows TSHARC daemon functionality for ID: $CURRENTID"
			$CPCONSOLE IOCTL_SET_FEATURE $CURRENTID FEATURE_MOUSE_ENABLE 1
			CURRENTID=$((CURRENTID + 1))
		done;
	fi
}

startDaemon()
{
	if [ "$(ps -A | grep "tsharcd" | sed -e '/grep/,/grep/d')" != "" ]; then
		echo " Already running. Done."
		checkForX
		exit 1
	fi

	if [ "" != "$(ps -A | grep "gpm" | sed -e '/grep/,/grep/d')" ]; then
		echo "gpm is running.  This process may need to be stopped before running TSHARC daemon"
	fi

	${CONFIGDIRECTORY}/add_serio_raw 2> /dev/null

#	if [ -f "/usr/bin/enable-serio-raw.sh" ]; then	
#		/usr/bin/enable-serio-raw.sh
#	fi

	if [ ! -f $DAEMON ]; then
		echo
		echo -n "Cannot start TSHARC daemon, file does not exist.  Please run install.sh from installation directory."
	else
	$DAEMON ${CONFIGDIRECTORY} $tsharcd_args > $tsharcd_output
	sleep 1
	checkForX

	fi

	echo "Done."
}

stopDaemon()
{
	"$CPCONSOLE" IOCTL_EXIT
#	rm -vf /tmp/tsharcctl
#	rm -vf /tmp/tsharcoutput
#	if [ -f "/usr/bin/disable-serio-raw.sh" ]; then	
#		/usr/bin/disable-serio-raw.sh
#	fi

}

createFIFOBuffers()
{
#	if [ ! -p "/tmp/tsharcctl" ]; then
#		mkfifo --mode=0000 /tmp/tsharcctl
#	fi

#	if [ ! -p "/tmp/tsharcoutput" ]; then
#		mkfifo --mode=0000 /tmp/tsharcoutput
#	fi

#	if [ ! -p "/tmp/tsharcevents" ]; then
#		mkfifo --mode=0000 /tmp/tsharcevents
#	fi

#	chmod 666 /tmp/tsharcctl
#	chmod 666 /tmp/tsharcoutput
#	chmod 666 /tmp/tsharcevents


	if [ $(id -u) != 0 ]; then
#		echo "Root permissions necessary to changed permissions of dev nodes"
		echo
	else
		# These next two lines are necessary to communicate with usb controller
		# without root access
		# 2.4 kernels
	if [ -d "/proc/bus/usb" ]; then	
		chmod -R 777 /proc/bus/usb/*
	fi
		# 2.6 kernels
	if [ -d "/dev/bus/usb" ]; then	
		chmod -R 777 /dev/bus/usb/*
	fi
		
		chmod 666 /dev/ttyS*
		chmod 666 /dev/tsharc*
	fi
}

case "$1" in
	create)

#	if [ $(id -u) != 0 ]; then
#		echo "Root permissions necessary for this parameter"
#		exit 1
#	fi

	;;
    start)
	echo -n "Starting tsharcd "
	createFIFOBuffers
	startDaemon
	;;
    stop)
	echo -n "Stopping tsharcd:"

	if [ "$(ps -A | grep "tsharcd" | sed -e '/grep/,/grep/d')" = "" ]; then
		echo " Cannot stop since TSHARC daemon not running. Done."
		exit 1
	fi
	stopDaemon

	echo " Done."
	;;
    restart)
	echo -n "Restarting tsharcd:"

	if [ "$(ps -A | grep "tsharcd" | sed -e '/grep/,/grep/d')" = "" ]; then
		echo " Cannot restart since TSHARC daemon not running. Done."
		exit 1
	fi

	stopDaemon
	startDaemon

	echo " Done."
	;;
    status)
	echo -n "Checking for tsharcd:"
	RUNSTATUS="$(ps -A | grep "tsharcd" | sed -e '/grep/,/grep/d')"

	if [ -z "$RUNSTATUS" ]; then
		echo -n " Not running."
	else	
		echo -n " Running."
	fi

	echo " Done."
	;;
    *)
	echo "Usage: $0 {start|stop|restart|status}"
	exit 1
	;;
esac
