#! /bin/sh
#
# lisa       Bring up/down lisa - lan browser for KDE
#
# chkconfig: 2345 30 17
# description: lan browser daemon for KDE

# Do not load RH compatibility interface.
WITHOUT_RC_COMPAT=1

# Source function library.
. /etc/init.d/functions

LOCKFILE=/var/lock/subsys/lisa
RETVAL=0
ARGS="-q"
LISA=/usr/sbin/lisa
CONFIG_FILE=/etc/lisarc

start()
{
	if ! [ -e $CONFIG_FILE ]; then
		printf "Generating config file for lisa, please run 'kdesu kcontrol' to customise"
		# See http://lisa-home.sourceforge.net/ for more details
		# on the config file format.
		# PingAddresses/AllowedAddresses should only be route 
		# entries that are not gateways,loopback or multicast:
		IPNMS_ALL=`/sbin/route -n |awk 'BEGIN {ORS=";"};$4=="U"&&$8!="lo"&&$1!~/224.0.0.0/ {print $1"/"$3}'`
		# BroadcastNetwork should be only the internal subnet,
		# take first route from above:
		IPNMS=`/sbin/route -n |awk ' $4=="U"&&$8!="lo"&&$1!~/224.0.0.0/ {print $1"/"$3}'|head -n1`
		echo "SecondWait=-1"> $CONFIG_FILE
		echo "SearchUsingNmblookup=1">> $CONFIG_FILE
		echo "DeliverUnnamedHosts=0" >>$CONFIG_FILE
		echo "FirstWait=30" >> $CONFIG_FILE
		echo "MaxPingsAtOnce=256" >>$CONFIG_FILE 
		echo "UpdatePeriod=300" >> $CONFIG_FILE
		echo "PingAddresses=$IPNMS_ALL">> $CONFIG_FILE 
		echo "AllowedAddresses=$IPNMS_ALL" >> $CONFIG_FILE
		echo "BroadcastNetwork=$IPNMS" >>$CONFIG_FILE
		echo "PingNames=" >> $CONFIG_FILE
		echo_passed
		echo
	fi

	msg_starting $"lan browser daemon for KDE"
	start_daemon --lockfile "$LOCKFILE" --expect-user root --no-announce -- $LISA $ARGS -c $CONFIG_FILE
	RETVAL=$?
	return $RETVAL
}

stop()
{
	msg_stopping $"lan browser daemon for KDE"
	stop_daemon --lockfile "$LOCKFILE" --expect-user root --no-announce -- $LISA
	RETVAL=$?
	return $RETVAL
}

restart()
{
	## If first returns OK call the second, if first or
	## second command fails, set echo return value.
	stop
	sleep 1
	start
}

case "$1" in
    start)
	start
	;;
    stop)
	stop
	;;
    restart)
	restart
	;;
    reload)
	stop
	start
	;;
    status)
	status --expect-user root -- $LISA
	RETVAL=$?
	;;
    condstart)
	if ! [ -f "$LOCKFILE" ]; then
	    start
	fi
	;;
    condstop)
	if [ -f "$LOCKFILE" ]; then
	    stop
	fi
	;;
    condrestart)
	if ! [ -f "$LOCKFILE" ]; then
	    restart
	fi
	;;
    *)
	msg_usage "${0##*/} {start|stop|status|restart|reload|condstart|condstop|condrestart}"
	RETVAL=1
	;;
esac
exit $RETVAL
