#!/bin/bash
#$Id: dinst,v 1.2 2004/04/01 13:08:42 chris Exp $

if [ $(uname -s) != "Linux" ]; then
    echo "Not running on Linux -- aborted!" 1>&2
    exit 1
fi

if [ "$EUID" != "0" ]; then
    echo "Must be run as root" 1>&2
    exit 1
fi

if [ $# -ne 1 ]; then
    echo "usage: $0 <srcdir>"  1>&2
    echo "       <srcdir> containing files to be installed"  1>&2
    echo "                (aksusbd, aksusbd.*)"  1>&2
    exit 1
fi

srcdir="$1"

# get default run level
runlevel=$(egrep "^id:.*:initdefault" /etc/inittab | cut -d: -f2)

#echo "default runlevel: $runlevel"

REQSTART=nothing
DSCRIPT=aksusbd
# determine Linux distribution (Suse, RedHat, Debian)
if [ -f /etc/debian_version ]; then
    SSCRIPT=aksusbd.debian
elif [ -f /etc/SuSE-release ]; then
    SSCRIPT=aksusbd.suse
elif [ -f /etc/redhat-release ]; then
    SSCRIPT=aksusbd.redhat
else
    SSCRIPT=aksusbd.gen
    DSCRIPT=aksusbd.gen
    echo
    echo "warning: unknown Linux distribution, using generic startup script"   1>&2
    echo
fi

# check availability of source files
if [ ! -f "${srcdir}/aksusbd" ]; then
    echo "file 'aksusbd' missing in '${srcdir}'"   1>&2
    exit 2
fi
if [ ! -f "${srcdir}/${SSCRIPT}" ]; then
    echo "file '${SSCRIPT}' missing in '${srcdir}'"   1>&2
    exit 2
fi

# determine where system startup scripts are located
# override by setting STARTUP_SCRIPTS environment variable
if [ -z "$STARTUP_SCRIPTS" ]; then
    # try some known places, use the first found
    if [ -e /etc/init.d ]; then
	STARTUP_SCRIPTS=/etc/init.d
    elif [ -e /etc/rc.d/init.d ]; then
	STARTUP_SCRIPTS=/etc/rc.d/init.d
    else
	echo "cannot determine location where startup scripts reside"   1>&2
	echo "please set STARTUP_SCRIPTS to this directory and try again"   1>&2
	exit 1
    fi
fi

#echo "startup scripts are located in $STARTUP_SCRIPTS"

# set destination for aksusbd executable
# override by setting AKSUSBDDESTDIR environment variable
if [ -z "$AKSUSBDDESTDIR" ]; then
    AKSUSBDDESTDIR=/usr/sbin       # default destination
fi

#echo "aksusbd installation directory $AKSUSBDDESTDIR"

# find runlevel specific startup directory
# override by setting RUNLEVELDIR environment variable
if [ -z "$RUNLEVELDIR" ]; then
    # try some known places, use the first found
    if [ -e /etc/rc${runlevel}.d ]; then
	RUNLEVELDIR=/etc/rc${runlevel}.d
    elif [ -e /etc/rc.d/rc${runlevel}.d ]; then
	RUNLEVELDIR=/etc/rc.d/rc${runlevel}.d
    else
	echo "cannot determine location for startup scripts for runlevel $runlevel"   1>&2
	echo "please set RUNLEVELDIR to the proper directory and try again"   1>&2
	exit 1
    fi
fi

#echo "runlevel $runlevel startup scripts in $RUNLEVELDIR"

# determine correct startup dependency for SuSE
if [ "$SSCRIPT" == aksusbd.suse ]; then
    SUSE_VERSION=$(cat /etc/SuSE-release | grep VERSION | cut -f3 -d\  )
    SUSE_VERSION_MAIN=$(echo $SUSE_VERSION | cut -d. -f1)
    SUSE_VERSION_SUB=$(echo $SUSE_VERSION | cut -d. -f2)

    if [ $SUSE_VERSION_MAIN -lt 8 ]; then
        # < 8
	REQSTART=hotplug\ autofs\ \$syslog
    elif [ $SUSE_VERSION_MAIN -eq 8 ]; then
	if [ $SUSE_VERSION_SUB -lt 1 ]; then
            # 8.0
	    REQSTART=hotplug\ autofs\ \$syslog
	else
            # >= 8.1
	    REQSTART=\$syslog\ \$local_fs
	fi
    else
        # > 8
	REQSTART=\$syslog\ \$local_fs
    fi
fi

echo "---------------------------------------"
echo "Copy AKSUSB daemon to $AKSUSBDDESTDIR ..."
install -c -m 555 -g root -o root "${srcdir}/aksusbd" $AKSUSBDDESTDIR
if [ $? -gt 0 ]; then exit $?; fi

tmpdir="${TMPDIR:=/tmp}"
tmpfile="$TMPDIR/aksusbdinst$$"

echo "Copy AKSUSB daemon startup script to $STARTUP_SCRIPTS"

cat ${srcdir}/${SSCRIPT} |
    sed -e "s~@@@AKSUSBDDESTDIR@@@~$AKSUSBDDESTDIR~"   \
	-e "s~@@@STARTUP_SCRIPTS@@@~$STARTUP_SCRIPTS~" \
	-e "s~@@@AKSUSBREQSTART@@@~$REQSTART~" \
    | (rm -f "$tmpfile"; cat - > "$tmpfile")

install -c -m 555 -g root -o root "$tmpfile" $STARTUP_SCRIPTS/$DSCRIPT
if [ $? -gt 0 ]; then exit $?; fi

rm "$tmpfile"

echo "Setting up to autostart AKSUSB daemon "
if [ -L $RUNLEVELDIR/S90$DSCRIPT ]; then
    echo "removing existing link $RUNLEVELDIR/S90$DSCRIPT"
    rm -f $RUNLEVELDIR/S90$DSCRIPT
fi
(cd $RUNLEVELDIR; ln -vs $STARTUP_SCRIPTS/$DSCRIPT S90$DSCRIPT)
if [ $? -gt 0 ]; then exit $?; fi

echo "Trying to start AKSUSB daemon ... "

dpid=$(ps axh | grep aksusbd | grep -v grep | sed 's/ *\(.*\)/\1/' |  cut -f1 -d\  | head -1)
if [ -n "$dpid" ]; then
    echo "Killing already running AKSUSB daemon..."
    kill $dpid
    sleep 2
fi


$STARTUP_SCRIPTS/$DSCRIPT start
echo Done

echo "---------------------------------------"
