#!/bin/sh 
###############################################################################
# Copyright 1997-1998 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.1.42.1 1998/09/14 23:10:26 f_mcneil Exp $
#
# functions:
#
#	check_errstat
#	root_check
#	rexit_stat
#	check_os
#	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"
#echo " -r recover configuration from file (not implemented yet)"
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

SPMDIR=${SPMDIR:=/var/spm}
SPMFROM=${SPMFROM:=../spm}
INSTFORM=${INSTFROM:=../install}
TMP=${TMP:=/tmp}
miCustom=${miCustom:=1}
miSilent=${miSilent:=0}
miDeinst=${miDeinst:=0}
miProgname="$0"
FROM=$SPMFROM
OS=`uname`
HOST=`hostname`
miSpmHost=none
miExt=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
iscopied=0

# 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`
[ $OK ] || 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
}


# check for supported operating system versions
# set system dependend system calls 
check_os()
{

[ $miSpmHost != $HOST -a $miSpmHost != none ] && UNAME="$RSH $miSpmHost $UNAME"

case $OS in 
	SunOS)
		OSversion=`$UNAME -r | awk -F. '{print $1}' 2>/dev/null`	
		[ $OSversion = 5 ] && {
			miExt=sunos5
			return
			}
	;;
	AIX)
		OSversion=`$UNAME -v`	
		[ $OSversion = 4 ] && {
			miExt=aix4
			return 
			}
	;;
 	OSF1)	
 		OSversion=`$UNAME -r | awk -F. '{print $1}' 2>/dev/null`
 		[ $OSversion = V4 ] && {
 			miExt=decunix4
 			return
 			}
 	;;
	HP-UX)
		OSversion=`$UNAME -r | awk -F. '{print $2}' 2>/dev/null`	
		[ $OSversion = 09 ] && {
			miExt=hpux9
			return
			}
	;;
	IRIX)
		OSversion=`$UNAME -r  2>/dev/null`	
		[ $OSversion = 5.2 ] && miExt=irix
		[ $OSversion = 5.3 ] && miExt=irix
		[ $OSversion = 6.2 ] && miExt=irix
		[ $OSversion = 6.3 ] && miExt=irix
		[ $OSversion = 6.4 ] && miExt=irix
		[ $miExt=irix ]  && return
	;;
  	IRIX64)
  		OSversion=`$UNAME -r | awk -F. '{print $1}' 2>/dev/null`	
  		[ $OSversion = 6 ] && miExt=irix
		[ $miExt=irix ]  && return
  	;;
esac
	
echo "Sorry, this platform $OS $OSversion is not supported"
#[ $miSilent = 0 ] && {
#	echo "... supported platforms are: ..."
#	echo "	IRIX5.2"
#	echo "	IRIX5.3"
#	echo "	IRIX6.2"
#	echo "	IRIX6.3"
##	echo "	IRIX64 6.2"
##	echo "	IRIX64 6.3"
##	echo "	IRIX64 6.4"
#	echo "	AIX4.x"
#	echo "	HP-UX9.x"
#	echo "	SunOS5.x"
#	echo "	OSF1 V4.x"
#	}
errcode=1;message="";check_errstat
}

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

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

[ 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 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()
{
miSpmHost=$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 "... installation of the $miName ..."

if [ $miCustom = 1 ];then
	echo "Which host is your $miName server? [$miSpmHost]?"
	read tmp
	miSpmHost=${tmp:-$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 a $miName running ..."
	if [ $miCustom = 1 ];then
		echo
		echo "Kill $miName and update ?[y]"
		read tmp
                [ ${tmp:-y} = n ] && return
	fi
	[ $miSilent = 0 ] && 
		echo "... OK, trying to install $miName on $miSpmHost ..."
	${INSTFROM}/rcspm stop 1>/dev/null 2>&1
	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`
	if [ $miSilent = 0 ] ;then
		echo "... it seems you are running NIS ..."
		echo "... Your NIS master is $miNISmaster ..."
	fi
	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}'`
	[ $miSilent = 0 ] && {
		echo "... you are using DNS for host name resolution ..."
		echo "... Your nameserver has the address: $miDNSserver ..."
	}
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 "... trying  to add spm port $SpmPort to $miServiceFile ..."
[ $miSilent = 0 ] && 
	echo "... default port number is $SpmPortNumber ..."

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

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

	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 ..."
	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 "NOTICE: $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 $miSilent = 0 ] &&
		echo "Ask your administrator to delete spmhost0 from DNS database"
	[ $miNIShosts = 1 -a $miSilent = 0 ] && {
		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 "... adding default spmhost to hosts information ..."

if [ $miDNS = 1 ]; then
	[ $miSilent = 0 ] && 
		echo "... ask your administrator to change DNS name base ..."
		echo "... add nickname spmhost0 to the SPM-servers hostname ..."
	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 "... ${miNISmaster}'s $hostfile has to be changed ..."
	        echo "Add spmhost0 to $HOST's entry in $miNISmasters $hostfile"
		echo "NOTICE:"
		echo "Without this entry your mental ray servers will not find licenses"
		}

        if [ $miCustom = 1 ] ; then
		echo
                echo "Shall I wait until you changed $hostfile [y]?"
                read tmp
                [ ${tmp:-y} = y ] && {
                        echo "Type any key when you finished"
                        while read tmp
                        do
                                return
                        done
                }
        fi
        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

[ $miSilent = 0 ] && echo "... copy $miName software to $SPMDIR ..."
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 "$miName is already installed, overwrite it?[y]"
			read tmp
			[ ${tmp:-y} = n ] && {
				return
			}
		fi
	}
fi
check_os

cp ${FROM}/spm_key.$miExt $SPMDIR/spm_key	2>/dev/null
errcode=$?;message="cannot copy spm_key.$miExt to $SPMDIR";check_errstat
chmod 750 $SPMDIR/spm_key
cp ${FROM}/spmstat.$miExt $SPMDIR/spmstat	2>/dev/null
errcode=$?;message="cannot copy spmstat.$miExt to $SPMDIR";check_errstat
chmod 750 $SPMDIR/spmstat
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.$miExt $SPMDIR/spmd	2>/dev/null
	errcode=$?
	message="cannot copy spmd.relay.$miExt 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.$miExt $SPMDIR/spmd	2>/dev/null
	errcode=$?;message="cannot copy spmd.$miExt to $SPMDIR";check_errstat
	chmod 750 $SPMDIR/spmd 2>/dev/null
	cp ${FROM}/spm_key.$miExt $SPMDIR/spm_key	2>/dev/null
	errcode=$?;message="cannot copy spm_key.$miExt to $SPMDIR";check_errstat
	chmod 750 $SPMDIR/spm_key
	cp ${FROM}/spmstat.$miExt $SPMDIR/spmstat	2>/dev/null
	errcode=$?;message="cannot copy spmstat.$miExt to $SPMDIR";check_errstat
	chmod 755 $SPMDIR/spmstat
	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 spmd 
# start it automatically after reboot
# supported OS' : AIX4,HPUX9,OSF1,IRIX5,IRIX6,SUNOS5
start_spmd()
{
[ $miSilent = 0 ] && echo "... start $miName and update boot scripts ..."
if [ $miCustom = 1 ] ;then
	echo
	echo "start $miName now [y]?"
	read tmp
else
	tmp=y
	[ $miSilent = 0 ] && echo "... starting $miName now ..."
fi
[ ${tmp:-y} = y ] && {
	${SPMDIR}/spmd
	errcode=$?
	message="cannot start $miName"
	check_errstat
	}

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


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)
		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)
 		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)	
		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)
		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
}

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

deinstall_spmd()
{

[ $miSilent = 0 ] && echo "... stopping $miName ..."
${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

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

[ `basename $miProgname` = 'mi_install_spm' ]  && find_spm_host
if [ $miSpmHost != $HOST  -a $miSpmHost != none ];then
 	OS=`$RSH $miSpmHost $UNAME 2>/dev/null`
 	errcode=$?;message="cannot login on $miSpmHost as root";check_errstat
 	check_os
 	trap "$RSH $miSpmHost rm -f ${TMP}/*spm* ${TMP}/remote_*" 2 15
	if [ $miRelay = 0 ] ;then
		[ -f ${SPMFROM}/spmd.$miExt ] && {
 		rcp ${SPMFROM}/spmd.$miExt $miSpmHost:${TMP}/spmd.$miExt                              	2>/dev/null
 		errcode=$?
		message="Platform not supported"
		check_errstat
		}
		iscopied=1
	fi
	if [ $miRelay = 1 -a $miExt = irix ]; then
		RelayFile=`ls ${SPMFROM}/relay*`
		rcp ${SPMFROM}/$RelayFile $miSpmHost:${TMP}
 		errcode=$?
		message="platform not supported"
		check_errstat
 		rcp ${SPMFROM}/spmd.relay.$miExt                                                   $miSpmHost:${TMP}/spmd.relay.$miExt	2>/dev/null
 		errcode=$?
		message="cannot copy spmd.relay.$miExt to $miSpmHost"
		check_errstat
		iscopied=1
	fi
	if [ $iscopied = 1 ] ;then
 	rcp ${INSTFROM}/rcspm $miSpmHost:${TMP}/rcspm 2>/dev/null
	errcode=$?;message="cannot copy rcspm to $miSpmHost";check_errstat
 	rcp ${SPMFROM}/spm-config $miSpmHost:${TMP}/spm-config 2>/dev/null
	errcode=$?;message="cannot copy spm-config to $miSpmHost";check_errstat
 	rcp ${SPMFROM}/spmstat.$miExt $miSpmHost:${TMP}/spmstat.$miExt                             2>/dev/null
	errcode=$?;message="cannot copy spmstat to $miSpmHost";check_errstat
 	rcp ${SPMFROM}/spm_key.$miExt $miSpmHost:${TMP}/spm_key.$miExt                             2>/dev/null
 	errcode=$?
	message="rcp spm_key.$miExt $miSpmHost failed"
	check_errstat
	fi
	rcp $miProgname $miSpmHost:${TMP}/remote_install_spm
  	$RSH $miSpmHost "touch ${TMP}/OK;
  			chmod 700 ${TMP}/remote_install_spm;
  			${TMP}/remote_install_spm $miOptions"  
  	rexit_stat
  	$RSH $miSpmHost "rm -f ${TMP}/*spm* ${TMP}/remote_*" 2>/dev/null
	exit 0
fi

check_os

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