#! /bin/sh
# Copyright (c) 2004 SuSE Linux AG, Eschborn, Germany.
# All rights reserved.
#
# Author: Axel Schmidt
#
# SDCLD system startup script
#
# sdcld - Client daemon is part of the SWDIST project
#         (SoftwareWare DISTribution based an SUSE ALICE2)
#
### BEGIN INIT INFO
# Provides:          sdcld
# Required-Start:    $network 
# X-UnitedLinux-Should-Start: alice ypbind
# Required-Stop:
# Default-Start:     3 5
# Default-Stop:
### END INIT INFO

# Check for existence of needed config files
CLD_CONFIG=/etc/swdist/swdist.conf
test -r $CLD_CONFIG || {
   echo "SWDIST $CLD_CONFIG not existing"
   exit 6
}

# Check for missing binaries (stale symlinks should not happen)
CLD_BIN=/usr/sbin/sdcld
test -x $CLD_BIN || {
   echo "SWDIST $CLD_BIN not installed"
   exit 5
}

# check for alice sysconfig file
# get the ALICE_CONFIG_AGENT_KEY from it
# if it is there
ALICE_SYSCONFIG=/etc/sysconfig/alice
SDCLD_PARAMS=""
test -r $ALICE_SYSCONFIG && {
. $ALICE_SYSCONFIG
if [ "$ALICE_CONFIG_AGENT_KEY" != "" ] ; then
SDCLD_PARAMS="$SDCLD_PARAMS --key $ALICE_CONFIG_AGENT_KEY"
fi;
}

HOSTNAME=$( hostname -f  2> /dev/null )
if [ "$HOSTNAME" != "" ] ; then
SDCLD_PARAMS="$SDCLD_PARAMS --machine $HOSTNAME"
else
logger "SDCLD: DNS problem - can't resolve hostname"
fi;

if [ "$SDCLGUI_GROUP" == "" ] ; then
SDCLGUI_GROUP="users"
fi;

STATEFILE=/var/lib/swdist/STATE
test -x $STATEFILE || {
    touch $STATEFILE
    chown root:$SDCLGUI_GROUP $STATEFILE
    chmod 640 $STATEFILE
}

INSTFILE=/var/lib/swdist/INST
test -x $INSTFILE || {
    touch $INSTFILE
    chown root:$SDCLGUI_GROUP $INSTFILE
    chmod 660 $INSTFILE    
}

. /etc/rc.status
# Reset status of this service
rc_reset

case "$1" in
    start)
	echo -n "Starting sdcld "
	## Start daemon with startproc(8). If this fails
	## the return value is set appropriately by startproc.
	startproc $CLD_BIN $SDCLD_PARAMS

	# Remember status and be verbose
	rc_status -v
	;;
    stop)
	echo -n "Shutting down sdcld "
	## Stop daemon with killproc(8) and if this fails
	## killproc sets the return value according to LSB.

	killall $CLD_BIN

	# Remember status and be verbose
	rc_status -v
	;;
    restart)
	## Stop the service and regardless of whether it was
	## running or not, start it again.
	$0 stop
	$0 start

	# Remember status and be quiet
	rc_status
	;;
    trigger)
	## Signal the daemon to reload its config. Most daemons
	## do this on signal 1 (SIGHUP).
	## If it does not support it, restart.

	echo -n "Trigger Update Service of sdcld "
	killproc -SIGUSR1 $CLD_BIN
	rc_status -v
	;;
    status)
	echo -n "Checking for service sdcld "
	## Check status with checkproc(8), if process is running
	## checkproc will return with exit status 0.

	# Return value is slightly different for the status command:
	# 0 - service up and running
	# 1 - service dead, but /var/run/  pid  file exists
	# 2 - service dead, but /var/lock/ lock file exists
	# 3 - service not running (unused)
	# 4 - service status unknown :-(
	# 5--199 reserved (5--99 LSB, 100--149 distro, 150--199 appl.)

	# NOTE: checkproc returns LSB compliant status values.
	checkproc $CLD_BIN
	# NOTE: rc_status knows that we called this init script with
	# "status" option and adapts its messages accordingly.
	rc_status -v
	;;
    *)
	echo "Usage: $0 {start|stop|status|restart|trigger}"
	exit 1
	;;
esac
rc_exit
