#!/bin/sh
#
#	fmlicense 2.0 1/22/89
#
# 	Copyright (c) 1988-1989 by Frame Technology Corp.
#
#	Modification history:
#	v 1.3	6/22/88:		Suzann Larsen
#			Original.
#	v 2.0	1/22/89:		Eddie Kessler
#
version=2.0
script=`basename $0`
usrfil=frameusers
autofil=autostart
envfil=env.sh
envfilcsh=env.csh
mkrdir=$FMHOME/.fminit2.0
usrdir=$FMHOME/.fminit2.0
thishost=`hostname`
umask 022

# Routine to ask a question answered y or n.
ask(){
	answer=undefined
	until [ "$answer" = "y" -o "$answer" = "n" ]; do 
		echo -n "$1 [y]: "
		read answer
		if [ "$answer" = "" ]; then
			answer=y
		fi
	done
	[ $answer = y ]
}

# Subroutine:   chkexist filename
chkexist () {
chkfile=$1
if [ -s $usrdir/$chkfile ]; then
    echo ""
    echo "$usrdir/$chkfile exists."
    echo "    (You should overwrite it unless you have manually modified the file.)"
    if ask "    OK to overwrite existing $chkfile?"; then
	echo ""
    else
        echo ""
        echo "    Existing $chkfile will NOT be overwritten."
        echo ""
        return 1
    fi
    rm -f $usrdir/$chkfile
fi
return 0
}
# end of subroutines

# identify ourselves to user
echo "$script $version:
"

# save arg1 (calls to subroutines destroy it)
arg1=$1

if [ "$PWD" = "" ]; then
	cwd=`pwd`
else
	cwd=$PWD
fi

# if arg 1 exists, it should be directory where to find license files
if [ $arg1 ]; then
	if [ `expr index $arg1 '//'` = 1 ]; then	# fully rooted
		usrdir=$arg1
	else
		usrdir=$cwd/$arg1
	fi
	if [ ! -d $usrdir ]; then
		mkdir $usrdir
		if  [ $? != 0 ]; then
			echo "$script: Could not create $usrdir."
			exit 1
		fi
	fi
fi

echo "fmlicense creates and modifies the frameusers file
($usrdir/frameusers).  The frameusers file
contains licensing information and controls users' access to licenses.
If the frameusers file contains invalid information, Frame products
will not run.

CAUTION: Run this program only on the machine that you intend to
use as the default license server host.  (The license server host is
the machine that runs the license server process and is typically the
machine on which you installed the Frame software.)
This machine is $thishost with hostid `hostid`.
"

if ask "Would you like to continue?"; then
	echo 
else
	exit 1
fi

# check for correct permissions on directory
if [ ! -d $usrdir ]; then
    echo "$script: $usrdir is not a directory"
    exit 1
fi
if [ ! -x $usrdir ]; then
    echo "$script: You don't have execute permission in $usrdir"
    exit 1
fi

# if no users file, copy in the sample file
if [ ! -s $usrdir/$usrfil ]; then 
    if [ ! -s $mkrdir/$usrfil.sample -o ! -r $mkrdir/$usrfil.sample ]; then
        echo "$script: $mkrdir/$usrfil.sample doesn't exist or not readable."
        exit 1
    fi
    cp $mkrdir/$usrfil.sample $usrdir/$usrfil
    if [ $? != 0 ]; then
        echo "$script: No $usrfil in $usrdir, and couldn't copy sample."
        exit 1
    fi
fi

# check for correct permissions on users file
if [ ! -r $usrdir/$usrfil ]; then
    echo "$script: $usrdir/$usrfil cannot be read."
    exit 1
fi

# test -w is busted
touch $usrdir/$usrfil 2> /dev/null
if [ $? = 0 ]; then
    echo "
This program will prompt you for the following information:

1.  The host ID of the license server host
2.  The host ID of the backup server host (optional)
3.  Your Frame serial number (obtain from Frame)
4.  The number of licenses you have purchased for FrameMaker (U.S. version)
5.  The number of licenses you have purchased for FrameWriter
6.  The number of licenses you have purchased for International FrameMaker
7.  The number of licenses you have purchased for FrameViewer
8.  Your Software Update Plan's expiration date
9.  Your temporary password's expiration date (only necessary if you have
	a temporary password)
10. Your Frame password

You can determine your server hosts' host IDs by typing 'hostid' at the
server hosts' prompts.  If you have not already obtained the other licensing
information from Frame, please contact your Frame dealer.
"

    if ask "Would you like to continue?"; then
	    echo 
    else
	    exit 1
    fi
else
	echo "Cannot write into $usrdir/$usrfil."
	exit 1
fi

# tell user what file we are using and call program to do the edits 
echo "Using $usrdir/$usrfil.....
"
$FMHOME/bin/bin.`arch`/usersedit $usrdir/$usrfil -mc
# use ed to remove first line ( <TheRoot ) & last line ( > ) from file
if [ $? = 0 ]; then
    echo "1d
    \$d
    w
    q" | ed $usrdir/$usrfil >/dev/null
fi

if [ -w $usrdir ]; then

	# create set environment script
	echo "Creating $usrdir/$envfil"
	echo "    (script to set FRAMEUSERSD_HOST and FRAMEUSERSD_RC)"
	chkexist $envfil
	if [ $? = 0 ]; then
	    echo "FRAMEUSERSD_HOST=$thishost" > $usrdir/$envfil
	    if [ $arg1 ]; then
		echo "FRAMEUSERSD_RC=$usrdir/autostart" >> $usrdir/$envfil
	    else
		echo "FRAMEUSERSD_RC=\$FMHOME/.fminit2.0/autostart" >> $usrdir/$envfil
	    fi
	    echo "export FRAMEUSERSD_HOST FRAMEUSERSD_RC" >> $usrdir/$envfil

	    echo "setenv FRAMEUSERSD_HOST $thishost" > $usrdir/$envfilcsh
	    if [ $arg1 ]; then
		echo "setenv FRAMEUSERSD_RC $usrdir/autostart" >> $usrdir/$envfilcsh
	    else
		echo "setenv FRAMEUSERSD_RC \$FMHOME/.fminit2.0/autostart" >> $usrdir/$envfilcsh
	    fi
	fi

	# create autostart script
	echo "Creating $usrdir/$autofil"
	echo "    (script to auto-start the Frame License Server)"
	chkexist $autofil
	if [ $? = 0 ]; then
	    echo "#! /bin/sh
if [ \$1 = \`hostname\` ]; then
	echo Attempting local auto-start of the Frame License Server.
	\$FMHOME/bin/rpc.frameusersd $usrdir/frameusers
else
	echo Attempting remote auto-start of the Frame License Server on host \$1.
	rsh \$1 -n /bin/ls / > /dev/null 2>&1
	if [ \$? != 0 ]; then
		echo Failed to access \\(rsh\\) remote FLS host at \$1.
		exit 1 
	fi 
	rsh \$1 -n \\
		'cd $FMHOME; bin/rpc.frameusersd $usrdir/frameusers' &
	if [ \$? != 0 ]; then
        echo Failed to access \\(rsh\\) remote FLS host at \$1.
		exit 1
	fi
fi
echo Please wait 1 minute while the license server process initializes.
sleep 30
echo Please wait 30 seconds while the license server process initializes.
sleep 20
echo Please wait 10 seconds while the license server process initializes.
sleep 10
echo Ready.
exit 0
" > $usrdir/$autofil
	    chmod 0755 $usrdir/$autofil
	fi
else
	echo "$usrdir not writable"
fi

# rename makerpwd so maker doesn't access old license file.
if [ -f $FMHOME/.fminit2.0/makerpwd ]; then
    mv -f $FMHOME/.fminit2.0/makerpwd $FMHOME/.fminit2.0/makerpwd.v1.1
    if [ $? != 0 ]; then
        echo "$script: Was unable to rename $FMHOME/.fminit2.0/makerpwd to makerpwd.v1.1"
        echo "    This is essential for the current licensing to work."
        echo "    You must do this manually before proceeding."
    fi
fi

echo
if ask "Would you like to start the license server process on $thishost?"; then
    pid=`ps axww | egrep 'rpc.frameusersd' | egrep -v '(grep|log)' | \
	awk -e '{print $1}'`
    if [ "$pid" != "" ]; then
	pid=`echo $pid`
	status=`kill -9 $pid 2>&1`
	if [ "$status" != "" ]; then
	    echo "There is a license server process already running on this machine.
You don't have permission to kill it.  Please have the superuser execute
the kill -9 $pid, and then run this program again."
	    exit 1
	fi
	echo Killed $pid.
    fi
    # run autostart script just created
    echo "Running $usrdir/$autofil script."
    $usrdir/$autofil $thishost
    echo "
If the license server process is \"listening,\" you have successfully
completed the licensing of your Frame software.  If the password is
incorrect for the information you have entered, please run $script
again and enter the information exactly as you obtained it from Frame
Technology.  If you customize your frameusers file, run fmlicense
again to restart the license server process with the new licensing
information."
fi
echo "
$script done."
exit 0
