#!/bin/sh
#  Copyright (c) 1989-1996 Adobe Systems, Incorporated. All rights reserved.
#
# fmaddlicense
#

: ${FMHOME:?} ${FMARCH:=`$FMHOME/bin/scripts/fmarch`}

binary=$FMHOME/bin/$FMARCH/`basename $0`
ASK=$FMHOME/bin/scripts/fm_ask
FMREGCARD=$FMHOME/bin/fmregcard
FLSADM=$FMHOME/bin/fmflsadm

backup=/tmp/licfile$$
TF=/tmp/fmadd$$
trap "rm -f $TF" 0
trap 'exit 1' 1 2 3 15

# some platforms don't get "echo -n" right.
ecs= ecf=
case `echo -n` in
*-n*) ecf='\c';;
*) ecs='-n';;
esac

# Add check if on non-SVR4 machine then use hostname instead of uname -n
case `$FMHOME/bin/scripts/fmarch` in
sunxm.s5.sparc|moto.s5.m88k|sgi.irix.mips) curhost=`uname -n` ;;
*) curhost=`hostname` ;;
esac

: ${FM_FLS_HOST:=localhost}
if [ X"$FM_FLS_HOST" = Xlocalhost -o X"$FM_FLS_HOST" = X$curhost ]; then
	server="the current machine";
else
	server=$FM_FLS_HOST
fi

reinit=y
option= licfile=

for arg in ${1+"$@"}; do
	case $arg in
	-mc)
		# This is a placeholder passed from fmsetupfls, in case
		# a special disambiguator is needed for some patch.
		;;
	-noreinit)
		reinit=n
		;;
	-*)
		option=$arg
		;;
	*)
		[ -z "$licfile" ] && licfile=$arg
		;;
	esac
done

if [ -z "$licfile" ]; then
	if [ -f ./licenses ]; then
		licfile=`pwd`/licenses
	elif [ X"$HOME" = X/ ]; then
		licfile=$FMHOME/fminit/licenses
	else
		licfile=$HOME/fminit/licenses
	fi
fi
licdir=`echo $licfile | sed 's%\(.*/\).*%\1%'`

if [ ! -f $licfile ]; then
	touch $backup
else
	cp $licfile $backup
fi
> $TF

$binary -t $TF $option $licfile || exit 1

if [ X"$reinit" = Xn ] || cmp $backup $licfile >/dev/null 2>&1; then
	:
elif $FLSADM ping >/dev/null 2>&1 \
		&& echo "If you have modified the licenses file used to initialize" \
		&& echo "the license server on $server, you may choose to update" \
		&& echo "the server with the new information now.  This will have" \
		&& echo "no effect if only personal licenses have been modified." \
		&& echo  \
		&&  $ASK y n "Reinitialize the license server on $server now?"; then
	case `$FLSADM reinit 2>&1` in
	*success*)
		echo "License server successfully reinitialized."
		;;
	*unchanged*)
		echo "The licenses file that the license server on $server"
		echo "was started with has not changed; you probably need to"
		echo "restart the license server with the new licenses file"
		echo "($licfile)."
		;;
	*"no licenses"*)
		echo "License server reinitialized with 0 licenses!"
		echo "You probably want to run this script again and add some."
		;;
	*"does not support"*)
		echo "The license server on $server is an older version that does"
		echo "not support reinitialization.  You should restart it instead,"
		echo "and consider running a newer version."
		;;
	*)
		echo "There was an unexpected problem trying to reinitialize"
		echo "the license server.  You should try restarting it instead."
		;;
	esac
fi

[ -s "$TF" ] && $FMREGCARD -list $TF
exit 0	# fmregcard optional, so ignore its exit code
