#!/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.16 2002/03/21 10:51:18 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
#	update_scripts
# 	host_equal
#
###############################################################################

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:/sbin
[ $OS = 'Darwin' ] && PATH=$PATH:/usr/bin:/sbin
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'
[ $OS = 'Darwin' ] && 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
}

#returns true if hosts are equal, otherwise 1
host_equal()
{
host1=`echo $host1 | awk -F. '{print $1}'`
host2=`echo $host2 | awk -F. '{print $1}'`

[ x$host1 = x$host2 ] && return 0

return 1 
}

#******************************************************************************
# 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
host1=$miRayMaster
host2=$HOST
host_equal
if [ x$miRayMaster != x  -a $? = 1 ] ;then
	OS=`$RSH $miSpmHost $UNAME 2>/dev/null`
	errcode=$?; message="cannot login on $miSpmHost as root (look into /tmp/mi-config)";check_errstat
	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 is 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

host1=$miSpmHost
host2=$HOST
host_equal
if [ $? = 1 ]; 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

if [ $OS = 'Linux' -a $miExt = 'linux-x86' ]; then
	[ $miSilent = 0 ] && 
		echo "... installing kernel module ..."
	SMP=`uname -v | awk '{print $2}'` 
	KERN=`uname -r | awk -F. '{print $2}'`

	[ $SMP = 'SMP' ] &&  smp='.smp'
	[ $KERN = '4' ] && kern='.24'

	${FROM}/spm_insmod -f ${FROM}/spmkmod${smp}${kern}.o 1>/dev/null 2>&1

	cp ${FROM}/spm_insmod  $SPMDIR	2>/dev/null
	cp ${FROM}/spmkmod${smp}${kern}.o $SPMDIR	2>/dev/null
	errcode=$?;message="cannot copy spmkmod to $SPMDIR";check_errstat
	chmod 600 $SPMDIR/spmkmod${smp}${kern}.o
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}/spmstat $SPMDIR/spmstat	2>/dev/null
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,Linux X86
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;;
	Linux)	[ -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 /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
		[ $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"
	;;
	Linux)
		[ -f /etc/rc.d/init.d/spm ] && {
			[ $miSilent = 0 ] &&
				echo "... bootscripts already updated ..."
			return
			}
		cp ${INSTFROM}/rcspm /etc/rc.d/init.d/spm 2>/dev/null
		errcode=$?;message="cannot copy spm file to /etc/rc.d/init.d"
		check_errstat	
		chmod 750 /etc/rc.d/init.d/spm 2>/dev/null
		ln -s /etc/rc.d/init.d/spm /etc/rc.d/rc0.d/K33spm 2>/dev/null
		errcode=$?;message="cannot link spm to rc0.d";check_errstat
		level=`runlevel | awk '{print $2}'`
		ln -s /etc/rc.d/init.d/spm /etc/rc.d/rc$level.d/S85spm 2>/dev/null
		errcode=$?;message="cannot link spm to rc$level.d";check_errstat
		[ $miCustom = 1 ] && 
			echo "NOTICE: added spm to /etc/rc$level.d/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 
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
			}
	;;
	Linux)
		[ $miSilent = 0 ] && 
			echo "... removing /etc/rc.d/init.d/spm ..."
		level=`runlevel | awk '{print $2}'`
		[ -f /etc/rc.d/init.d/spm ] && {
			/etc/rc.d/init.d/spm stop
			rm -f /etc/rc.d/init.d/spm
			rm -f /etc/rc.d/rc0.d/K33spm 
			rm -f /etc/rc.d/rc$level.d/S85spm
			}
	;;
	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
host1=$miSpmHost
host2=$HOST
host_equal
if [ $? = 1 -a $miSpmHost != none ];then
	[ $miSilent = 0 ] && {
		[ $miDeinst = 0 ] &&
			echo "... starting remote installation ..."
		[ $miDeinst = 1 ] &&
			echo "... starting remote deinstallation ..."
		}
 	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
	[ $OS = 'Linux' ] && {
		rcp ${SPMFROM}/spmkmod.* ${miSpmHost}:${TMP}
		rcp ${SPMFROM}/spm_insmod ${miSpmHost}:${TMP}
		}
	rcp ${INSTFROM}/rcspm ${miSpmHost}:${TMP}
	rcp ${INSTFROM}/rcspm ${miSpmHost}:${TMP}
	rcp ${INSTFROM}/mi_checkos ${miSpmHost}:${TMP}
	rcp ${INSTFROM}/spmstat ${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

[ $miDeinst = 1 -a ! -d ${SPMDIR} ] && {
		echo "... there is no License Server installed ..."
		exit 0
		}

[ $miRelay = 1 -a ! -f ${SPMFROM}/spmd.relay ] && errcode=1
[ $miRelay = 0 -a ! -f ${SPMFROM}/spmd ] && errcode=1
[ $errcode = 1 ] && 
	errcode=1;message="This platform is not supported.";check_errstat

[ $OS = 'Linux' ] && {
	[ -f /etc/redhat-release ] || 
		errcode=1;
		message="only RedHat 6.x and 7.x are supported";
		check_errstat
	}

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
