#!/bin/sh 

SPMD=spmd
SPMDIR=/var/spm
SPMBOOTSCR=/etc/init.d/spm
SPMSIG="[\ \/]${SPMD}$"
TMP=/tmp
INSTFROM=/var/spm
USERID=`id | awk 'BEGIN{ FS="[=(]" } { print $2}'`
PROG=`basename $0`
SMP=`uname -v | awk '{print $2}'`
KERN=`uname -r | awk -F. '{print $2}'`
PS="ps -e"
UNAME=uname
VERBOSE=1
if [ -e /etc/redhat-release ] ; then
	RHVER=`grep 7.3 /etc/redhat-release | cut -d ' ' -f 5`
fi
[ x$SMP = 'xSMP' ] && smp='.smp'
[ x$KERN = 'x4' ] && kern='.24'
[ x$RHVER = 'x7.3' ] && kern='.rh73' && smp=''

verbose()
{
	[[ ${VERBOSE} = 1 ]] && echo "    $1"
}

usage()
{
	echo "usage: install_spmd -i|u|s|t[h]"
	echo "	-h help"
	echo "	-i install SPM daemon"
	echo "	-u uninstall SPM daemon"
	echo "	-s start SPM daemon"
	echo "	-t stop SPM daemon"
	exit 0
}

killproc() {            # kill the named process(es)
        pid=`ps -e |
             grep "$1" | grep -v grep | 
             sed -e 's/^  *//' -e 's/ .*//'`
        if [ "$pid" != "" ] ; then
		kill $signal $pid
	fi
}

# utility functions check_errstat
check_errstat()
{
if [ $errcode != 0 ];then
    echo "!!! FATAL ERROR: $message :$errcode"
	echo 
        exit  $errcode
fi
errcode=0
}

inst_rh() {

	[ -f /etc/init.d/spm ] && {
		verbose "SPM bootscripts already updated."
		return
	}
	
	cp ${INSTFROM}/rcspm /etc/init.d/spm 2>/dev/null
	errcode=$?;message="cannot copy spm file to /etc/init.d"
	check_errstat	
	chmod 750 /etc/init.d/spm 2>/dev/null
	ln -s /etc/init.d/spm /etc/rc0.d/K33spm 2>/dev/null
	errcode=$?;message="cannot link spm to rc0.d";check_errstat
	ln -s /etc/init.d/spm /etc/rc3.d/K33spm 2>/dev/null
	errcode=$?;message="cannot link spm to rc3.d";check_errstat
	ln -s /etc/init.d/spm /etc/rc5.d/K33spm 2>/dev/null
	errcode=$?;message="cannot link spm to rc5.d";check_errstat
	ln -s /etc/init.d/spm /etc/rc3.d/S91spm 2>/dev/null
	errcode=$?;message="cannot link spm to rc3.d";check_errstat
	ln -s /etc/init.d/spm /etc/rc5.d/S91spm 2>/dev/null
	errcode=$?;message="cannot link spm to rc5.d";check_errstat
	verbose "SPM bootscripts added to /etc/init.d"
}

inst_suse() {
	[ -f /etc/init.d/spm ] && {
		verbose "SPM bootscripts already updated."
		return
	}
	
	cp ${INSTFROM}/rcspm /etc/init.d/spm 2>/dev/null
	errcode=$?;message="cannot copy spm file to /etc/init.d"
	check_errstat	
	chmod 750 /etc/init.d/spm 2>/dev/null
	ln -s /etc/init.d/spm /etc/rc.d/rc0.d/K33spm 2>/dev/null
	errcode=$?;message="cannot link spm to rc0.d";check_errstat
	ln -s /etc/init.d/spm /etc/rc.d/rc3.d/K33spm 2>/dev/null
	errcode=$?;message="cannot link spm to rc3.d";check_errstat
	ln -s /etc/init.d/spm /etc/rc.d/rc5.d/K33spm 2>/dev/null
	errcode=$?;message="cannot link spm to rc5.d";check_errstat
	ln -s /etc/init.d/spm /etc/rc.d/rc3.d/S91spm 2>/dev/null
	errcode=$?;message="cannot link spm to rc3.d";check_errstat
	ln -s /etc/init.d/spm /etc/rc.d/rc5.d/S91spm 2>/dev/null
	errcode=$?;message="cannot link spm to rc5.d";check_errstat
	verbose "SPM bootscripts added to /etc/init.d"
}

inst_spmd() {
	if [ -f /etc/redhat-release ] ; then
		inst_rh
	elif [ -f /etc/SuSE-release ] ; then
		inst_suse
	fi
}

start_spmd()
{
	${PS} | grep "${SPMSIG}" 1>/dev/null 2>&1
	if [ $? = 0 ] ; then 
		verbose "SPM License Server is already running."
		exit 1;
	else
		if [ -x ${SPMBOOTSCR} ] ; then
			${SPMBOOTSCR} start
		else
			lsmod | grep spmkmod${smp}${kern} 1>/dev/null 2>&1
			[ $? = 1 ]  && {
				echo "!!! SPM kernel module (spmkmod) could not load."
				exit -1 
				}
			if test -x ${SPMDIR}/${SPMD}; then
				${SPMDIR}/${SPMD}
				[ $? = 0 ] && verbose "SPM daemon started."
			fi
			errcode=$?; message="cannot start the SPM License Server";check_errstat
		fi
	fi
}

stop_spmd()
{
	${PS} | grep "${SPMSIG}" 1>/dev/null 2>&1
	if [ $? != 0 ] ; then 
		verbose "SPM License Server is not running."
		exit 1;
	else
		if [ -x ${SPMBOOTSCR} ] ; then
			${SPMBOOTSCR} stop  
		else 
			signal='-9'
			killproc "${SPMSIG}"
			errcode=$?;message="Could not stop ${SPMD}";check_errstat
			verbose "SPM daemon stopped."
			# try to remove the kernel module
			#rmmod spmkmod${smp}${kern} 1>/dev/null 2>&1
			sleep 5
		fi
	fi
}




	
uninst_spmd_rh()
{
	if [ -x ${SPMBOOTSCR} ] ; then
		verbose "Uninstalling SPM bootscripts"
		rm -f /etc/init.d/spm 
		rm -f /etc/rc0.d/K33spm 
		rm -f /etc/rc2.d/S91spm
		rm -f /etc/rc5.d/S91spm
		rm -f /etc/rc5.d/K33spm
		rm -f /etc/rc3.d/S91spm
		rm -f /etc/rc3.d/K33spm
	else 
		verbose "SPM Bootscripts not installed."
		return
	fi
}

uninst_spmd_suse()
{
	if [ -x ${SPMBOOTSCR} ] ; then
		verbose "Uninstalling SPM bootscripts"
		rm -f /etc/init.d/spm 
		rm -f /etc/rc.d/rc0.d/K33spm 
		rm -f /etc/rc.d/rc2.d/S91spm
		rm -f /etc/rc.d/rc5.d/S91spm
		rm -f /etc/rc.d/rc5.d/K33spm
		rm -f /etc/rc.d/rc3.d/S91spm
		rm -f /etc/rc.d/rc3.d/K33spm
	else 
		verbose "SPM Bootscripts not installed."
		return
	fi
}

uninst_spmd()
{
	if [ -f /etc/redhat-release ] ; then
		uninst_spmd_rh
	elif [ -f /etc/SuSE-release ] ; then
		uninst_spmd_suse
	fi
}

if test "$USERID" != "0" ; then
	echo "!!! $PROG must be run with root priveleges";
	exit ;
fi

# parse command line
set -- `getopt iustqh $@`
[ $? != 0 ] && usage

while :
do 
	case $1 in 
		-i) inst_spmd;;
		-u) uninst_spmd;;
		-s) start_spmd;;
		-t) stop_spmd;;
		-h) usage;;
		-q) VERBOSE=0
			break;;
        --) break;;
	esac
	shift
done

