#!/bin/sh

###############################################################
# "Advantedge_license"
#
# Ultimatte AdvantEdge Plug-Ins 
# Starts and/or stops the FLEXlm license manager
#
# 14-May-97 DG Manpearl Create
# 16-May-97 DG Manpearl Convert from csh to sh
# 08-Jul-98 DG Manpearl Port for Spark support.
# 27-Apr-00 DG Manpearl Migrate from ult_dl_license.
# 10-Jul-02 G  Colombo  Changes for AdvantEdge
###############################################################

###############################################################
# Variables
###############################################################

PRODUCT="Ultimatte AdvantEdge Plug-Ins"
LICENSE_PATH="/usr/local/Ultimatte/AdvantEdge"
TOOLS_PATH="$LICENSE_PATH/flexlm"
LICENSE="$LICENSE_PATH/Advantedge.lic"

LOG_FILE="$TOOLS_PATH/Advantedge_lic.log"

LMGRD="$TOOLS_PATH/lmgrd"
LMUTIL="$TOOLS_PATH/lmutil"
LMDOWN="$LMUTIL lmdown"
LMDIAG="$LMUTIL lmdiag"
LMSTAT="$LMUTIL lmstat"
usage="usage: $0 [start|stop|both|diag|stat|help]\n"
start=0
stop=0
diag=0
stat=0

###############################################################
# Hello
###############################################################
echo "\n$PRODUCT License Management"
echo " 1997-2003 Ultimatte Corp. All rights reserved"

###############################################################
# Check that there is exactly one argument
###############################################################
if test $# -ne 1; then
    echo "$usage";
    exit 1;
fi

###############################################################
# Determine Start, Stop, or Both
###############################################################
case "$1" in
    'start')
	start=1
	;;
    'stop')
	stop=1
	;;
    'both')
	start=1
	stop=1
	;;
    'diag')
	diag=1
	;;
    'stat')
	stat=1
	;;
    'help')
	echo "Help Screen"
	echo "\n   Advantedge_license start:"
	echo "      Starts the $PRODUCT "
	echo "      FLEXlm floating license server by executing:"
	echo "         $TOOLS_PATH/lmgrd -c license.dat"
	echo "      from within the directory:"
	echo "         $LICENSE_PATH\n"
	echo "      Note:  If there is any chance that a license "
	echo "      server is already running with another or "
	echo "      an invalid license, you must stop the license "
	echo "      manager first with 'Advantedge_license stop'."
	echo "\n   Advantedge_license stop:"
	echo "      Stops the $PRODUCT "
	echo "      FLEXlm floating license server by executing:"
	echo "         $TOOLS_PATH/lmutil lmdown -c license.dat"
	echo "      from within the directory:"
	echo "         $LICENSE_PATH\n"
	echo "      Note:  Run this command before starting the "
	echo "      license manager with 'Advantedge_license start'."
	echo "\n   Advantedge_license both:"
	echo "      Stops and then Starts the Ultimatte AdvantEdge Plug-Ins "
	echo "      FLEXlm floating license server."
	echo "\n   Advantedge_license diag:"
	echo "      Executes the FLEXlm utility 'lmdiag' to list"
	echo "      diagnostics of the Ultimatte AdvantEdge license file."
	echo "\n   Advantedge_license stat:"
	echo "      Executes the FLEXlm utility 'lmstat' to list"
	echo "      statistics of the Ultimatte AdvantEdge license file."
	echo "\n   Advantedge_license help:  Displays this help screen\n"
	echo "Done\n"
	exit 1
	;;
    *)
	echo "$usage";
	exit 1;
	;;
esac

###############################################################
# Check for the license directory
###############################################################
if test ! -d $LICENSE_PATH; then
    echo "Error: License directory not found\n\t$LICENSE_PATH\nDone\n"
    exit 1
fi

###############################################################
# Check for the license file
###############################################################
if test ! -r $LICENSE; then
    echo "Error: License file not found\n\t$LICENSE\nDone\n"
    exit 1
fi

###############################################################
# Check for license manager daemon
###############################################################
if test $start -eq 1; then
    if test ! -r $LMGRD; then
	echo "Error: License daemon lmgrd not found\n\t$LMGRD\nDone\n"
	exit 1
    fi
fi

###############################################################
# Check for license utilities
###############################################################
if test $stop -eq 1; then
    if test ! -r $LMUTIL; then
	echo "Error: License ultilities lmutil not found\n\t$LMUTIL\nDone\n"
	exit 1
    fi
fi

###############################################################
# Stop the license manager
###############################################################
if test $stop -eq 1; then
    $LMDOWN -c $LICENSE
    sleep 1
    echo "Ultimatte AdvantEdge License Management: Stop command executed"
fi

###############################################################
# Start the license manager
###############################################################
if test $start -eq 1; then
    if test `whoami` = "root"; then
	/bin/su guest -c "$LMGRD -c $LICENSE" 2>&1 >>$LOG_FILE
#	/bin/su guest -c "$LMGRD -c $LICENSE >>$LOG_FILE 2>&1"
    else
	$LMGRD -c $LICENSE 2>&1 >>$LOG_FILE
#	$LMGRD -c $LICENSE >>$LOG_FILE 2>&1
    fi
    sleep 1
    echo "Ultimatte AdvantEdge License Management: Start command executed"
fi

###############################################################
# Check Diagnostics
###############################################################
if test $diag -eq 1; then
    $LMDIAG -c $LICENSE
    sleep 1
    echo "Ultimatte AdvantEdge License Management: Diagnostics command executed"
fi

###############################################################
# Check Status
###############################################################
if test $stat -eq 1; then
    $LMSTAT -c $LICENSE
    sleep 1
    echo "Ultimatte AdvantEdge License Management: Status command executed"
fi

echo "Done\n"
