#! /bin/sh
#
#  Name:
#     lmreread   script file for rereading FLEXlm License Manager license file
#
#  Usage:
#     lmreread [-c licensefile] [-l logfile] [-m marker]
#
#  Description:
#     lmreread allows you to read in a new license file without bringing the
#     license manager daemon down. If the old and new license files don't
#     match in the critical parts then the user is given the option of
#     restarting the daemons with the new license file.
#
#  Options:
#
#     -c licensefile
#
#	    Path of the license file.
#	    DEFAULT: $MATLAB/etc/license.dat
#
#     -l logfile 
#
#	    Path of the log file.
#           DEFAULT: /usr/adm/license.log4  (except sol2)
#                    /usr/sadm/license.log4 (sol2 only)
#
#     -m marker
#
#           Marker string used to build the link names.  The marker string
#           should contain the string 'lm' so you can grep on the output of
#           the 'ps' command using 'lm' as the search string.
#           DEFAULT: lm_TMW4
#
#  Copyright (c) 1992-95 by The MathWorks, Inc.
#  $Revision: 1.12 $  $Date: 1995/05/01 16:13:23 $
#__________________________________________________________________________
#
#**************************************************************************
# Determine the path of the MATLAB root directory - always one directory
# up from the path to this command.
#**************************************************************************
#
    filename=$0
#
# Now it is either a file or a link to a file.
#
    cpath=`pwd`
#
# Follow up to 8 links before giving up. Same as BSD 4.3
#
    n=1
    maxlinks=8
    while [ $n -le $maxlinks ]
    do
#
# Get directory correctly!
#
	newdir=`echo "$filename" | awk '
                        { tail = $0
                          np = index (tail, "/")
                          while ( np != 0 ) {
                             tail = substr (tail, np + 1, length (tail) - np)
                             if (tail == "" ) break
                             np = index (tail, "/")
                          }
                          head = substr ($0, 1, length ($0) - length (tail))
                          if ( tail == "." || tail == "..")
                             print $0
                          else
                             print head
                        }'`
	if [ ! "$newdir" ]; then
	    newdir="."
	fi
	(cd $newdir) > /dev/null 2>&1
	if [ $? -ne 0 ]; then
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo ''
    echo 'Internal error 1: We could not determine the path of the'
    echo '                  MATLAB root directory.'
    echo ''
    echo "                  original command path = $0"
    echo "                  current  command path = $filename"
    echo ''
    echo '                  Please contact:'
    echo '' 
    echo '                      MathWorks Technical Support'
    echo ''
    echo '                  for further assistance.'
    echo ''
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	    exit 1
	fi
	cd $newdir
#
# Need the function pwd - not the built in one
#
	newdir=`/bin/pwd`
#
	newbase=`expr //$filename : '.*/\(.*\)' \| $filename`
        lscmd=`ls -l $newbase 2>/dev/null`
	if [ ! "$lscmd" ]; then
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo ''
    echo 'Internal error 2: Could not determine the path of the'
    echo '                  MATLAB root directory.'
    echo ''
    echo "                  original command path = $filename"
    echo "                  current  command path = $filename"
    echo ''
    echo '                  Please contact:'
    echo '' 
    echo '                      MathWorks Technical Support'
    echo ''
    echo '                  for further assistance.'
    echo ''
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	    exit 1
	fi
#
# Check for link portably
#
	if [ `expr "$lscmd" : '.*->.*'` -ne 0 ]; then
	    filename=`echo "$lscmd" | awk '{ print $NF }'`
	else
#
# It's a file
#
	    dir="$newdir"
	    command="$newbase"
#
	    cd $dir/..
	    MATLAB=`/bin/pwd`; export MATLAB
	    break
	fi
        n=`expr $n + 1`
    done
    if [ $n -gt $maxlinks ]; then
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo ''
    echo 'Internal error 3: More than $maxlinks links in path to'
    echo "                  this script. That's too many!"
    echo ''
    echo "                  original command path = $filename"
    echo "                  current  command path = $filename"
    echo ''
    echo '                  Please contact:'
    echo '' 
    echo '                      MathWorks Technical Support'
    echo ''
    echo '                  for further assistance.'
    echo ''
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	exit 1
    fi
#	
    cd $cpath
#
#**************************************************************************
#
    . $MATLAB/etc/util/arch.sh
    if [ "$Arch" = "unknown" ]; then
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo ''
    echo '    Sorry! We could not determine the machine architecture for your'
    echo '           host. Please contact:'
    echo '' 
    echo '               MathWorks Technical Support'
    echo ''
    echo '           for further assistance.'
    echo ''
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    elif [ "$Arch" = "lnx86" ]; then
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo ''
    echo '    lmreread is not supported or required on Linux.'
    echo ''
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        exit 1
    fi
        exit 1
#
    (cd $MATLAB/etc/$Arch) > /dev/null 2>&1
    if [ $? -ne 0 ]; then
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo ''
    echo "    Sorry! License manager directory MATLAB/etc/$Arch does not exit."
    echo ''
    echo "           MATLAB = $MATLAB"
    echo ''
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        exit 1
    fi
#
    licensefile=$MATLAB/etc/license.dat
    if [ $Arch != "sol2" ]; then
        lmlogfile=/usr/adm/license.log4
    else
        lmlogfile=/usr/sadm/license.log4
    fi
    marker="lm_TMW4"
#
    set_licensefile="FALSE"
#
# Parse the arguments
#
#     -c licensefile
#     -l logfile
#     -m marker
#
    stat="OK"
    while [ "$stat" = "OK" -a  $# -gt 0 ]; do
        case "$1" in
            -c)
                shift
		if [ $# -gt 0 ]; then
                    case "$1" in
                        -*)
                            stat=""
                            ;;
                         *)
                            licensefile="$1"
                            shift
                            set_licensefile="TRUE"
                            ;;
                    esac
		else
		    stat=""
		fi
                ;;
            -l)
                shift
		if [ $# -gt 0 ]; then
                    case "$1" in
                        -*)
                            stat=""
                            ;;
                         *)
		    	    lmlogfile="$1"
		    	    shift
                            ;;
                    esac
		else
		    stat=""
		fi
                ;;
            -m)
                shift
		if [ $# -gt 0 ]; then
                    case "$1" in
                        -*)
                            stat=""
                            ;;
                         *)
		    	    marker="$1"
		    	    shift
                            ;;
                    esac
		else
		    stat=""
		fi
                ;;
	     *)
		stat="";
		;;
	esac
    done
    if [ "$stat" = "" ]; then
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   echo ''
   echo '    Usage: lmreread [-c licensefile] [-l logfile] [-m marker]'  
   echo ''
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	exit 1
    fi
#
    if [ $set_licensefile = "FALSE" -a ! -f $MATLAB/etc/license.dat ]; then
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo ''
    echo '    Sorry! No license.dat file found in MATLAB/etc directory.'
    echo ''
    echo "           MATLAB = $MATLAB"
    echo ''
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        exit 1
    elif [ $set_licensefile = "TRUE" -a ! -f $licensefile ]; then
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo ''
    echo '    Sorry! License file does not exist.'
    echo ''
    echo "           file = $licensefile"
    echo ''
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        exit 1
    fi
#
    (cat /dev/null >> $lmlogfile) 2>/dev/null
    if [ $? -ne 0 ]; then
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo ''
    echo '    Sorry! Cannot write in log file.'
    echo ''
    echo "           file = $lmlogfile"
    echo ''
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        exit 1
    fi
#
# Check for /usr/tmp/$marker.dat file - if one does not exist then
# we assume that the hosts on the SERVER lines of the $licensefile
# are the correct ones to contact to reread the ORIGINAL license file.
# 
    if [ ! -f /usr/tmp/$marker.dat ]; then
#
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo ''
    echo '    NOTE: Not running on a license server . . .'
    echo '          Using CURRENT license file:'
    echo ''
    echo "    licensefile = $licensefile"
    echo ''
    echo '    with server lines:'
    echo ''
    echo '-----------------------------------------------'
    grep '^SERVER' $licensefile
    echo '-----------------------------------------------'
    echo ''
    echo '    Will contact those servers to read ORIGINAL license file . . .'
    echo ''
    echo '    If you changed the license file path or any DAEMON option'
    echo '    file path or any information in the option file(s) you MUST'
    echo '    run lmstart on each license SERVER to update the'
    echo '    license manager.'
    echo '' 
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#
    else
#
# Get critical parts of old and new license files and compare. The content
# of the SERVER lines must be the same and in the same order. Otherwise,
# prompt the user to restart the daemons with the new license file.
#
        cat /usr/tmp/$marker.dat | awk '
		/^SERVER/ { print $1, $2, $3 }
			  {next}' > /tmp/$$a
        cat $licensefile | awk '
		/^SERVER/ { print $1, $2, $3 }
			  {next}' > /tmp/$$b
        diff /tmp/$$a /tmp/$$b > /tmp/$$c
        nl=`wc -l /tmp/$$c | awk '{ print $1 }'`
        nl=`expr "$nl" + 0`
        if [ $nl -ne 0 ]; then
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo ''
    echo "    Sorry! You are on a license server BUT you can't use lmreread."
    echo '           Critical parts of ORIGINAL license file are different than'
    echo '           the CURRENT license file. That is:'
    echo ''
    echo '    from ORIGINAL license file - (to be read again)'
    echo '-----------------------------------------------'
    cat /tmp/$$a
    echo '-----------------------------------------------'
    echo ''
    echo '    from CURRENT license file -'
    echo '-----------------------------------------------'
    cat /tmp/$$b
    echo '-----------------------------------------------'
    echo ''
    echo '           The server lines must be the same and in the same order.'
    echo ''
    echo '           If the CURRENT license file is the correct then run'
    echo '           lmstart on each license SERVER to update the license manager.'
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	    rm -f /tmp/$$a /tmp/$$b /tmp/$$c > /dev/null 2>&1
#
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo ''
    echo '    Nothing done . . .'
    echo ''
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	    exit 1
	else
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo ''
    echo '    NOTE: Rereading the license file specified at lmstart . . .'
    echo ''
    echo '    If you changed the license file path or any DAEMON option'
    echo '    file path or any information in the option file(s) you MUST'
    echo '    run lmstart on each license SERVER to update the'
    echo '    license manager.'
    echo ''
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	fi
    fi
#
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo '------------------------------------------------------------------'
#
    $MATLAB/etc/$Arch/lmreread -c $licensefile
#
    if [ $? -ne 0 ]; then

#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo ''
    echo '    BAD STATUS! reread did not work properly . . .'
    echo ''
    echo '                Check any messages above and in the logfile on'
    echo '                license server host(s).'
    echo ''
    echo '------------------------------------------------------------------'
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        exit 1
    fi
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo '------------------------------------------------------------------'
#
    exit 0
