#!/bin/sh
############################################################################### # Copyright 1997-2000 by mental images GmbH & Co.KG, Fasanenstr. 81, D-10623
# Berlin, Germany. All rights reserved.
#
# installation of software protection software SPM
#
# $Id: mi_install_spm,v 1.5 2000/06/19 14:19:19 runa Exp $
#
# functions:
#
#	check_errstat
#	root_check
#	rexit_stat
#	find_spm_host
#	find_flex_server
#	get_network_information
#	add_spm_port
# 	set_spmhost
# 	install_spmd
#	start_spmd
#
###############################################################################

usage()
{
echo "usage:  mi_install_spm [ -sduh ]"
echo "	-h help"
echo "	-s silent installation"
echo "	-d default installation"
echo "	-u deinstallation of SPM"
exit 0
}


# parsing command line options
set -- `getopt sdhu $@`
[ $? != 0 ] && usage

while :
do 
	case $1 in 
        -s) miSilent=1; miOptions="$miOptions -s";;
        -d) miCustom=0; miOptions="$miOptions -d";;
	-u) miDeinst=1; miOptions="$miOptions -u";;
	-h) usage;;
	-r) ;;
        --) break;;
	esac
	shift
done

ROOT=$TOP
[ x$TOP = x ] && ROOT=`dirname $0`/..
miProgname="$0"
TMP=${TMP:=/tmp/mi}
if [ $miProgname = ${TMP}/remote_install_spm ] ; then
	INSTFROM=$TMP
else
	INSTFORM=${INSTFROM:=${ROOT}/install}
fi
miExt=`${INSTFROM}/mi_checkos`
[ $miExt = none ] &&
	{
	echo "This platform ist not supported to run SPM software"
	exit
	}
SPMDIR=${SPMDIR:=/var/spm}
SPMFROM=${SPMFROM:=${ROOT}/${miExt}/spm}
miCustom=${miCustom:=1}
miSilent=${miSilent:=0}
miDeinst=${miDeinst:=0}
FROM=$SPMFROM
OS=`uname`
HOST=`hostname`
miSpmHost=none
miRelay=0
miName=SPM
miDNS=0
miNIS=0
miNISmaster=$HOST
miNISservice=0
miNIShosts=0
miDNSserver=$HOST
miServiceFile=/etc/services
miFlexServer=none
miFlexPort=744
miFlexFile=none
errcode=0
miConfig=/tmp/mi-config

# set system dependent system calls
[ $miProgname = ${TMP}/remote_install_spm ] && {
	FROM=$TMP
	INSTFROM=$TMP
	SPMFROM=$TMP
	}
PATH=/bin
[ $OS = 'OSF1' ] && PATH=$PATH:/sbin
[ $OS = 'SunOS' ] && PATH=$PATH:/usr/ucb
[ $OS = 'HP-UX' ] && PATH=$PATH:/usr/bin:/usr/sbin
[ $OS = 'IRIX' -o $OS = 'IRIX64' ] && PATH=$PATH:/usr/sbin:/usr/bsd
[ $OS = 'Linux' ] && PATH=$PATH:/usr/bin
UNAME=uname
RSH=rsh
[ -f /usr/bin/remsh ] && RSH=/usr/bin/remsh
[ -f /bin/remsh ] && RSH=/bin/remsh
PS='ps -ef'
[ $OS = 'SunOS' -a "`$UNAME -r | awk -F. '{print $1}'`" = 4 ] && PS='ps uax'
[ $OS = 'Linux' ] && PS='ps uax'

# utility functions check_errstat
check_errstat()
{
if [ $errcode != 0 ];then
	rm -f ${TMP}/OK 
        echo "FATAL ERROR: $message"
	echo "installation procedure aborted"
	echo 
        exit  $errcode
fi
errcode=0
}

# check remote exit status
rexit_stat()
{
OK=`$RSH $miSpmHost ls ${TMP}/OK 2>/dev/null`
$RSH $miSpmHost rm -rf $TMP 
[ x$OK = x ] &&  exit 1
}

# most things does not work if you are not superuser
root_check()
{
if [ `whoami` != 'root' ]; then
	errcode=1
	message="Sorry, you must be root to install SPM"
	check_errstat
fi
}


#******************************************************************************
# determination of the FLEXlm server
find_flex_server()
{
[ $miDeinst = 1 ] && return

[ $miSilent = 0 ] &&  {
	echo
	echo "... looking for the FLEXlm server ..."
	echo 
	}

[ x$SI_LOCATION = x ] && SI_LOCATION=/usr/softimage
[ -f $SI_LOCATION/flexlm/licenses/client.dat ] && 
	miFlexFile=$SI_LOCATION/$miFlexFile
[ -f /usr/local/flexlm/licenses/license.dat ] && 
	miFlexFile=/usr/local/flexlm/licenses/license.dat
[ x$LM_LICENSE_FILE != x ] &&
	[ -f $LM_LICENSE_FILE ] && miFlexFile=$LM_LICENSE_FILE

miFlexServer=`(grep SERVER $miFlexFile | awk '{print $2}') 2>/dev/null`
[ x$miFlexServer = x ] && miFlexServer=$HOST
miFlexPort=`(grep SERVER $miFlexFile | awk '{print $4}') 2>/dev/null`
grep SOFTIMAGE $miFlexFile 1>/dev/null 2>&1
[ x$miFlexPort = x ] && miFlexPort=744
if [ $miCustom = 1 ]; then
	echo "Which host is running the FLEXlm (lmgrd) daemon [ $miFlexServer ]?"
	read tmp
	miFlexServer=${tmp:-$miFlexServer}
	echo "Which port is used by FLEXlm [ $miFlexPort ] ?"
	read tmp
	miFlexPort=${tmp:-$miFlexPort}
fi	
[ $miSilent = 0 ] &&
	echo "... using ${miFlexServer}@${miFlexPort} for FLEXlm licenses ..."
}

#****************************************************************************** 
# determination of the SPM server
# default SPM host is localhost
# in customize mode it is possible to install on another host
find_spm_host()
{

if [ $miSilent = 0 ]; then 
	[ $miDeinst = 0 ] && {
		echo 
		echo "*** start installation of licensing software ***"
		echo
	}
	[ $miDeinst = 1 ] && {
		echo 
		echo "*** deinstalling licensing software ***"
		echo
	}
fi


[ $miRelay = 1 ] && find_flex_server

[ $miSilent = 0 -a $miDeinst = 0 ] && {
	echo
	echo "... installation of the License Server ..."
	}

miSpmHost=$HOST
miRayMaster=`grep miRayMaster $miConfig | awk '{print $2}'` 2>/dev/null
if [ x$miRayMaster != x  -a x$miRayMaster != x$HOST ] ;then
	ospmhost=`rsh $miRayMaster grep miSpmhost $miConfig | awk '{print $2}' 2>/dev/null`
	miSpmHost=${ospmhost:-$miSpmHost}
else
	ospmhost=`grep miSpmhost $miConfig | awk '{print $2}' 2>/dev/null`
	miSpmHost=${ospmhost:-$miSpmHost}
fi

if [ $miCustom = 1 -a x$ospmhost = x ];then
	echo
	echo "Which host shall be your License Server [$miSpmHost]?"
	read tmp
	miSpmHost=${tmp:-$miSpmHost}
        if [ x$ospmhost != x ] ;then
                sed -e "/miSpmhost /s/$ospmhost/$miSpmHost/" < $miConfig > $miConfig.tmp
                mv $miConfig.tmp $miConfig 1>/dev/null 2>&1
        else
                echo "miSpmhost $miSpmHost" >> $miConfig 2>/dev/null
        fi
else
	[ $miDeinst = 0 -a $miSilent = 0 ]
		echo "... License Server will be installed on $miSpmHost ..."
fi


[ $miDeinst = 1 ] && return

if [ $miSpmHost != $HOST ]; then
	OS=`$RSH $miSpmHost $UNAME 2>/dev/null`
	errcode=$?;message="cannot login on $miSpmHost as root";check_errstat
	PS='ps -ef'
	[ $OS = 'SunOS' ] && PS='ps uax'
	[ $OS = 'Linux' ] && PS='ps uax'
	PS="$RSH $miSpmHost $PS"
fi

$PS | grep -v grep | grep spmd 1>/dev/null 2>&1
if [ $? = 0 ]; then
	[ $miSilent = 0 ] && 
		echo "... there is already a License Server running ..."
	
	if [ $miCustom = 1 ];then
		echo
		echo "Kill License Server and update ?[y]"
		read tmp
                [ ${tmp:-y} = n ] && return
	fi
	[ $miSilent = 0 ] && 
		echo "... OK, trying to install License Server on $miSpmHost ..."
	return
fi

}

#*******************************************************************************
# hosts table and services is distributed by NIS, DNS or administered locally
# who is the NIS master DNS server
get_network_information()
{

[ -f /etc/resolv.conf ] &&  miDNS=1
$PS | grep ypbind | grep -v grep 1>/dev/null 2>&1
[ $? = 0 ] &&  miNIS=1

if [ $miNIS = 1 ] ;then
	miNISmaster=`ypwhich -m passwd 2>/dev/null`
	ypcat services 2>&1 | grep 'no such map'
	[ $? != 0 ] && miNISservice=1
	ypcat hosts 2>&1 | grep 'no such map'
	[ $? != 0 ] && miNIShosts=1
fi

if [ $miDNS = 1 ]; then
	miDNSserver=`grep nameserver /etc/resolv.conf | awk '{print $2}'`
fi

	

if [ $miNIS = 0 -a $miDNS = 0 ] ; then
	[ $miSilent = 0 ] && 
	echo "... local files /etc/services and /etc/hosts will be changed ..."
fi
}


#*******************************************************************************
# add spm prot mi-spm to /etc/services
# check if the port number 7050 is already in use
# check for existing ports of mi-spm 
add_spm_port()
{
SpmPortNumber=7050
SpmPort=mi-spm
	
if [ $miDeinst = 1 ]; then
	[ -f ${miServiceFile}.orig ] && {
		[ $miSilent = 0 ] && 
			echo "... restoring $miServiceFile ..."
		mv ${miServiceFile}.orig ${miServiceFile} 1>/dev/null 2>&1
	}
	[ $miNISservice = 1 -a $miSilent = 0 ] && {
		echo "... ${miNISmaster}'s $miServiceFile has to be changed ..."
		echo "Delete $SpmPort from $miNISmaster's $miServiceFile!"
		}
	return
fi

[ $miSilent = 0 ] && {
	echo
	echo "... License Server needs port $SpmPort to communicate ..."
	echo "... default port number is $SpmPortNumber ..."
	}

if [ $miNISservice = 1 -a $miNISmaster != $HOST ];then
	[ $miSilent = 0 ] &&
		echo "... getting services via NIS from $miNISmaster ..."
        `ypcat services | grep $SpmPort 1>/dev/null 2>&1`
        [ $? = 0 ] && {
                [ $miSilent = 0 ] && {
                        echo "... the spm port is already installed ..."
			echo "... nothing to be done ..."
			}
                return
                }

	[ $miSilent = 0 ] && {
		echo "... ${miNISmaster}'s $miServiceFile has to be changed ..."
		echo "... without this entry the License Server won't start ..."		
		echo "NOTICE: Add to $miNISmaster's $miServiceFile:"
		echo
		echo "$SpmPort	$SpmPortNumber/tcp # SW protection"
		echo
		}

	if [ $miCustom = 1 ] ; then
		echo
		echo "Waiting until you added $SpmPort [y]?"
		read tmp
		[ ${tmp:-y} = y ] && {
 			echo "Type any key when you finished"
			while read tmp
			do 
				return
			done
		}
	fi
	return
fi

SpmPort=`grep $SpmPortNumber $miServiceFile|awk '{print $1}' 2>/dev/null`
if [ $SpmPort -a $SpmPort = 'mi-spm' ]; then
	[ $miSilent = 0 ] && {
		echo "... spm port is already installed ..."
		echo "... nothing to be done ..."
		}
	return
fi

if [ $miCustom = 1 ]; then
	echo
	echo "Shall I change $miServiceFile for you [y]?"
	read tmp
	[ ${tmp:-y} = n ] && return
fi

if [ $SpmPort ];then 
	[ $miSilent = 0 ] && 
		echo "... spm port is used by another service ..."
	while true
	do
   		SpmPortNumber=`expr $SpmPortNumber + 1`
		grep $SpmPortNumber $miServiceFile 1>/dev/null 2>&1
		[ $? != 0 ] && break
	done
	if [ $miCustom = 1 ]; then
		echo
		echo "port to be used by SPM[$SpmPortNumber]?"
		read tmp
		SpmPortNumber=${tmp:-$SpmPortNumber}
	fi			
fi
[ $miSilent = 0 ] &&
	echo "... original $miServiceFile will be saved in ${miServiceFile}.orig ..."
[ ! -f ${miServiceFile}.orig ] &&
	cp $miServiceFile ${miServiceFile}.orig 1>/dev/null 2>&1

[ $miSilent = 0 ] && 
	echo "... adding mi-spm to $SpmPortNumber ..."

(echo "mi-spm          $SpmPortNumber/tcp" >> $miServiceFile) 2>/dev/null
errcode=$?;message="cannot add spm port to $miServiceFile";check_errstat
[ $miCustom = 1 ] && echo "WARNING: $miServiceFile was changed, please verify"
}

#*******************************************************************************
# add spmhostX nicknames to the suitable hosts information file
# for NIS and DNS ask the system adminstrator
# in any other case add it to local hosts file
set_spmhost()
{
spmhost=$HOST
hostfile=/etc/hosts
i=0

if [ $miDeinst = 1 ]; then
	[ $miDNS = 1 -a $miNIShosts = 1 ] && {
		echo "Delete nickname spmhost0 from DNS/NIS hostbase."
		return
		}
	[ $miDNS = 1 ] &&
		echo "Ask your administrator to delete spmhost0 from DNS database."
	[ $miNIShosts = 1  ] && {
		echo "Delete spmhost0 from $miNISmaster's ${hostfile}."
		}
	[ -f ${hostfile}.orig ] && {
		[ $miSilent = 0 ] && 
			echo "... restoring original $hostfile ..."
		mv ${hostfile}.orig $hostfile
		}
	return
fi

[ $miSilent = 0 ] && {
	echo
	echo "... license server has to be known as spmhost0 ..."
	echo "... without this entry no licenses will be found ..."
	echo "... guessing what kind of name resolution will be used ..."
	}

if [ $miDNS = 1 -a $miNIS = 1 ]; then
	[ $miSilent = 0 ] &&
		echo "... hmm, you are probably running NIS and DNS ..."

	echo "nickname spmhost0 has to be added to SPM-server's entry"
	echo "in the DNS/NIS hostbase. Ask your system administrator."
	echo
	return
fi

if [ $miDNS = 1 -a $miNIS != 1 ]; then
	[ $miSilent = 0 ] && 
		echo "... you are probably getting hostnames via DNS ..."

	echo "Add nickname spmhost0 to the SPM-server in the DNS hostbase."
	return
fi

if [ $miNIShosts = 1 -a $miNISmaster != $HOST ]; then
	ypcat hosts | grep -v grep | grep $HOST | grep spmhost  1>/dev/null 2>&1
        [ $? = 0 ] && {
                [ $miSilent = 0 ] &&
                        echo "... $HOST already registered as spmhost ..."
                return
                }

	[ $miSilent = 0 ] && {
		echo "... getting hostnames via NIS from $miNISmaster ..."
		echo "... ${miNISmaster}'s $hostfile has to be changed ..."
	        echo "Add nickname spmhost0 to $HOST's entry in ${miNISmaster}'s $hostfile"
		echo
		}
        return

fi

grep $HOST $hostfile | grep -v grep | grep spmhost 1>/dev/null 2>&1
	[ $? = 0 ] && {
		[ $miSilent = 0 ] &&
			echo "... $HOST already registered as spmhost ..."
	return
	}

if [ $miCustom = 1 ]; then
	echo
	echo "Shall I change local $hostfile for you [y]?"
	read tmp
	[ ${tmp:-y} = n ] && {
		return
		}
fi

grep $HOST $hostfile 1>/dev/null 2>&1
if [ $? != 0 ] ;then
	echo "... WARNING: $miSpmHost is not in $hostfile ..."
	echo "... Add this entry and rerun the installation procedure ..."
	errcode=1;message="";check_errstat
fi

grep spmhost $hostfile 1>/dev/null 2>&1
if [ $? = 0 ] ;then
	[ $miSilent = 0 ] && 
		echo "... there already exist spmhosts in the network ..."
	if [ $miCustom = 1 ]; then
		echo
		echo "Shall I register $HOST as an additional spmhost[y]?"
		read tmp
		[ ${tmp:-y} = n ] && {
			return
		}
	fi
fi
while true
do 
	grep spmhost$i $hostfile 1>/dev/null 2>&1
	[ $? != 0 ] && break
	i=`expr $i + 1`
done
(sed -e "/$HOST	/ s/$/ spmhost${i}/" < $hostfile > /tmp/hosts.tmp) 2>/dev/null
errcode=$?;message="cannot write in this directory";check_errstat
grep spmhost${i} /tmp/hosts.tmp 1>/dev/null 2>&1
if [ $? != 0 ] ;then
	(sed -e "/$HOST / s/$/ spmhost${i}/" <$hostfile > /tmp/hosts.tmp) 2>/dev/null
fi
grep spmhost${i} /tmp/hosts.tmp 1>/dev/null 2>&1
if [ $? != 0 ] ;then
	(sed -e "/$HOST$/ s/$/ spmhost${i}/" <$hostfile > /tmp/hosts.tmp) 2>/dev/null
fi
grep spmhost${i} /tmp/hosts.tmp 1>/dev/null 2>&1
if [ $? != 0 ];then
	echo "WARNING: changing $hostfile failed, please do it yourself"
	return
fi
[ $miSilent = 0 ] &&
	echo "... original $hostfile will be saved in ${hostfile}.orig ..." 

[ ! -f ${hostfile}.orig ] && 
	cp $hostfile ${hostfile}.orig 1>/dev/null 2>&1

mv /tmp/hosts.tmp $hostfile 2>/dev/null
errcode=$?;message="no permission to overwrite $hostfile";check_errstat
[ $miSilent = 0 ] && echo "... $HOST registered as spmhost$i ..."
[ $miCustom = 1 ] && echo "WARNING: $hostfile was changed, please verify"
}

#*******************************************************************************
# install local SPM-daemon spmd in /var/spm
# copy spm-key to /var/spm
# start spmd
# only root can do that
install_spmd()
{
if [ $miDeinst = 1 ]; then
	[ $miSilent = 0 ] && 
		echo "... removing $SPMDIR ..."
	[ -d $SPMDIR ] &&
		rm -rf $SPMDIR
	return
fi


if [ ! -d $SPMDIR ];then
	mkdir -p $SPMDIR 2>/dev/null
	errcode=$?;message="cannot make $SPMDIR";check_errstat
	chmod 755 $SPMDIR 2>/dev/null
else
	[ -f $SPMDIR/spmd ] && { 
		if [ $miCustom = 1 ];then
			echo
			echo "The License Server is already installed, continue with installation? [y]"
			read tmp
			[ ${tmp:-y} = n ] && {
				exit 1
			}
		fi
	}
fi

[ $miSilent = 0 ] && {
	echo
	echo "... copying SPM software to $SPMDIR ..."
	}

if [ $miRelay = 0 ]; then
	cp ${FROM}/spm_key $SPMDIR/spm_key	2>/dev/null
	errcode=$?;message="cannot copy spm_key to $SPMDIR";check_errstat
	chmod 750 $SPMDIR/spm_key
fi
cp ${FROM}/spm-config $SPMDIR/spm-config	2>/dev/null
errcode=$?;message="cannot copy spm-config to $SPMDIR";check_errstat

if [ $miRelay = 1 ]; then
	cp ${FROM}/spmd.relay $SPMDIR/spmd	2>/dev/null
	errcode=$?
	message="cannot copy spmd.relay to $SPMDIR"
	check_errstat
	chmod 750 $SPMDIR/spmd 2>/dev/null
	RelayFile=`ls ${FROM}/relay*`
	errcode=$?;message="cannot find relay keyfile in $FROM";check_errstat
	cp $RelayFile $SPMDIR 2>/dev/null
	errcode=$?;message="cannot copy $RelayFile to $SPMDIR";check_errstat
	sed "s/# FLEXLM_LICENSE_FILE = \/usr\/local\/flexlm\/licenses\/license.dat/FLEXLM_LICENSE_FILE = ${miFlexPort}@${miFlexServer}/g" < ${SPMDIR}/spm-config > ${SPMDIR}/spm-config.tmp 2>/dev/null
	errcode=$?;message="cannot edit $SPMDIR/spm-config";check_errstat
	mv ${SPMDIR}/spm-config.tmp ${SPMDIR}/spm-config 1>/dev/null 2>&1
else
	cp ${FROM}/spmd $SPMDIR/spmd	2>/dev/null
	errcode=$?;message="cannot copy spmd to $SPMDIR";check_errstat
	chmod 750 $SPMDIR/spmd 2>/dev/null
	cp ${FROM}/spm_key $SPMDIR/spm_key	2>/dev/null
	errcode=$?;message="cannot copy spm_key to $SPMDIR";check_errstat
	chmod 750 $SPMDIR/spm_key
	cp ${FROM}/spm-config $SPMDIR/spm-config	2>/dev/null
	errcode=$?;message="cannot copy spm-config to $SPMDIR";check_errstat
fi
	chmod 640 $SPMDIR/spm-config 2>/dev/null
}

#******************************************************************************
# start it automatically after reboot
# supported OS' : AIX4,HPUX9,OSF1,IRIX5,IRIX6,SUNOS5
update_scripts()
{
[ $miSilent = 0 ] && echo "... updating boot scripts ..."
done=0

case $OS in 
	AIX)	[ -f /etc/inittab.orig ] && done=1;;
	HP-UX)  [ -f /etc/inittab.orig ] && done=1;;
	IRIX)	[ -f /etc/init.d/spm ] && done=1;;
	IRIX64)	[ -f /etc/init.d/spm ] && done=1;;
	OSF1)	[ -f /sbin/init.d/spm ] && done=1;;
	SunOS)	[ -f /etc/init.d/spm ] && done=1;;
esac

[ $done = 1 -a $miSilent = 0 ] &&
	{
	echo "... boot scripts already changed ..."
	return
	}


if [ $miCustom = 1 ] ;then
	echo
	echo "Shall I change boot scripts for you [y]?"
	read tmp
	[ ${tmp:-y} != y ] && {
		return
	}
fi

case $OS in 
	AIX)
		[ $miSilent = 0 ] &&
			echo "... inittab will be saved as inittab.orig ..."
		[ ! -f /etc/inittab.orig ] &&
			cp /etc/inittab /etc/inittab.orig 1> /dev/null 2>&1
		(echo "spm:2:once:$SPMDIR/spmd >/dev/console 2>&1" \
			>>/etc/inittab) 2>/dev/null
		errcode=$?
		message="cannot change /etc/inittab"
		check_errstat
		[ $miCustom = 1 ] && echo "NOTICE: /etc/inittab was changed"
	;;
	HP-UX)
		[ $miSilent = 0 ] &&
			echo "... inittab will be saved as inittab.orig ..."
		[ ! -f /etc/inittab.orig ] &&
			cp /etc/inittab /etc/inittab.orig 1> /dev/null 2>&1
		(echo "spm:2:once:$SPMDIR/spmd >/dev/console 2>&1" \
			>>/etc/inittab) 2>/dev/null
		errcode=$?
		message="cannot change /etc/inittab"
		check_errstat
		[ $miCustom = 1 ] && echo "NOTICE: /etc/inittab was changed"
	;;
	IRIX)
		[ -f /etc/init.d/spm ] && {
			[ $miSilent = 0 ] &&
				echo "... 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/rc2.d/S91spm 2>/dev/null
		errcode=$?;message="cannot link spm to rc2.d";check_errstat
		[ $miCustom = 1 ] && echo "NOTICE: added spm to /etc/init.d"
	;;
 	IRIX64)
		[ -f /etc/init.d/spm ] && {
			[ $miSilent = 0 ] &&
				echo "... 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/rc2.d/S91spm 2>/dev/null
		errcode=$?;message="cannot link spm to rc2.d";check_errstat
		[ $miCustom = 1 ] && echo "NOTICE: added spm to /etc/init.d"
	;;
	OSF1)	
		[ -f /sbin/init.d/spm ] && {
			[ $miSilent = 0 ] &&
				echo "... bootscripts already updated ..."
			return
			}
		cp ${INSTFROM}/rcspm /sbin/init.d/spm 2>/dev/null
		errcode=$?;message="cannot copy spm file to /sbin/init.d"
		check_errstat	
		chmod 750 /sbin/init.d/spm 2>/dev/null
		ln -s /sbin/init.d/spm /sbin/rc0.d/K33spm 2>/dev/null
		errcode=$?;message="cannot link spm to rc0.d";check_errstat
		ln -s /sbin/init.d/spm /sbin/rc3.d/S85spm 2>/dev/null
		errcode=$?;message="cannot link spm to rc3.d";check_errstat
		[ $miCustom = 1 ] && echo "NOTICE: added spm to /sbin/init.d"
	;;
	SunOS)
		[ -f /sbin/init.d/spm ] && {
			[ $miSilent = 0 ] &&
				echo "... 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
		[ $miCustom = 1 ] && echo "NOTICE: added spm to /etc/init.d"
		ln -s /etc/init.d/spm /sbin/rc0.d/K33spm 2>/dev/null
		errcode=$?;message="cannot link spm to rc0.d";check_errstat
		ln -s /etc/init.d/spm /sbin/rc2.d/S90spm 2>/dev/null
		errcode=$?;message="cannot link spm to rc2.d";check_errstat
		[ $miCustom = 1 ] && echo "NOTICE: added spm to /etc/init.d"
	;;
	esac
}

#******************************************************************************
#starting SPM daemon if licenses are already installed

start_spmd()
{
[ $miSilent = 0 ] && {
	echo
	echo "... starting the License Server ..."
	}

[ $miRelay = 0 -a  ! -f ${SPMDIR}/.spm-kf ] &&
	{
	echo	
	echo "NOTICE:"
	echo "Cannot start the License Server yet."
	echo "Licenses have to be installed first."
	echo
	${INSTFROM}/mi_install_licenses $miOptions
	[ $? != 0 ] && exit $?
	return
	}

if [ $miCustom = 1 ] ;then
	echo
	echo "start the License Server now [y]?"
	read tmp
else
	tmp=y
	[ $miSilent = 0 ] && echo "... starting the License Server ..."
fi

$PS | grep -v grep | grep spmd 1>/dev/null 2>&1
[ $? = 0 ]  && ${INSTFROM}/rcspm stop 

[ ${tmp:-y} = y ] && {
	${SPMDIR}/spmd 1>/dev/null 2>&1 &
	errcode=$?
	message="cannot start the License Server"
	check_errstat
	[ $miSilent = 0 ] && echo "... License Server started ..."
	}

}

#******************************************************************************
# deinstall boot script for spmd

deinstall_spmd()
{

[ $miSilent = 0 ] && echo "... stopping the License Server ..."
${INSTFROM}/rcspm stop 1>/dev/null 2>&1
case $OS in 
	AIX)
		[ $miSilent = 0 ] && 
			echo "... restoring /etc/inittab ..."
		[ -f /etc/initttab.orig ]
			mv /etc/inittab.orig /etc/inittab 1>/dev/null 2>&1
	;;
	HP-UX)
		[ $miSilent = 0 ] && 
			echo "... restoring /etc/inittab ..."
		[ -f /etc/initttab.orig ]
			mv /etc/inittab.orig /etc/inittab 1>/dev/null 2>&1
	;;
	IRIX)
		[ $miSilent = 0 ] && 
			echo "... removing /etc/init.d/spm ..."
			[ -f /etc/init.d/spm ] && {
			rm -f /etc/init.d/spm rm -f /etc/rc0.d/K33spm 
			rm -f /etc/rc2.d/S91spm
			}
	;;
 	IRIX64)
		[ $miSilent = 0 ] && 
			echo "... removing /etc/init.d/spm ..."
		[ -f /etc/init.d/spm ] && {
			rm -f /etc/init.d/spm
			rm -f /etc/rc0.d/K33spm 
			rm -f /etc/rc2.d/S91spm
			}
	;;
	OSF1)	
		[ $miSilent = 0 ] && 
			echo "... removing /etc/init.d/spm ..."
		[ -f /etc/init.d/spm ] && {
			rm -f /etc/init.d/spm
			rm -f /etc/rc0.d/K33spm 
			rm -f /etc/rc2.d/S85spm
			}
	;;
	SunOS)
		[ $miSilent = 0 ] && 
			echo "... removing /etc/init.d/spm ..."
		[ -f /etc/init.d/spm ] && {
			rm -f /etc/init.d/spm
			rm -f /etc/rc0.d/K33spm 
			rm -f /etc/rc2.d/S90spm
			}
	;;
	esac

}


#******************************************************************************
# main of mi_install_spm                                                      *
#******************************************************************************

root_check
if [ ! -f $miConfig ] ;then 
	touch $miConfig
	errcode=$?
	message="Cannot touch $miConfig"
	check_errstat
else
	[ $miSilent = 0 ] && 
		echo ... using configuration file $miConfig ...
fi


[ -f ${SPMFROM}/spmd.relay ] && {
	miRelay=1
	miName='Relay Daemon'
	}

[ `basename $miProgname` = 'mi_install_spm' ]  && find_spm_host
if [ $miSpmHost != $HOST  -a $miSpmHost != none ];then
	[ $miSilent = 0 ] &&
		echo "... starting remote installation ..."
 	OS=`$RSH $miSpmHost $UNAME 2>/dev/null`
 	errcode=$?;message="cannot login on $miSpmHost as root";check_errstat
	$RSH $miSpmHost rm -rf ${TMP} 1>/dev/null 2>&1
	$RSH $miSpmHost mkdir ${TMP} 1>/dev/null 2>&1
        errcode=$?
        message="mkdir ${TMP} on $miSpmHost failed"
        check_errstat
	rcp ${INSTFROM}/rcspm ${miSpmHost}:${TMP}
	rcp ${INSTFROM}/mi_checkos ${miSpmHost}:${TMP}
	miExt=`$RSH $miSpmHost ${TMP}/mi_checkos 2>/dev/null`
	[ $miExt = none ] && {
		echo "This version of ${OS} is not supported for License Server"
		continue
		}
	SPMFROM=${ROOT}/${miExt}/spm
	trap "$RSH $miSpmHost  'rm -rf ${TMP}'" 2 15
	[ -f ${SPMFROM}/spmd ] && {
 		rcp ${SPMFROM}/spmd $miSpmHost:${TMP}/spmd
 		errcode=$?
		message="Cannot copy ${SPMFROM}/spmd"
		check_errstat
	}
	[ -f ${SPMFROM}/spm_key ] && {
 		rcp ${SPMFROM}/spm_key $miSpmHost:${TMP}
 		errcode=$?
		message="Cannot copy ${SPMFROM}/spm_key"
		check_errstat
	}
	[ -f ${SPMFROM}/spm-config ] && {
 		rcp ${SPMFROM}/spm-config $miSpmHost:${TMP}
 		errcode=$?
		message="Cannot copy ${SPMFROM}/spm-config"
		check_errstat
	}
        rcp $miProgname $miSpmHost:${TMP}/remote_install_spm
        $RSH $miSpmHost  "touch ${TMP}/OK;
                chmod 700 ${TMP}/remote_install_spm;
                ${TMP}/remote_install_spm $miOptions" 2>/dev/null
        rexit_stat
	exit 0
fi

[ $miRelay = 1 -a ! -f ${SPMFROM}/spmd.relay ]  && errcode=1
[ $miRelay = 0 -a ! -f ${SPMFROM}/spmd ] && errcode=1
if [ $errcode = 1 ] ;then
	[ $miDeinst = 1 ] && {
		echo "... there is no License Server installed ..."
		exit 0	
		}
	errcode=1;message="platform not supported";check_errstat
fi
install_spmd	
get_network_information
add_spm_port
set_spmhost
[ $miDeinst = 0 ] && {
	update_scripts
	start_spmd
	[ $miSilent = 0 ] && 
		echo "... installation of the License Server finished ..."
	}
[ $miDeinst = 1 ] && deinstall_spmd
exit 0
