#! /bin/sh

# start dccifd

# This script assumes that it is being run by root or the DCC user, probably
# while the system is starting.  See the misc/rcDCC script.

# Copyright (c) 2003 by Rhyolite Software
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND RHYOLITE SOFTWARE DISCLAIMS ALL
# WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
# OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL RHYOLITE SOFTWARE
# BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
# OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
# WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
# ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
# SOFTWARE.
#	Rhyolite Software DCC 1.2.16-1.7 $Revision$
#	Generated automatically from start-dccifd.in by configure.

exec 1>&2 </dev/null

DCC_LOGGER="logger -s -p ${DCC_ERROR_LOG_FACILITY-mail.err} -t DCC"
DCC_HOMEDIR=/var/lib/dcc
# check the args once to get the home directory
while getopts "xh:r:a:" c; do
    case $c in
	x) set -x;;
	h) DCC_HOMEDIR="$OPTARG";;
	*) ;;
    esac
done
. $DCC_HOMEDIR/dcc_conf
DCCIFD_ARGS="$DCCIFD_ARGS $DCC_LOG_ARGS"

USAGE="`basename $0`: [-x] [-h homedir] [-r rundir] [-a args]"
OPTIND=1
while getopts "xh:r:a:" c; do
    case $c in
	x) ;;
	h) ;;
	r) DCC_RUNDIR="$OPTARG";;
	a) DCCIFD_ARGS="$DCCIFD_ARGS $OPTARG";;
	*) eval $DCC_LOGGER "$USAGE"; exit 1;;
    esac
done
shift `expr $OPTIND - 1 || true`
if test "$#" -ne 0; then
    eval $DCC_LOGGER "$USAGE"
    exit 1
fi

if test "$DCC_HOMEDIR" != /var/lib/dcc; then
    DCCIFD_ARGS="-h $DCC_HOMEDIR $DCCIFD_ARGS"
fi
if test ! -z "$DCC_RUNDIR" -a "$DCC_RUNDIR" != /var/run/dcc; then
    DCCIFD_ARGS="-R $DCC_RUNDIR $DCCIFD_ARGS"
fi
if test ! -z "$DCCIFD_LOGDIR"; then
    DCCIFD_ARGS="$DCCIFD_ARGS -l $DCCIFD_LOGDIR"
fi
if test ! -z "$DCCIFD_LOG_AT$DCCIFD_REJECT_AT"; then
    if test -z "$DCCIFD_CKSUMS"; then
	DCCIFD_CKSUMS="CMN"
    fi
    for CK in $DCCIFD_CKSUMS $DCCIFD_XTRA_CKSUMS; do
	DCCIFD_ARGS="$DCCIFD_ARGS -t $CK,$DCCIFD_LOG_AT,$DCCIFD_REJECT_AT"
    done
fi
if test ! -z "$DCCIFD_WHITECLNT"; then
    DCCIFD_ARGS="$DCCIFD_ARGS -w $DCCIFD_WHITECLNT"
fi
if test ! -z "$DCCIFD_USERDIRS"; then
    DCCIFD_ARGS="$DCCIFD_ARGS -U $DCCIFD_USERDIRS"
fi


# stop an existing dccifd
#   signal the process group because on Linux systems it does not work
#   to signal the main process.  See `man sigwait` on a Linux system.
PID=`cat $DCC_RUNDIR/dccifd.pid 2>/dev/null`
if test -n "$PID"; then
    if `kill -15 $PID 2>/dev/null`; then
	CNT=0
	while test $CNT -lt 6; do
	    if `/bin/kill -15 $PID 2>/dev/null`; then
		if test $CNT -eq 0; then
		    echo -n "    stopping dccifd PID $PID "
		else
		    echo -n "."
		fi
		sleep 1
		CNT=`expr $CNT + 1`
	    else
		break
	    fi
	done
	/bin/kill -9 $PID 2>/dev/null
	if test $CNT -ne 0; then
	    echo
	fi
    fi
fi


case "$DCCIFD_ENABLE" in
    [oO][nN]);;
    *) exit 0;;
esac
if test ! -x $DCC_LIBEXEC/dccifd; then
    eval $DCC_LOGGER "cannot start dccifd because it has not been installed"
    exit 0
fi

if test ! -d $DCC_RUNDIR; then
    mkdir $DCC_RUNDIR
fi

if test -z "$DCCUID" -o X"$DCCUID" = X"$USER"; then
    eval $DCC_LIBEXEC/dccifd $DCCIFD_ARGS
else
    if test -d $DCC_RUNDIR; then
	chown $DCCUID $DCC_RUNDIR
    fi
    su - $DCCUID -c "$DCC_LIBEXEC/dccifd $DCCIFD_ARGS"
fi
