#!/bin/sh
#
# Script for controlling CDWserver
#
# chkconfig: 345 90 10
# description: CDWserver is part of webCDwriter. \
#              webCDwriter makes your local CD-writer available \
#              to the users in your network.
# processname: CDWserver
# pidfile: /var/run/CDWserver.pid
# config: /etc/CDWserver/config

# directories
ALLDIRS=insert

# the path to your PID file
export pidFile=/var/run/CDWserver.pid

# make "setuid root copies" of cdrdao, cdrecord, mkisofs and readcd
for tool in cdrdao cdrecord mkisofs readcd
do
	if [ ! -e $TOOLSDIR/$tool ]; then
		if [ -e /usr/bin/$tool ]; then
			cp -pf /usr/bin/$tool $TOOLSDIR/ || :
		elif [ -e /usr/local/bin/$tool ]; then
			cp -pf /usr/local/bin/$tool $TOOLSDIR/ || :
		fi
		if [ -e $TOOLSDIR/$tool ]; then
			chown root $TOOLSDIR/$tool 2> /dev/null \
				&& chgrp cdwserve $TOOLSDIR/$tool 2> /dev/null \
				&& chmod 4750 $TOOLSDIR/$tool || :
		fi
	fi
done

# See how we were called.
case "$1" in
	start)
		echo -n "Starting CDWserver: "
		if [ -f $pidFile ] \
				&& kill -0 `cat $pidFile` 2> /dev/null; then
			echo "already running"
		else
			if $SBINDIR/CDWserver ; then
				echo "done"
			else
				echo "could not be started"
			fi
		fi
		if [ -d /var/lock/subsys ]; then
			touch /var/lock/subsys/CDWserver
		fi
		;;
	stop)
		echo -n "Stopping CDWserver: "
		if [ -f $pidFile ] \
				&& kill -TERM `cat $pidFile` 2> /dev/null; then
			rm -f $pidFile
			echo "done"
		else
			echo "no running CDWserver found"
		fi
		rm -f /var/lock/subsys/CDWserver
		;;
	restart)
		$0 stop
		$0 start
		;;
	condrestart)
		if [ -e /var/lock/subsys/CDWserver ]; then
			$0 restart
			exit $?
		else
			exit 0
		fi
		;;
	status)
		lynx -dump http://localhost:12411
		;;
	*)
		echo "Usage: $0 {start|stop|restart|status}"
		exit 1
esac

exit 0
