#!/bin/sh
# Init script to print color logs at any tty with any user
#
# Copyright(C) 2000, KARASZI Istvan, License under GPL version 2 or higher.
#
# Version 0.2 -- Mon Feb 26 23:47:05 CET 2001

PATH="/bin/:/sbin/:/usr/sbin/:/usr/bin/";
LOGFILES="/var/log/messages";
TTY="/dev/tty12";
USER="root"
NICE="19"
TAIL="/usr/bin/tail";
COLORIZE="/usr/local/bin/colorize.pl";
PIDFILE="/var/run/colorize.pid";

start () {
	su $USER -c "$TAIL -0f $LOGFILES | nice -$NICE $COLORIZE > $TTY 2> /dev/null &"
	PID=`pgrep -n -u $USER \`basename $TAIL\``;
	if [ "$PID" != "" ]; then
		echo $PID > $PIDFILE
		echo -n " started";
	else
		echo -n " failed";
	fi
}

stop () {
	if [ -e $PIDFILE ]; then
		PID=`cat $PIDFILE`;
		if [ "$PID" != "" ]; then
			kill "$PID" 2> /dev/null
			if [ $? -ne 0 ]; then
				echo " failed ($PID)";
				exit -1
			else
				rm $PIDFILE
				echo -n " stopped"
			fi
			
		else
			echo -n " failed";
		fi
	else
		echo;
	fi
}

case "$1" in
	start)
		echo -n "Starting log on $TTY... ";
		start
		echo ".";
		;;
	stop)
		echo -n "Stopping log on $TTY... ";
		stop
		echo ".";
		;;
	restart)
		echo -n "Restartin log on $TTY... ";
		stop
		start
		echo ".";
		;;
	*)
		echo "Usage $0 {start|stop|restart}";
		exit 1;
		;;
esac
