#!/bin/sh
#  Copyright (c) 1989-1997 Adobe Systems, Incorporated. All rights reserved.
#
# fmsetupfls
#
version=5.5
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
if [ -f /bin/hostname ]; then h=`hostname`; else h=`/bin/uname -n`; fi
thishost=$h
ASK=$FMHOME/bin/scripts/fm_ask
OVW=$FMHOME/bin/scripts/fm_overwrite
umask 022

# some platforms don't get "echo -n" right.  So we set these variables,
# and use "echo $ecs foobar $ecf" instead.  Sigh.
ecs= ecf=
case `echo -n` in
*-n*) ecf='\c';;
*) ecs='-n';;
esac

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, FrameMaker 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 Adobe FrameMaker 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 search 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
}

dir=
until [ -n "$dir" ]; do
	echo $ecs \
	"Name of the directory into which the license server should write
""usage logs [$logdir]:" $ecf
	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
	[ -d "$dir" ] || {
		$ASK n y \
		 "$dir doesn't exist or is not a directory (use this name anyway)?" \
		 && dir=
		continue
	}
	[ -w "$dir" ] || {
		$ASK n y "You don't have permission to create files in directory""
""$dir (use this name anyway)?" && dir=
		continue
	}
done
logfile=$dir/fm_fls.log
>$logfile && chmod 666 $logfile

# 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 \
 "# environment configuration (/bin/sh) for Adobe FrameMaker product licensing.""
""FM_FLS_HOST=$thishost""
""FM_FLS_AUTO=$Prtdir/$autofil""
""export FM_FLS_HOST FM_FLS_AUTO" > $usrdir/$envfil
	echo \
 "# environment configuration (/bin/csh) for Adobe FrameMaker product licensing.""
""setenv FM_FLS_HOST $thishost""
""setenv FM_FLS_AUTO $Prtdir/$autofil" > $usrdir/$envfilcsh
fi

# now pick up those environment values we just wrote out.
. $usrdir/$envfil

# create autostart script
echo "Creating $usrdir/$autofil"
echo "    (script to auto-start the Adobe FrameMaker Products License Server)"
case $usrdir in
$FMHOME/*)	# force it to be here.
	setfmhome='FMHOME="'$FMHOME'"'
	;;
*)			# default it here.
	setfmhome=': ${FMHOME:="'$FMHOME'"}'
	;;
esac
if $OVW $usrdir/$autofil; then
	da=`date`
	echo \
 '#!/bin/sh
''# '"$autofil written $da by ${USER:-$LOGNAME}"'
''logfile="'"$logfile"'"
''usrdir="'"$usrdir"'"
'"$setfmhome"'; export FMHOME
''PATH="$PATH:/etc:/usr/etc:/usr/sbin"; export PATH # for rpcinfo
''rpcinfo=rpcinfo prognum=300215
''# hostname gives better results on non-SVR4 machines.
''case `$FMHOME/bin/scripts/fmarch` in
''sunxm.s5.sparc|moto.s5.m88k|sgi.irix.mips) curhost=`uname -n`;;
''*) curhost=`hostname` ;;
''esac
''if [ X"$1" = Xlocalhost -o X"$1" = X$curhost ]; then
''	h=localhost
''	echo "Attempting local auto-start of the Adobe FrameMaker Products License Server."
''	$FMHOME/bin/fm_fls -log $logfile -cd /tmp $usrdir/licenses || {
''		echo "Could not start $FMHOME/bin/fm_fls on local host." >&2
''		exit 1
''	}
''else
''	h=$1
''	echo "Attempting remote auto-start of the Adobe FrameMaker Products License Server on host $h."
''	# How System V spells "rsh"
''	if [ -f /usr/bin/remsh ]; then
''		rshcmd=/usr/bin/remsh
''	else
''		rshcmd=rsh
''	fi
''	# Make sure we can execute remote commands.
''	$rshcmd $h -n /bin/sh -c : > /dev/null 2>&1 || {
''		echo "Unable to access ($rshcmd) remote FLS host $h."; exit 1
''	}
''	# Run the command, and check its exit status.  This is complicated
''	# by inconsistent rsh behavior. 
''	rtest="KR$$RK"
''	ok=`$rshcmd $h -n \
''		$FMHOME/bin/fm_fls -log $logfile -cd /tmp $usrdir/licenses "&&" \
''			echo $rtest`
''	case "$ok" in *"$rtest") ;;
''	*)
''		echo "Could not start $FMHOME/bin/fm_fls on host $h." >&2
''		exit 1
''		;;
''	esac
''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
''[ X"$ok" = Xt ] && { 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

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 0

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

# 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 different platforms.
	pid=""
	case $FMARCH in
	*.os.*)
		pid=`ps axww | egrep fm_fls | egrep -v '(grep)' | awk '{print $1}'`
		;;
	*.s5.*|hp*)
		if ps -ef >/dev/null 2>&1; then
			pid=`ps -ef | egrep fm_fls | egrep -v '(grep)' | awk '{print $2}'`
		else
			break
		fi
		;;
	ibm*|sgi*)
		pid=`ps -ea | egrep fm_fls | egrep -v '(grep)' | awk '{print $1}'`
		;;
	esac
	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 Adobe FrameMaker Products software.  
""If the registration number is incorrect for the information you have 
""entered, please run $script again and enter the information exactly 
""as you obtained it from Adobe Systems, Incorporated or your reseller."
fi
echo "
""$script $version done."
exit 0
