#! /bin/sh

# start dccm

# This script assumes that it is being run by root or the DCC user, probably
# while the system is starting.  It should be run before sendmail is started
# to avoid complaints from sendmail about the socket not existing.
# 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.47 $Revision$
#	Generated automatically from start-dccm.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:u:c:a:l:L:R:w:" c; do
    case $c in
	x) set -x;;
	h) DCC_HOMEDIR="$OPTARG";;
	*) ;;
    esac
done
. $DCC_HOMEDIR/dcc_conf
if test 0"$DCC_CONF_VERSION" -lt 2; then
    # deal with bash reserved $UID and old versions of dcc_conf
    if test -z "$DCCUID" -a ! -z "$UID"; then
	DCCUID="$UID"
    fi
    if test -z "$DCC_RUNDIR" -a ! -z "$DCCM_RUNDIR"; then
	DCC_RUNDIR="$DCCM_RUNDIR"
    fi
fi
DCCM_ARGS="$DCCM_ARGS $DCC_LOG_ARGS"

ARGV0=dccm
USAGE="`basename $0`: [-x] [-h homedir] [-r rundir] [-u UID] [-c argv0] [-a args]
	[-l logdir] [-L log-at] [-R reject_at] [-w whiteclnt]"
OPTIND=1
while getopts "xh:r:u:c:a:l:L:R:w:" c; do
    case $c in
	x) ;;
	h) ;;
	r) DCC_RUNDIR="$OPTARG";;
	u) DCCUID="$OPTARG";;
	c) ARGV0="$OPTARG"
	    if test -z "$ARGV0" \
		 -o X`expr "$ARGV0" : '\([-_a-zA-Z0-9]*\)` != X"$ARGV0"; then
		eval $DCC_LOGGER "illegal argv0"
		exit 1
	    fi;;
	a) DCCM_ARGS="$DCCM_ARGS $OPTARG";;
	l) DCCM_LOGDIR="$OPTARG";;
	L) DCCM_LOG_AT="$OPTARG";;
	R) DCCM_REJECT_AT="$OPTARG";;
	w) DCCM_WHITECLNT="$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

# stop an existing dccm
#   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/$ARGV0.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 dccm 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

# assume unset DCCD_ENABLE is "on" for upward compatibility
case "$DCCM_ENABLE" in
    [oO][fF][fF]) exit 0;;
esac
if test ! -x $DCC_LIBEXEC/dccm; then
    eval $DCC_LOGGER "cannot start dccm because it has not been installed"
    exit 1
fi

if test "$DCC_HOMEDIR" != /var/lib/dcc; then
    DCCM_ARGS="-h $DCC_HOMEDIR $DCCM_ARGS"
fi
if test ! -z "$DCC_RUNDIR" -a "$DCC_RUNDIR" != /var/run/dcc; then
    DCCM_ARGS="-R $DCC_RUNDIR $DCCM_ARGS"
fi
if test ! -z "$DCCM_LOGDIR"; then
    DCCM_ARGS="$DCCM_ARGS -l $DCCM_LOGDIR"
fi
if test ! -z "$DCCM_LOG_AT$DCCM_REJECT_AT"; then
    if test -z "$DCCM_CKSUMS"; then
	DCCM_CKSUMS="CMN"
    fi
    for CK in $DCCM_CKSUMS $DCCM_XTRA_CKSUMS; do
	DCCM_ARGS="$DCCM_ARGS -t $CK,$DCCM_LOG_AT,$DCCM_REJECT_AT"
    done
fi
if test ! -z "$DCCM_WHITECLNT"; then
    DCCM_ARGS="$DCCM_ARGS -w $DCCM_WHITECLNT"
fi
if test ! -z "$DCCM_USERDIRS"; then
    DCCM_ARGS="$DCCM_ARGS -U $DCCM_USERDIRS"
fi

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

if test "$ARGV0" != dccm -a ! -s $DCC_LIBEXEC/$ARGV0; then
    ln -s dccm $DCC_LIBEXEC/$ARGV0
fi

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