#!/bin/sh
##############################################################################
# $Id: stereoserver.init,v 1.2 2001/04/12 06:13:37 jheiss Exp $
##############################################################################
# Init script for NetStereo server
#
# chkconfig: 2345 90 10
# description: Server for NetStereo application
##############################################################################
# $Log: stereoserver.init,v $
# Revision 1.2  2001/04/12 06:13:37  jheiss
# Changed the log and pid file locations to standard system directories.
# Dropped the volume down, it was too high.
# Added code to make sure the server isn't already running.
# Added flag on java commandline to restrict the ports that RXTX
# iterates through.  This dramatically speeds up startup, and
# eliminates the errors that I was getting from the kernel when RXTX
# poked at devices that didn't exist.
#
##############################################################################

SU=true
USER=jheiss
APPDIR=/home/jheiss/netstereo/server/java
MUSICDIR=/music
LOGFILE=/var/log/stereoserver.log
PIDFILE=/var/run/stereoserver.pid
JAVA=/usr/java/jdk1.3/bin/java
SETVOLUME="aumix -v 50"
KILLMPG123=true

case "$1" in
	start)
		# Make sure the server isn't already running
		ps -e | awk '{print $1}' | grep -qs "^`cat $PIDFILE`\$"
		if [ "$?" = 0 ]
		then
			exit
		fi

		if [ -n "$SETVOLUME" ]
		then
			$SETVOLUME
		fi
		cd $MUSICDIR
		if [ x"$SU" = "xtrue" ]
		then
			su $USER -c "$JAVA -classpath $APPDIR -Dgnu.io.rxtx.SerialPorts=/dev/ttyS0:/dev/ttyS1:/dev/ttyS2:/dev/ttyS3 StereoServer" > $LOGFILE 2>&1 &
		else
			$JAVA -classpath $APPDIR StereoServer > $LOGFILE 2>&1 &
		fi
		pid=$!
		echo "$pid" > $PIDFILE
		;;
	stop)
		kill `cat $PIDFILE`
		if [ x"$KILLMPG123" = "xtrue" ]
		then
			mpg123pid=`ps -e | grep mpg123 | head -1 | awk '{print $1}'`
			if [ -n "$mpg123pid" ]
			then
				kill $mpg123pid
			fi
		fi
		;;
	restart)
		$0 stop
		$0 start
		;;
	*)
		echo "Usage: stereoserver {start|stop|restart}"
esac

