#!/bin/sh
# rds 19 February 2007
# Script to recreate lmf db & allow user to install licenses

test `uname -s` = Linux && echocmd="echo -e" || echocmd="echo"

$echocmd "\nThis script will remove the existing LMF license database"
$echocmd "and prompt you to install your LMF licenses to a new database."


if test -f ../etc/cobver
then
	$echocmd "\nThis script cannot be run from this location ($COBDIR/lmf)."
	$echocmd "You must run this script from the location where License Manager is installed."
	exit 1
fi


if test -f ../cobver
then
	$echocmd "\nThis script cannot be run from this location ($COBDIR/lmf)."
	$echocmd "You must run this script from the location where License Manager is installed."
	exit 1
fi


if test ! -f mflm_manager
then
	$echocmd "\nThis script cannot be run from this location."
	$echocmd "You must run this script from the location where License Manager is installed."
	exit 1
fi

if test ! -w /etc/passwd
then
	$echocmd "\nYou must be running as root to execute this script."
	exit 1
fi


YN="x"
while [ "$YN" != "y" -a "$YN" != "Y" -a "$YN" != "N" -a "$YN" != "n" ]
do
	$echocmd "\nDo you wish to continue? (y/n)"
	read YN
done

if [ "$YN" = "N" -o "$YN" = "n" ]
then
	$echocmd "\nlmfdbrecover exiting with no action."
	exit 0
fi

unset COBPATH
unset COBDATA
unset COBSW
COBDIR=`pwd`



export COBDIR
LIBPATH=$COBDIR/lib:/lib:$LIBPATH
export LIBPATH
LD_LIBRARY_PATH=$COBDIR/lib:/lib:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH
SHLIB_PATH=$COBDIR/lib:/lib:$SHLIB_PATH
export SHLIB_PATH

rm -f mflmfdb*
	./bin/rts32 lmfnewdb

$echocmd "\nLicense db recovery utility (lmfdbrecover) starting."
MN="0"
while [ "$MN" != "1" -a "$MN" != "2" -a "$MN" != "3" ]
do
	$echocmd "\n1. Text Mode license install"
	$echocmd "2. Screen Mode license install"
	$echocmd "3. Exit with no license install"
	read MN
done

if [ "$MN" = "3" ]
then
        $echocmd "\nYou have chosen to exit lmfdbrecover without installing"
        $echocmd "any licenses."
        $echocmd "The LMF license databse is currently empty." 
        $echocmd "You can install licenses either by running lmfdbrecover"
        $echocmd "again, or by running mflmadm or mflmcmd directly."
	exit 0
fi


if [ "$MN" = "2" ]
then
	$echocmd "\n"
	sh mflmadm
	$echocmd "\nlmfdbrecover complete"
	exit 0
fi

YN="y"
while [ "$YN" = "y" -o "$YN" = "Y" ]
do
	
	sh mflmcmd
	$echocmd "\nInstall another license? (y/n)"
	read YN

done

$echocmd "\nlmfdbrecover complete."
exit 0
