#!/bin/sh
# 
# lcdctl - Control script for lcdproc
#
# The exit codes returned are:
#	0 - operation completed successfully
#	1 - 
#	2 - usage error
#	3 - lcdproc could not be started
#	4 - lcdproc could not be stopped
#       5 - lcdproc screen could not be frozen
#       6 - lcdproc screen could not be unfrozen
#	7 - configuration error
#
# When multiple arguments are given, only the error from the _last_
# one is reported.  Run "lcdctl help" for usage info
#
#
# |||||||||||||||||||| START CONFIGURATION SECTION  ||||||||||||||||||||
# --------------------                              --------------------
prefix=/usr/local
exec_prefix=${prefix}
bindir=${exec_prefix}/bin
sysconfdir=${prefix}/etc
# 
# the path to your PID file
PIDFILE=/var/run/lcdproc.pid
#
# the path to your lcdproc binary, including options if necessary
LCDPROC=${exec_prefix}/bin/lcdproc
# 
# the path to your lcdproc config file
CONFIGFILE=${prefix}/etc/lcdproc.cf
if [ -f $CONFIGFILE ]; then
	CONFIG=`cat $CONFIGFILE`
else 
	echo "$0: Couldn't find lcdproc config file ... exiting"
	ERROR=7
	exit $ERROR
fi
#
# --------------------                              --------------------
# ||||||||||||||||||||   END CONFIGURATION SECTION  ||||||||||||||||||||

ERROR=0
ARGV="$@"
if [ "x$ARGV" = "x" ] ; then 
    ARGS="help"
fi

for ARG in $@ $ARGS
do
    case $ARG in
    start)
	if $LCDPROC $CONFIG ; then
	    echo "$0 $ARG: lcdproc started"
	else
	    echo "$0 $ARG: lcdproc could not be started"
	    ERROR=3
	fi
	;;
    stop)
	if killall lcdproc ; then
	    echo "$0 $ARG: lcdproc stopped"
	else
	    echo "$0 $ARG: lcdproc could not be stopped"
	    ERROR=4
	fi
	;;
    freeze)
	if killall -USR1 lcdproc ; then
	    echo "$0 $ARG: lcdproc display frozen"
	else
	    echo "$0 $ARG: lcdproc display could not be frozen"
	    ERROR=5
	fi
	;;
    unfreeze)
	if killall -USR2 lcdproc ; then
	    echo "$0 $ARG: lcdproc display unfrozen"
	else
	    echo "$0 $ARG: lcdproc display could not be unfrozen"
	    ERROR=6
	fi
	;;
    *)
	echo "usage: $0 (start|stop|freeze|unfreeze|help)"
	cat <<EOF

start      - start lcdproc
stop       - stop lcdproc
freeze 	   - freeze the current lcd screen
unfreeze   - unfreeze the current lcd screen
help       - this screen

EOF
	ERROR=2
    ;;

    esac

done

exit $ERROR

