:
#ident	"@(#)nlp:etc/lp	1.3"

##########
# If not invoked with at least one argument, assume we are
# running on a pre-SVR3.0 machine. We'll check our name to
# see what we do. Otherwise, the first argument tells what
# to do.
##########
if [ $# = 0 ]
then
	case $0 in
	*/lp-start )
		state=start
		;;
	*/lp-stop )
		state=stop
		;;
	esac
else
	state=$1
fi

set `who -r`

# if [ $8 != "0" ]
# then
#	 exit
# fi

case $state in
'start')
	if [ $9 = "2" -o $9 = "3" ]
	then
		exit
	fi
	rm -f /usr/spool/lp/SCHEDLOCK

	# We can get rid of any left over fifos automatically since there is no
	# way to check if they are okay.

	rm -f /usr/spool/lp/fifos/public/*
	rm -f /usr/spool/lp/fifos/private/*

	# List contents of requests directory, files in this directory are named
	# according to <request_id>-0. There is always a corresponding file in
	# /usr/spool/lp/temp with exactly the same name.

	cd /usr/spool/lp

	LIST=`cd requests; echo *`
	if [ "$LIST" != "*" ]
	then
		for i in $LIST
		do
			  if [ "$i" = "*" ]
			  then
				  break
			  fi

			  # Get the request id. I.e. subtract the -0 from the file name.

			  ID=`echo $i | sed -e "s/\([0-9]*\)-.*/\1/"`

			  # If the corresponding file in the temp directory
			  # doesn't exist then remove the copy in the requests directory.

			  if [ ! -f temp/$i ]
			  then
				  rm -f requests/$i
				  rm -f temp/$ID*
				  continue
			  fi

			  # Get the names of any data files that are stored
			  # in the temp directory. These are named <request_id>-1,
			  # <request_id>-2, ..., <request_id>-N.
			  # If the file isn't named as such then it won't be in the temp
			  # directory. This of course breaks down if the user has
			  # lp'd a file called <request_id>-1, for example, and
			  # didn't use the -c option to lp. However, this is pretty
			  # unlikely !

			  for f in `sed -n -e "/^[^F]/d" -e "/$ID/s/^F //p" temp/$i`
			  do
				  # If the temp data files don't exist then rm the
				  # two request files from the requests and temp
				  # directories.

				  if [ ! -f $f ]
				  then
					rm -f requests/$i
					rm -f temp/$ID*
					break
				  fi
			   done
		done
	fi

	# Now check to see if there is anything in the temp directory that
	# isn't in the requests directory.

	ID=`cd temp; ls | sed -e "s/\([0-9]*\)-.*/\1/" | sort -u`
	for i in $ID
	do
		if [ ! -f requests/$i-0 ]
		then
			rm -f temp/$i*
		fi
	done

	echo "Starting LP Scheduler"
	pids=`ps -e | /usr/bin/egrep "lpsched" | /usr/bin/cut -c1-6`
	if [ "$pids" != "" ]
	then
		echo "LP Scheduler is already running - kill old one and restart"
		/usr/lib/lpshut
		sleep 3
		/bin/kill -9 $pids > /dev/null 2>&1
	fi
	/usr/lib/lpsched

# lpd should only be started if TCP/IP is installed.

	if [ -c /dev/tcp ]
	then
		echo "Starting LPD Daemon"
		pids=`ps -e | /usr/bin/egrep "lpd" | /usr/bin/cut -c1-6`
		if [ "$pids" != "" ]
		then
			echo "LPD Daemon is already running - killing old one and restarting"
			/bin/kill -9 $pids > /dev/null 2>&1
		fi
		/usr/lib/lpd
	fi
	;;
'stop')
	echo "Stopping LP Scheduler"
	/usr/lib/lpshut
	sleep 3
	echo "Stopping LPD Daemon"
	pids=`ps -e | /usr/bin/egrep "lpd|lpsched" | /usr/bin/cut -c1-6`
	if [ "$pids" != "" ]
	then
		/bin/kill -9 $pids > /dev/null 2>&1
	fi
	;;
esac
