#!/bin/csh -f
# $Header: enable,v 1.1007 93/04/22 15:05:25 shawn Exp $

set init = PINIT
set pass = _PASS.IN
umask 0
START:
echo '********************************************************************'
echo '==                  SSI SOFTWARE ENABLE                             '
echo '********************************************************************'
echo ""
if(! $?user ) set user = unset

#if (($user != root) && (`whoami` != root)) then
#	echo '### Please try again as a superuser. '
#	exit 3
#	endif

VERIFYTIME:
set dat = ` date | sed -e 's/\([a-zA-Z]*\) \([a-zA-Z]*\) \([0-9]*\) \([0-9:]*\) \([A-Z]*\) \([0-9]*\)/\1 \2 \3 \6/' `
echo '?? Is this the correct date: '$dat' (y/n)?'

set rep = $<
if (($rep != y) && ($rep != Y)) then
    if ($rep != n) then
	echo '## Please type the character "y" (for yes) or "n" (for no).'
	goto VERIFYTIME	
    else
	echo '## You need to have the system manager correct the system date.'
	echo '## Continuing with an incorrect date is guaranteed to cause'
	echo '## trouble. Please fix the date now.'
	exit(9)
    endif
endif

if ($#argv == 2) goto ARG_IV
if ($#argv == 3) goto ARG_IVA


if ($#argv == 1) then
	set ifcdir = $1
	else
	set ifcdir = `pwd`
	endif

# check for relative name rather than full path name
if (-d /usr/local/fintronic/magellan/$ifcdir ) set ifcdir =
/usr/local/fintronic/magellan/$ifcdir

if (-e $ifcdir/LIB/ifc_rel.h) then
	set ifc_rel = $ifcdir/LIB/ifc_rel.h
	goto SETIFCREL
	endif

if (-e $ifcdir/lib/ifc_rel.h) then
	set ifc_rel = $ifcdir/lib/ifc_rel.h
	goto SETIFCREL
	endif

echo '## Could not find "lib/ifc_rel.h" or "LIB/ifc_rel.h" in:'
echo '##	 "'$ifcdir'"'
echo '## Is software installed in "'$ifcdir'"?'
exit(9)

SETIFCREL:
set i = `grep IFC < $ifc_rel | sed -e 's/.*IFC.*"\(.*\)"/\1/' `
set v = `grep VER < $ifc_rel | sed -e 's/.*VER.*"\(.*\)"/\1/' `

grep ATR < $ifc_rel >& /dev/null
if ($status == 0) then
	set atr = `grep ATR < $ifc_rel | sed -e 's/.*ATR.*"\(.*\)"/\1/' `
	set prop = "-prop $atr"
else
	set atr = ""
	set prop = ""
endif
goto ENABTYP

ARG_IVA:
set i = $1
set v = $2
set prop = "-prop $3"
goto ENABTYP

ARG_IV:
set i = $1
set v = $2
set prop = ""
goto ENABTYP


ENABTYP:
echo ""
if( $atr == "" ) then
    set atr_mesg = ""
else
    set atr_mesg = '("'$atr'")'
endif
echo '== Will enable '`hostname`' for software "'$i'", rev "'$v'"' $atr_mesg

set local = `echo $atr | grep -c "^l"`
if( "$local" != "0" ) then
    echo '==     '`hostname`' will NOT be enabled for remote X Window display'
endif

echo '== Enabling is a two step procedure:'
echo ""
echo 'Step 1: INITIALIZE ("i"):'
echo '		This creates a new "PINIT" file. Only initialize once.'
echo '		Do not re-initialize to extend an expired license,'
echo '		or even if the PINIT is removed after you send it to SSI.'
echo 'Step 2: VALIDATE ("v"):'
echo '		This is used to enter a "PASS.OUT". May be used any number of'
echo '		times to extend or re-enable expired software, as long as you'
echo '		do not re-initialize.'
echo 'TEST ("t"):'
echo '		Optional test to verify at any time if the software is enabled'
echo -n '== Please enter one of : "i", "v", "t": '
set phase = $<
if ( ("$phase" != i) && ("$phase" != v) && ( "$phase" != t) ) goto SETIFCREL
echo ""

if ($phase == i) set soft = pinit
if ($phase == v) set soft = pvalid
FIND_en:
set endir = ( $ifcdir/BIN $ifcdir/bin ) 
foreach dir (  $endir )
	if (-e $dir/sec_usr) then
		if ($phase == i) goto INITIALIZE
		if ($phase == v) goto VALIDATE
		if ($phase == t) goto TEST
		endif
	end
echo '## Missing either of bin/sec_usr or BIN/sec_usr. Incomplete package.'
exit(1)


INITIALIZE:
$dir/sec_usr -action i -pack $i -vers $v $prop > $init
if ($status != 0 ) then
	echo '## Failed initialization or validation.'
	exit(99)
	endif


echo '== GETTING A VALIDATION PASSWORD:'
echo '	Successfully initialized and created a file called PINIT, which you'
echo '	will need to obtain a password from SSI. To obtain a password you'
echo '	can send e-mail to enable@systems.com, or you can dial in to our'
echo '	Telebit WorldBlazer modem (415-812-1830), using 1200 up to 38,400 baud:'
echo ' '
echo '	(a) To use the e-mail method, add two lines to the PINIT file so'
echo '	that it is formatted exactly as follows:'
echo ' '
echo '		END USER: John Doe, Super Systems, Central CAD'
echo '		PINIT:'
echo '		a7b4  a7e5  9e18  293b'
echo '		5d5d  81cb  6c4b  d1'
echo ' '
echo '	Please make sure to include your name, company, and division.'
echo '	E-mail the modified PINIT file to enable@systems.com, and our enabling'
echo '	system will e-mail a password back to you.'
echo ' '
echo '	(b) You can use the modem method to get a password immediately, but'
echo '	you need an account in our system first.  If you need an account,'
echo '	please contact SSI.'
echo ' '
echo '== VALIDATION:'
echo '	Once you get the password back, run the VALIDATION ONLY.'
echo '	DO NOT RE-RUN INITIALIZATION AGAIN, even if the software is reloaded,'
echo '	expires, or the PINIT/PASS.OUT files are removed. SSI can always cut'
echo '	you a new password as long as you do not re-initialize.'
echo '	Otherwise you will give yourself and SSI unnecessary extra work.'
echo ' '
echo '== PROBLEMS:'
echo '	If you have any problems, either: send e-mail to support@systems.com,'
echo '	send a fax to 415 812-1820, or call our main number 415 812-1800.'
echo ' '
exit(0)


VALIDATE:
echo '== In what file have you enter the SSI supplied password?'
echo -n '?? File name? '
set passf = $<
if ($passf != "") then
	if ( -e $passf ) then
		cat $passf > $pass
		goto RUNEN
	else
		if (-e ../$passf) then
			cat ../$passf > $pass
			goto RUNEN
		else
			echo '## Could not find file "'$passf'"'
			goto VALIDATE
		endif
	endif
endif

RUNEN:
$dir/sec_usr -pack $i -vers $v -action v -passwd $pass $prop
if ($status != 0 ) exit(99)
rm -f $pass
set rf = .$i'_'$v.r
set if = .$i'_'$v.i
# chmod a+rx /usr/ssi /usr/local/fintronic/magellan/.lic
chmod a+rx /usr/ssi /usr/local/fintronic/magellan/' '*
if ($status != 0) then
	echo 'Cannot set permissions right in /usr/ssi'
	exit 99
	endif
# touch /usr/local/fintronic/magellan/.lic/'DO_NOT_TOUCH_THIS_SSI_DIR'
touch '/usr/local/fintronic/magellan/            /DO_NOT_TOUCH_THIS_SSI_DIR'
# chmod a+r  /usr/local/fintronic/magellan/.lic/$if
chmod a+r  /usr/local/fintronic/magellan/' '*/$if
if ($status != 0) then
	echo 'Cannot set "i" permissions right'
	exit 98
	endif
# chmod a+rw /usr/local/fintronic/magellan/.lic/$rf
chmod a+rw /usr/local/fintronic/magellan/' '*/$rf
if ($status != 0) then
	echo 'Cannot set "r" permissions right'
	exit 97
	endif
echo ""
echo '*************************************************************'
echo '== Enabling complete; the software is now ready to be used.'
echo '*************************************************************'
exit(0)


TEST:
$dir/sec_usr -pack $i -vers $v -action t
if ($status != 0) then 
echo '## The software in this directory is not enabled to run'
else
echo '== The software in this directory is properly enabled'
endif
exit(0)
