#! /bin/sh
#
# patch inetd.conf and services
# originally by Axel Zinser (fifi@hiss.han.de)
#

prefix="/usr/local"
exec_prefix="${prefix}"
libexecdir="/usr/local/libexec/amanda"

USE_VERSION_SUFFIXES="no"
if test "$USE_VERSION_SUFFIXES" = "yes"; then
	SUF="-2.4.5"
else
	SUF=
fi

SERVICE_SUFFIX=""

USER="operator"

INETDCONF=/etc/inetd.conf
[ ! -f $INETDCONF ] && INETDCONF=/usr/etc/inetd.conf

SERVICES=/etc/services
[ ! -f $SERVICES ] && SERVICES=/usr/etc/services

ENABLE_AMANDAD=true

case `uname -n` in
"sparc64.ports.openbsd.org" | "sparc64.ports.openbsd.org".*)
    ENABLE_INDEX=true
    ENABLE_TAPE=true
    ;;
*)
    ENABLE_INDEX=false
    ENABLE_TAPE=false
    ;;
esac

CLIENT_PORT=10080
KCLIENT_PORT=10081
INDEX_PORT=10082
TAPE_PORT=10083

while [ $# != 0 ]; do
    case "$1" in
    --service-suffix=*)
	SERVICE_SUFFIX=`echo $1 | sed -e 's/[^=]*=//'`;;
    --version-suffix=*)
	SUF=`echo $1 | sed -e 's/[^=]*=//'`;;
    --inetd=*)
        INETDCONF=`echo $1 | sed -e 's/[^=]*=//' -e 's%^$%/dev/null%'`;;
    --services=*)
	SERVICES=`echo $1 | sed -e 's/[^=]*=//' -e 's%^$%/dev/null%'`;;
    --libexecdir=?*)
	libexecdir=`echo $1 | sed -e 's/[^=]*=//'`;;
    --user=?*)
	USER=`echo $1 | sed -e 's/[^=]*=//'`;;
    --enable-client)
	ENABLE_AMANDAD=true;;
    --disable-client)
	ENABLE_AMANDAD=false;;
    --enable-index)
	ENABLE_INDEX=true;;
    --disable-index)
	ENABLE_INDEX=false;;
    --enable-tape)
	ENABLE_TAPE=true;;
    --disable-tape)
	ENABLE_TAPE=false;;
    --client-port=?*)
	CLIENT_PORT=`echo $1 | sed -e 's/[^=]*=//'`;;
    --kclient-port=?*)
	KCLIENT_PORT=`echo $1 | sed -e 's/[^=]*=//'`;;
    --index-port=?*)
	INDEX_PORT=`echo $1 | sed -e 's/[^=]*=//'`;;
    --tape-port=?*)
	TAPE_PORT=`echo $1 | sed -e 's/[^=]*=//'`;;
    --usage | --help | -h)
	echo "call this script with zero or more of the following arguments:"
	echo "--version-suffix=<suffix>: append to program names [$SUF]"
	echo "--service-suffix=<suffix>: append to service names [$SERVICE_SUFFIX]"
	echo "--libexecdir=<dirname>: where daemons should be looked for [$libexecdir]"
	echo "--inetd=<pathname>: full pathname of inetd.conf [$INETDCONF]"
	echo "--services=<pathname>: full pathname of services [$SERVICES]"
	echo "    an empty pathname or /dev/null causes that file to be skipped"
	echo "--user=<username>: run deamons as this user [$USER]"
	echo "--enable/disable-client: enable/disable amandad [`$ENABLE_AMANDAD && echo enabled || echo disabled`]"
	echo "--enable/disable-index: enable/disable index server [`$ENABLE_INDEX && echo enabled || echo disabled`]"
	echo "--enable/disable-tape: enable/disable tape server [`$ENABLE_TAPE && echo enabled || echo disabled`]"
	echo "--client-port=<num>: amandad port number [$CLIENT_PORT]"
	echo "--kclient-port=<num>: kamandad port number [$KCLIENT_PORT]"
	echo "--index-port=<num>: index server port number [$INDEX_PORT]"
	echo "--tape-port=<num>: tape server port number [$TAPE_PORT]"
	exec true;;
    *)
	echo "$0: invalid argument $1.  run with -h for usage" >&2
	exec false;;
    esac
    shift
done

if [ "$SERVICES" = /dev/null ]; then :
elif [ -f "$SERVICES" ]; then
	TEMP="$SERVICES.new"
	{
	    egrep < "$SERVICES" -v "^(amanda|kamanda|amandaidx|amidxtape)${SERVICE_SUFFIX}[ 	]"
	    echo "amanda${SERVICE_SUFFIX} ${CLIENT_PORT}/udp"
	    echo "amanda${SERVICE_SUFFIX} ${CLIENT_PORT}/tcp"
	    echo "kamanda${SERVICE_SUFFIX} ${KCLIENT_PORT}/udp"
	    echo "amandaidx${SERVICE_SUFFIX} ${INDEX_PORT}/tcp"
	    echo "amidxtape${SERVICE_SUFFIX} ${TAPE_PORT}/tcp"
	} > "$TEMP"
	if diff "$SERVICES" "$TEMP" >/dev/null 2>/dev/null; then
		echo "$SERVICES is up to date"
	else
		cp "$TEMP" "$SERVICES" || echo "cannot patch $SERVICES"
	fi
	rm -f "$TEMP"
else
	echo "$SERVICES not found!"
fi
if [ "$INETDCONF" = /dev/null ]; then :
elif [ -f "$INETDCONF" ]; then
	$ENABLE_AMANDAD && test ! -f $libexecdir/amandad$SUF && echo "warning: $libexecdir/amandad$SUF does not exist" >&2
	$ENABLE_INDEX && test ! -f $libexecdir/amindexd$SUF && echo "warning: $libexecdir/amindexd$SUF does not exist" >&2
	$ENABLE_TAPE && test ! -f $libexecdir/amidxtaped$SUF && echo "warning: $libexecdir/amidxtaped$SUF does not exist" >&2
	TEMP="$INETDCONF.new"
	{
	    egrep < "$INETDCONF" -v "^(amanda|amandaidx|amidxtape)${SERVICE_SUFFIX}[ 	]"
	    $ENABLE_AMANDAD && echo "amanda${SERVICE_SUFFIX}    dgram  udp wait   $USER $libexecdir/amandad$SUF    amandad$SUF"
	    $ENABLE_INDEX && echo "amandaidx${SERVICE_SUFFIX} stream tcp nowait $USER $libexecdir/amindexd$SUF   amindexd$SUF"
	    $ENABLE_TAPE && echo "amidxtape${SERVICE_SUFFIX} stream tcp nowait $USER $libexecdir/amidxtaped$SUF amidxtaped$SUF"
	} > "$TEMP"
	if diff "$INETDCONF" "$TEMP" >/dev/null 2>/dev/null; then
		echo "$INETDCONF is up to date"
	else
		cp "$TEMP" "$INETDCONF" || echo "cannot patch $INETDCONF"
	fi
	rm -f "$TEMP"
else
	echo "$INETDCONF not found!"
fi
