#!/bin/sh

killproc() {            # kill the named process(es)
        pid=`/bin/ps -e x |
             /bin/grep $1 |
             /bin/grep -v grep |
             /bin/sed -e 's/^  *//' -e 's/ .*//'`
        [ "$pid" != "" ] && kill $pid
}

. /etc/zaurusd/config

case "$1" in
    start)
    	echo "Starting Zaurusd"

	if [ "x$HAVE_ALSA_MIXER" != "x" ]; then
	    /usr/sbin/alsactl restore
	fi

    	if [ "x$HAVE_INPUT_SWITCHES" != "x" -a -x /usr/bin/switchevd ]; then
    	    /usr/bin/switchevd $SWITCH_EVENT_DEVICE &
    	fi

    	if [ "x$HAVE_TSKEYS" != "x" -a -x /usr/bin/tskeys ]; then
    	    modprobe uinput	
            if [ -x /usr/bin/setleds ]; then
	        /usr/bin/setleds +num < /dev/tty0
	    fi
	    if [ -e /etc/profile.d/tslib.sh ]; then
	        . /etc/profile.d/tslib.sh
	    fi
    	    if [ -e /etc/pointercal ]; then
    	    	/usr/bin/tskeys $TSKEYS_CONF $TSKEYS_DEVICE &
    	    fi
    	fi
    ;;

    stop)
        echo "Stopping Zaurusd"
	if [ -x /usr/bin/switchevd ]; then
            killproc /usr/bin/switchevd
	fi
	if [ -x /usr/bin/tskeys ]; then
            killproc /usr/bin/tskeys
	fi
	if [ "x$HAVE_ALSA_MIXER" != "x" ]; then
	    /usr/sbin/alsactl store
	fi
    ;;

    restart)
    	$0 stop
    	sleep 1
    	$0 start
    ;;

    *)
        echo "usage: $0 { start | stop }"
    	exit 1
    ;;
esac

exit 0
