#!/bin/sh
#$Id: dinst,v 1.4 2004/06/08 14:55:10 chris Exp $

if [ $(uname -s) != "Linux" ]; then
    echo "Not running on Linux -- aborted!"
    exit 1
fi
if [ "$EUID" != "0" ]; then
    echo "Must be run as root"
    exit 1
fi

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

srcdir="$1"

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

#echo "default runlevel: $runlevel"

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

# check availability of source files
if [ ! -f "${srcdir}/hasplm" ]; then
    echo "file 'hasplm' 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"
	echo "please set STARTUP_SCRIPTS to this directory and try again"
	exit 1
    fi
fi

#echo "startup scripts are located in $STARTUP_SCRIPTS"

# set destination for hasplm executable
if [ -z "$HASPLMDESTDIR" ]; then
    HASPLMDESTDIR=/usr/sbin       # default destination
fi

#echo "hasplm installation directory $HASPLMDESTDIR"

# find runlevel specific startup directory
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"
	echo "please set RUNLEVELDIR to the proper directory and try again"
	exit 1
    fi
fi

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

echo "---------------------------------------"
echo "Copy HASP LM to $HASPLMDESTDIR ..."
install -c -m 555 -g root -o root "${srcdir}/hasplm" $HASPLMDESTDIR
if [ $? -gt 0 ]; then exit $?; fi

tmpdir=${TMPDIR:=/tmp}
tmpfile=$TMPDIR/hasplminst$$

echo "Copy HASP LM startup script to $STARTUP_SCRIPTS"

cat "${srcdir}/$SSCRIPT" |
    sed -e "s~@@@HASPLMDESTDIR@@@~$HASPLMDESTDIR~" \
        -e "s~@@@STARTUP_SCRIPTS@@@~$STARTUP_SCRIPTS~" \
    | (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 HASP LM"
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 HASP LM ... "

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


$STARTUP_SCRIPTS/$DSCRIPT start
echo Done

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