#!/bin/sh
# $Id: fmsetupfls,v 1.13 1993/02/02 16:05:18 sbs Exp $
# Copyright (c) 1992-1993 Frame Technology Corp. All rights reserved.
#
# fmsetupfls
#
version=4.0
script=`basename $0`
usrfil=licenses
autofil=fm_fls_auto
envfil=env.sh
envfilcsh=env.csh
mkrdir=$FMHOME/fminit
Prtdir='$FMHOME/fminit'	# version of usrdir suitable for printing
eval usrdir=$Prtdir	# actual usrdir value
logdir=$Prtdir/tmp
thishost=`hostname`
ASK=$FMHOME/bin/scripts/fm_ask
OVW=$FMHOME/bin/scripts/fm_overwrite
umask 022

usage="Usage:""
""  $script directoryname""
""to set up licenses in the given directory (default $usrdir).""
""  $script -help""
""to print this message""
"

# identify ourselves to user
echo "$script $version:
"
cwd=`pwd`

# if arg 1 exists, it should be directory where to find license files
case $1 in
'')
	;;
-h*)
	echo "$usage"
	exit 0
	;;
/*)
	usrdir=$1
	Prtdir=$usrdir
	logdir=$usrdir
	;;
*)
	usrdir=$cwd/$1
	Prtdir=$usrdir
	logdir=$usrdir
	;;
esac
[ -d $usrdir ] || mkdir $usrdir || {
	echo "$script: Could not create $usrdir."
	echo "$usage"
	exit 1
}

echo "$script creates and modifies the licenses file
($usrdir/licenses).  The licenses file
contains licensing information and controls users' access to licenses.
If the licenses 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.
"

$ASK y n "Would you like to continue?" || exit 1

# check for correct permissions on directory
if [ ! -d $usrdir ]; then
	echo "$script: $usrdir is not a directory"
	echo "$usage"
	exit 1
fi
if [ ! -x $usrdir ]; then
	echo "$script: You don't have execute permission in $usrdir"
	echo "$usage"
	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
		> $usrdir/$usrfil
	else
		cp $mkrdir/$usrfil.sample $usrdir/$usrfil
	fi
	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."
	echo "$usage"
	exit 1
fi

# test -w is busted
touch $usrdir/$usrfil 2> /dev/null || {
	echo "Cannot write into $usrdir/$usrfil."
	echo "$usage"
	exit 1
}

echo "
""This program will now ask you to add your licenses to the licenses
""file $usrdir/$usrfil.
"
$ASK y n "Would you like to continue?" || exit 1

$FMHOME/bin/fmaddlicense -mc $usrdir/$usrfil || exit 1

dir=
until [ -n "$dir" ]; do
	echo "Name of the directory into which the license server should write
""usage logs [$logdir]:"
	read dir junk
	case $dir in
	'')
		eval dir=$logdir
		;;
	*)
		# force interpretation of ~ -- will this break USG systems?
		dir=`csh -fc "echo $dir"`
		;;
	esac
	case $dir in
	'')
		# empty from botched filename expansion.
		continue
		;;
	/*)
		;;
	*)
		dir=$cwd/$dir
		;;
	esac
	if [ ! -d "$dir" ] && \
	 $ASK n y "$dir doesn't exist or is not a directory (use this name anyway)?"
	then
		dir=
	elif [ ! -w "$dir" ] && \
	 $ASK n y "You don't have permission to create files in directory""
""$dir (use this name anyway)?"
	then
		dir=
	fi
done
logfile=$dir/fm_fls.log


# create set environment script
echo "Creating $usrdir/$envfil"
echo "    (script to set FM_FLS_HOST and FM_FLS_AUTO)"
if $OVW $usrdir/$envfil; then
	echo \
 "FM_FLS_HOST=$thishost""
""FM_FLS_AUTO=$Prtdir/$autofil""
""export FM_FLS_HOST FM_FLS_AUTO" > $usrdir/$envfil
	echo \
 "setenv FM_FLS_HOST $thishost""
""setenv FM_FLS_AUTO $Prtdir/$autofil" > $usrdir/$envfilcsh
fi


# create autostart script
echo "Creating $usrdir/$autofil"
echo "    (script to auto-start the Frame License Server)"
if $OVW $usrdir/$autofil; then
	da=`date`
	echo '#!/bin/sh
''# '"$autofil written $da by ${USER:-$LOGNAME}"'
''logfile="'"$logfile"'"
''usrdir="'"$usrdir"'"
''# How System V spells "rsh"
''if [ -f /usr/bin/remsh ]; then
''	rshcmd=/usr/bin/remsh
''else
''	rshcmd=rsh
''fi
''PATH="$PATH:/etc:/usr/etc"; export PATH # /usr/etc/rpcinfo
''rpcinfo=rpcinfo prognum=300215 h=`hostname`
''if [ X"$1" = Xlocalhost -o X"$1" = X$h ]; then
''	echo "Attempting local auto-start of the Frame License Server."
''	$FMHOME/bin/fm_fls -log $logfile -cd /tmp $usrdir/licenses &
''else
''	h=$1
''	echo "Attempting remote auto-start of the Frame License Server on host $h."
''	$rshcmd $h -n /bin/ls / > /dev/null 2>&1 || {
''		echo "Failed to access ($rshcmd) remote FLS host $h."; exit 1
''	}
''	$rshcmd $h -n \
''		$FMHOME/bin/fm_fls -log $logfile -cd /tmp $usrdir/licenses &
''	if [ $? != 0 ]; then
''		echo "Failed to access ($rshcmd) remote FLS on host $h."; exit 1
''	fi
''fi
''ok=
''for e in 10 10 20 20; do
''	echo "Please wait a moment while the license server initializes..."
''	sleep $e
''	$rpcinfo -u $h $prognum >/dev/null 2>&1 && { ok=t; break;}
''done
''[ $ok ] && { echo "License server is ready."; exit 0;}
''echo "License server failed."; exit 1
''# LAST-LINE '"$da" > $usrdir/$autofil
	# LAST-LINE must match exactly, or the following will fail.
	# This allows us to detect write failures.
	if grep '^# LAST-LINE '"$da"'$' $usrdir/$autofil >/dev/null 2>&1; then
		chmod 0755 $usrdir/$autofil
	else
		echo "trouble writing $usrdir/$autofil
""(perhaps a remote file system is full?)" >&2
		exit 1
	fi
fi


# Restart server?
echo
if $ASK y n "Would you like to start the license server process on $thishost?"; then
	# Sort out differences between ps versions on UNIX and System V.
	if ps -ef >/dev/null 2>&1; then
		pid=`ps -ef | egrep fm_fls | egrep -v '(grep)' | awk '{print $2}'`
	else
		pid=`ps axww | egrep fm_fls | egrep -v '(grep)' | awk '{print $1}'`
	fi
	if [ "$pid" != "" ]; then
		pid=`echo $pid`
		status=`kill $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 $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 \"ready,\" 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 licenses file, run $script
""again to restart the license server process with the new licensing
""information."
fi
echo "
""$script done."
exit 0
