#! /bin/bash

# For RedHat and cousins:
# chkconfig: 2345 05 05
# description: Promethean Xorg Virtual Device Creator
# processname: activdevices
# create a fifo for x server to service Promethean devices

### BEGIN INIT INFO
# Provides:  			activdevices
# Required-Start:		$all
# Required-Stop:
# Default-Start:		2 3 4 5 
# Default-Stop:			0 1 6
# Description:			Promethean Xorg Virtual Device Creator
### END INIT INFO


RETVAL=0


case "$1" in
start)
	mkfifo /dev/input/activdevices
	mkdir /tmp/activboard
	chmod 777 /tmp/activboard

	echo -e "[\\033[03;32mChecking Promethean Kernel Module\\033[00m]"
	# -n = dry run: problem, it doesn't always return false
	# /sbin/modprobe -n promethean
	/sbin/modprobe promethean
	if [ $? -ne 0 ]; then
		pushd .
		cd /usr/src/promethean/kernel
		# Use b rather than make for livecd (gcc may not be installed)
		# also, requests an install first: the live cd may be built on a 64-bit machine.
		./b install
		popd
	fi

	if [ -f "/etc/X11/xorg.conf" ]; then
		grep -i promethean /etc/X11/xorg.conf >> /dev/null
		if [ $? -ne 0 ]; then
			python /usr/local/bin/modifyxorg.py
		fi
	fi

	for f in /dev/ACTIVBoard* 
	do
		if [ -e $f ]; then
			# when several devices are connected, the calibration doesn't always get loaded 
			# with udev.
			#send the calibration to the device
			/usr/local/bin/activlc --load $f
		fi
	done
	

	;;
stop)
	rm -f /dev/input/activdevices
	;;
*)
esac

exit $RETVAL

