#!/bin/sh 
###############################################################################
# Copyright 1997-2000 by mental images GmbH & Co.KG, Fasanenstr. 81, D-10623
# Berlin, Germany. All rights reserved.
#
# installation of SPM licenses
#
# $Id: mi_install_licenses,v 1.11 2001/07/31 09:51:16 runa Exp $
#
# functions:
#
#	check_errstat
#	rexit_stat
#	root_check
#	find_spm_host
#	host_equal
#	
###############################################################################

usage()
{ echo "usage:  mi_install_licenses [ -sdh ]"
echo "	-h help"
echo "	-s silent installation"
echo "	-d default installation"
exit 0
}


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

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

ROOT=$TOP
[ x$TOP = x ] && ROOT=`dirname $0`/..
SPMDIR=${SPMDIR:=/var/spm}
TMP=${TMP:=/tmp/mi}
if [ $0 = ${TMP}/remote_create_licenses -o $0 = ${TMP}/remote_install_licenses -o $0 = ${TMP}/remote_list_licenses ] ; then
	INSTFROM=$TMP
else
	INSTFROM=${INSTFROM:=${ROOT}/install}
fi
miConfig=/tmp/mi-config
miExt=`${INSTFROM}/mi_checkos`
[ $miExt = none ] &&
	{
	echo "Platform is not supported for SPM"
	exit
	}
SPMFROM=${SPMFROM:=${ROOT}/${miExt}/spm}
miCustom=${miCustom:=1}
miSilent=${miSilent:=0}
miDeinst=${miDeinst:=0}
miProgname="$0"
OS=`uname`
HOST=`hostname`
miSpmhost=none
miInst=4

# set system dependent system calls
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
[ $OS = 'Darwin' ] && 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'
[ $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 licenses"
	check_errstat
fi
}

#returns 0 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 SPM server
# default SPM host is localhost
# in customize mode it is possible to install on another host
find_spm_host()
{

[ $miSilent = 0 -a $miProgname != ${TMP}/remote_create_licenses ] && 
	echo "... determining 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 ];then
	echo
	echo "Generate/Install/List licenses for server [$miSpmhost]?"
	read tmp
	[ x$tmp = 'xn' ] && exit
	[ x$tmp = 'xy' ] || 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
fi

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 spmd | grep -v grep 1>/dev/null 2>&1

if [ $? != 0 ] ;then 
	if [ $miCustom = 1 ] ;then
		[ -d $SPMDIR ] || {
			echo "SPM software is not installed in $SPMDIR"
			echo "Do you want to install SPM software first[y]?"
			read tmp
                	[ ${tmp:-y} = y ] && ./mi_install_spm $miOptions
		}
	fi
fi

[ $miSpmhost = 'none' ] && {
		errcode=1;message="I cannot find your SPM server"
		check_errstat
		}
}

#**************************************************************************
# creates a license request file 'hostname'.rq

create_license_request()
{
[ $miSilent = 0 -a $miProgname != ${TMP}/remote_create_licenses ] && 
	echo "... creating license request ..."

host1=$miSpmhost
host2=$HOST
host_equal
if [ $? = 1 -a $miSpmhost != none ] ; then
	$RSH $miSpmhost rm -rf $TMP 
	$RSH $miSpmhost mkdir $TMP 
	rcp ${INSTFROM}/mi_checkos ${miSpmhost}:${TMP}
	rcp $miProgname ${miSpmhost}:${TMP}/remote_create_licenses
	trap "$RSH $miSpmhost rm -rf ${TMP}" 2 15
	$RSH $miSpmhost "touch ${TMP}/OK;
			chmod 700 ${TMP}/remote_create_licenses;
			${TMP}/remote_create_licenses $miOptions;
  			rm -f ${TMP}/remote_create_licenses"
	rexit_stat
	rcp ${miSpmhost}:${SPMDIR}/${miSpmhost}.rq . 1>/dev/null 2>&1
	errcode=$?
	message="${miSpmhost}.rq cannot be copied back from ${miSpmhost}:${TMP}"
	check_errstat
	return
fi

[ -f $SPMDIR/spm_key ] || {
	echo
	echo "SPM software not installed"
	echo "Please choose 'install license server'."
	rm -f /tmp/mi/OK
	return
	}

[ -f $SPMDIR/.spm-kf ] && {
	if [ $miCustom = 1 ] ;then
		$SPMDIR/spm_key -c ${SPMDIR}/${HOST}.rq
		errcode=$?;message="cannot run ${SPMDIR}/spm_key";check_errstat
	else
		$SPMDIR/spm_key -m Anybody -c ${SPMDIR}/${HOST}.rq
		errcode=$?;message="cannot run ${SPMDIR}/spm_key";check_errstat
	fi
}

[ -f $SPMDIR/.spm-kf ] || {
	echo y | $SPMDIR/spm_key -c ${SPMDIR}/${HOST}.rq
	errcode=$?;message="cannot run $SPMDIR/spm_key";check_errstat
	}

[ $miSpmhost = $HOST ] && cp ${SPMDIR}/${HOST}.rq .

[ $miSilent = 0 ] && {
	echo
	echo "*** Please mail file ./$HOST.rq to your license issuer. ***"
	echo
	}

[ $miCustom = 0 ] && exit 0
}

#**************************************************************************
# install license validation code from 'hostname.vc'

install_license()
{
vcfile=${HOST}.vc
if [ $miSilent = 0 -a $miProgname != ${TMP}/remote_install_licenses ] ;then
	echo "... installing your validation code ..."

	[ $miSpmhost != $HOST -a $miSpmhost != none ] && vcfile=${miSpmhost}.vc

	if [ ! -f $vcfile ] ; then
		echo "Where is your validation file [$vcfile]?"
		read tmp
		vcfile=${tmp:-$vcfile}
		[ -f $vcfile ] || {
			errcode=$?
			message="cannot find $vcfile"
			check_errstat
		}
	fi
fi

host1=$miSpmhost
host2=$HOST
host_equal
if [ $? = 1 -a $miSpmhost != none ] ; then
	$RSH $miSpmhost rm -rf $TMP 
	$RSH $miSpmhost mkdir $TMP 
	rcp ${INSTFROM}/mi_checkos ${miSpmhost}:${TMP}
	rcp ${INSTFROM}/rcspm  ${miSpmhost}:${TMP}
	rcp $vcfile $miSpmhost:/var/spm 1>/dev/null 2>&1
	rcp $miProgname ${miSpmhost}:${TMP}/remote_install_licenses
	rcp ${INSTFROM}/mi_checkos ${miSpmhost}:${TMP}
	trap "$RSH $miSpmhost rm -f ${TMP}/remote_install_licenses" 2 15
	$RSH $miSpmhost "touch ${TMP}/OK;
			chmod 700 ${TMP}/remote_install_licenses;
			${TMP}/remote_install_licenses $miOptions;
  			rm -f ${TMP}/remote_install_licenses"
	rexit_stat
	return
fi

[ -f $SPMDIR/spm_key ] || {
	echu
	-cho "SPM software not installed."
	-cho "Please choose 'install license server'."
	rm -f /tmp/mi/OK
	return
	}

[ $miSpmhost = $HOST ] && cp $vcfile $SPMDIR/`basename $vcfile` 1>/dev/null 2>&1

echo y | $SPMDIR/spm_key -x $SPMDIR/`basename $vcfile`
errcode=$?;message="cannot run $SPMDIR/spm_key";check_errstat

$PS | grep -v grep | grep spmd 1>/dev/null 2>&1
if [ $? = 0 ];then 
	[ $miSilent = 0 ] && 
		echo "... sending kill -HUP to spmd ..."
	${INSTFROM}/rcspm restart 	
else
	[ $miSilent = 0 ] &&
		echo "... starting spmd ..."
	${INSTFROM}/rcspm start
	errcode=$?;message="cannot start $SPMDIR/spmd";check_errstat
fi

if [ $miCustom = 1 ]; then
	echo
	echo "Verify your license installation [y]?"
	read tmp
	[ ${tmp:-y} = n ] && return
	$SPMDIR/spm_key -t 2>/dev/null
	errcode=$?
	message="There are some problems, contact your license issuer"
	check_errstat
fi

}

#**************************************************************************
# list installed licenses 

list_licenses()
{
[ $miSilent = 0 -a $miProgname != ${TMP}/remote_list_licenses ] && 
	echo "... list licenses installed on $miSpmhost ..."

host1=$miSpmhost
host2=$HOST
host_equal
if [ $? = 1 -a $miSpmhost != none ] ; then
	$RSH $miSpmhost rm -rf $TMP 
	$RSH $miSpmhost mkdir $TMP 
	rcp ${INSTFROM}/mi_checkos ${miSpmhost}:${TMP}
	rcp $miProgname ${miSpmhost}:${TMP}/remote_list_licenses
	trap "$RSH $miSpmhost rm -rf ${TMP}" 2 15
	$RSH $miSpmhost "touch ${TMP}/OK;
			chmod 700 ${TMP}/remote_list_licenses;
			${TMP}/remote_list_licenses $miOptions"
	rexit_stat
	return
fi

if [ -f $SPMDIR/spm_key ] ;then
		[ -f $SPMDIR/.spm-kf ] || {
			echo
			echo "There are no licenses installed."
			return
			}
		$SPMDIR/spm_key -t 
else
		echo 
		echo "SPM software not installed."
		echo "Please choose 'install license server'."	
		rm -f /tmp/mi/OK
		return
fi
errcode=$?;message="cannot run $SPMDIR/spm_key";check_errstat
}


#**************************************************************************
#	mi_install_licenses main
#**************************************************************************

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

[ -f ${SPMFROM}/*relay*-kf ] && {
	[ $miSilent = 0 ] &&
		echo "... FLEXlm licenses are provided by Softimage ..."
	exit 0
	}

[ $miProgname = "${TMP}/remote_create_licenses" ] && {
			create_license_request
			exit 0 
			}
[ $miProgname = "${TMP}/remote_install_licenses" ] && {
			install_license
			exit 0 
			}
[ $miProgname = "${TMP}/remote_list_licenses" ] && {
			list_licenses	
			exit 0
			}

[ $miSilent = 0 ] && {
	echo
	echo "*** start license management ***"
	echo
	}

find_spm_host

[ $miCustom = 0 ] && create_license_request

echo
echo choose what you want to do:
echo 1 create a license request
echo 2 install validation code
echo 3 list installed licenses
echo 4 get help
echo 5 quit

read tmp
miInst=${tmp:-4}

case $miInst in
        1) create_license_request;;
        2) install_license;;
	3) list_licenses;;
        4) usage;;
        5) exit 0;;
	q) exit 0;;
esac

