#!/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.6 2000/03/23 15:34:00 runa Exp $
#
# functions:
#
#	check_errstat
#	rexit_stat
#	root_check
#	find_spm_host
#
###############################################################################

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}
INSTFROM=${INSTFROM:=${ROOT}/install}
miConfig=/tmp/mi-config
miExt=`${INSTFROM}/mi_checkos`
[ $miExt = none ] &&
	{
	echo "Platform is not supported for SPM"
	exit
	}
SPMFROM=${SPMFROM:=${ROOT}/${miExt}/spm}
TMP=${TMP:=/tmp}
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

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 licenses"
	check_errstat
fi
}

#****************************************************************************** 
# 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 ] && echo "... looking for your SPM server ..."

root_check
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 licenses for 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
fi

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

if [ $? != 0 ];then 
	[ $miSilent = 0 ] && 
		echo "... there is no SPM daemon running ..." 
	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 ..."

if [ $miSpmhost != $HOST  -a $miSpmhost != none ] ; then
	rcp $miProgname ${miSpmhost}:${TMP}/remote_create_licenses
	trap "$RSH $miSpmhost rm -f ${TMP}/remote_create_licenses" 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-kf ] && {
	echo y | $SPMDIR/spm_key -m mental_images -c ${SPMDIR}/${HOST}.rq
	errcode=$?;message="cannot run $SPMDIR/spm_key";check_errstat
	}

[ -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 $HOST.rq to ray-keys@mental.com ***"
	echo
	}

[ $miCustom = 0 ] && exit 0
}

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

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

if [ $miSpmhost != $HOST  -a $miSpmhost != none ] ; then
	vcfile=${miSpmhost}.vc
fi

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

if [ $miSpmhost != $HOST  -a $miSpmhost != none ] ; then
	rcp $vcfile $miSpmhost:${SPMDIR}/`basename $vcfile` 1>/dev/null 2>&1
	rcp $miProgname ${miSpmhost}:${TMP}/remote_install_licenses
	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
fi

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

echo y | $SPMDIR/spm_key -x $SPMDIR/$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
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 ray-keys@mental.com"
	check_errstat
fi

[ $miSilent = 0 ] &&
	echo "... licenses are installed correctly ..."
}



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

[ ! -f $miConfig ] && {
	touch $miConfig
	errcode=$?
	message="Cannot touch $miConfig"
	check_errstat
	}

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

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

find_spm_host

[ $miCustom = 0 ] && create_license_request

echo choose what you want to do:
echo 1 create a license request
echo 2 install validation code from mental images
echo 3 get help
echo 4 quit

read tmp
miInst=${tmp:-4}

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

