#!/bin/sh

[ -e /etc/rc.d/PUPSTATE ] && . /etc/rc.d/PUPSTATE

#### extra options (note to self:  trash this and use a config file)
extra="-z 20";

#Make sure it doesn't start if it shouldn't
if [ ! "$NOSPLASH" = "yes" ]; then

	#Figure out where the actual logfile is..
	LOGFILE=/tmp/norm_boot.log
	BAKLOGFILE="${LOGFILE}.bak"
	if [ -L "$LOGFILE" ]; then
		WHATISIT=`readlink "$LOGFILE"`
		if [ "$WHATISIT" = "/initrd$LOGFILE/" ]; then
			LOGFILE="/initrd$LOGFILE"
			BAKLOGFILE="/initrd$BAKLOGFILE"
		fi	
	fi


	#Start the daemon
	if [ "$1" = "start" ]; then
		#make sure it's not a link or anything
		if [ -L "$LOGFILE" ]; then
			rm "$LOGFILE"
			cp "$BAKLOGFILE" "$LOGFILE"
		fi
		clear > /dev/console
		pebble-daemon null "$extra"
	fi
	#Pause the daemon (leave the input-hiding alone)
	if [ "$1" = "pause" ]; then
		echo "q" > /tmp/pebble-pipe &
		clear > /dev/console
		cat /tmp/norm_boot.log > /dev/console
	fi
	#Stop the daemon (halt the input-hiding)
	if [ "$1" = "stop" ]; then
		#back up the logfile, then link it to /dev/console to restore output
		#if [ ! -L "$LOGFILE" ]; then
			echo "shut 'er down"
			echo "q" > /tmp/pebble-pipe &
			clear > /dev/console
			cat /tmp/norm_boot.log > /dev/console
			rm "$BAKLOGFILE" 2>/dev/null
			cp "$LOGFILE" "$BAKLOGFILE"
			rm "$LOGFILE"
			ln -s /dev/console "$LOGFILE"
		#fi
	fi
	#Refresh the daemon
	if [ "$1" = "restart" ]; then
		[ ! -L "$LOGFILE" ] && echo "r" > /tmp/pebble-pipe &
	fi

fi