#!/bin/sh

if [ $# -lt 1 ] ; then 
    echo "I need some options"
    echo "$0 [option]"
    echo "  -d <proofdir>  This is the master node, so we need a file name"
    echo "                 E.g. /usr/share/root/proof. This option will"
    echo "                 create the directory <proofdir>/log, and/or"
    echo "                 the file <proofdir>/log/proof.log if they "
    echo "                 don't exist"
    echo "  -m <master>    This is a slave node, se we need the master"
    echo "                 hostname. E.g. pmaster.cern.ch"
    echo "  -c             Clean entries in /etc/syslog.conf"
    echo " "
fi

case $1 in
    "-d")
	if [ "x$2" = "x" ] ; then 
	    echo "I need the PROOF config directory"
	    exit 1
	fi
	file="$2/log"
	echo "Master node configuration - logfile: $file/proof.log"
	#
	# Create log directory, if it doesn't exist already 
	#
	if [ ! -d $file ] ; then mkdir -p $file  ; fi
	#
	# Create empty log file if it doen't exist already 
	#
	file="$file/proof.log"
	if [ ! -f $file ] ; then echo "" > $file ; fi
	;;
    "-m") 
	if [ "x$2" = "x" ] ; then 
	    echo "I need the PROOF master hostname"
	    exit 1
	fi
	file="@$2"
	echo "Slave node configuration - master: $file"
	;;
    "-c") 
	echo "Cleaning configuration"
	clean="yes"
	;;
    "*")
	echo "I need one of the options -d, -m, -c"
	;;
esac

if [ "x$clean" = "xyes" ] ; then 
    syslog-facility remove local5
    syslog-facility remove local6
    exit 0
fi

while [ true ] ; do 
    localnum=`syslog-facility set 'debug' $file 'none' /var/log/messages`
    if [ "x$localnum" = "xlocal5" ] ; then 
	localnum=`syslog-facility set 'debug' $file 'none' /var/log/messages`
	if [ "x$localnum" != "xlocal6" ] ; then 
	    echo "Couldn't get local6 facility, trying to revert"
	    syslog-facility remove local5
	    if [ "x$localnum" != "none" ] ; then 
		syslog-facility remove $localnum
	    fi
        fi
	break  
    elif [ "x$localnum" = "xnone" ] ; then
	echo "Seems like all local facilities are tied up at the moment"
	echo "You need to manually edit the /etc/syslog.conf file"
	break
    fi
    numbers="$numbers $localnum"
done

for num in $numbers ; do 
    syslog-facility remove $num
done

