#! /bin/sh
#
#  Name:
#     lmstat   script file for invoking FLEXlm (tm) License Manager
#	       Statistician
#
#  Usage:
#     lmstat [-a] [-A] [-c licensefile] [-f [feature]]
#	     [-l [regular_expression]] [-s [server]]
#	     [-S [daemon]] [-t timeout]
#
#  Description:
#     lmstat reports a variety of useful information concerning the current
#     usage of MATLAB. All the arguments and their descriptions are
#     taken directly from the FLEXlm (tm) program lmstat.
#
#  Options:
#
#     -a
#	    Display everything.
#
#     -A
#	    List all active licenses.
#
#     -c licensefile
#
#	    Path of the license file.
#	    DEFAULT: $MATLAB/etc/license.dat
#
#     -f [feature]
#
#	    List all users of the specified features(s).
#
#     -l [regular_expression]
#
#	    List all users of the features matching the given
#	    regular expression.
#
#     -s [server]
#
#	    Display the status of the specified server node(s).
#
#     -S [daemon]
#
#	    List all users of the specified daemon's features.
#
#     -t timeout
#
#	    Specifies the timeout interval, in seconds, during which
#	    daemons must complete their connections to each other. The
#	    default value is 10 seconds. A larger value may be
#	    desirable if the daemons are being run on busy systems
#	    or a very heavily loaded network.
#
#  Copyright (c) 1986-95 by The MathWorks, Inc.
#  $Revision: 1.10 $  $Date: 1995/05/01 16:14:14 $
#__________________________________________________________________________
#
#**************************************************************************
# 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 ''
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        exit 1
    elif [ "$Arch" = "lnx86" ]; then
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo ''
    echo '    lmstat is not supported or required on Linux.'
    echo ''
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        exit 1
    fi
#
    (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
#
    set_licensefile="FALSE"
#
# Parse the arguments
#
#     -a
#     -A
#     -c licensefile
#     -f [feature]
#     -l [regular_expression]   WATCH OUT! This must be quoted!
#     -s [server]
#     -S [daemon]
#     -t timeout
#
    stat="OK"
    quote="FALSE"
    arglist=
    while [ "$stat" = "OK" -a $# -gt 0 ]; do
        case "$1" in
            -a|-A)
		:
		;;
            -c)
                arglist="$arglist $1"
                shift
		if [ $# -gt 0 ]; then
		    case "$1" in
		        -*)
		    	    stat=""
			    ;;
		         *)
		    	    licensefile="$1"
		    	    set_licensefile="TRUE"
			    ;;
		    esac
		else
		    stat=""
		fi
                ;;
            -f|-s|-S)
                arglist="$arglist $1"
                shift
		if [ $# -gt 0 ]; then
		    case "$1" in
		        -*)
			    continue
			    ;;
		         *)
		    	    :
			    ;;
		    esac
		fi
                ;;
            -l)
                arglist="$arglist $1"
                shift
		if [ $# -gt 0 ]; then
		    case "$1" in
		        -*)
			    continue
			    ;;
		         *)
			    quote="TRUE"
			    ;;
		    esac
		fi
                ;;
            -t)
                arglist="$arglist $1"
                shift
		if [ $# -gt 0 ]; then
		    case "$1" in
		        -*)
		    	    stat=""
			    ;;
		         *)
			    :
			    ;;
		    esac
		else
		    stat=""
		fi
                ;;
	     *)
		stat=""
		;;
	esac
#
	if [ $# -gt 0 ]; then
#
# Properly quote the arguments
#
	    if [ $quote = "TRUE" ]; then
	   	if [ `expr "$1" : ".*'.*" \| "$1" : '.*".*'` -gt 0 ]; then
      		    arglist="$arglist ""`echo "$1" | awk '
        	    { squote = sprintf ("%c", 39)   # set single quote
          	    dquote = sprintf ("%c", 34)   # set double quote
          	    newarg=squote                 # initialize output string to
                                        	  # single quote
          	    lookquote=squote              # look for single quote
          	    oldarg = $0
          	    while ((i = index (oldarg, lookquote))) {
                    newarg = newarg substr (oldarg, 1, i - 1) lookquote
                    oldarg = substr (oldarg, i, length (oldarg) - i + 1)
                    if (lookquote == squote)
                        lookquote = dquote
                    else
                        lookquote = squote
                    newarg = newarg lookquote
          	    }
          	    print newarg oldarg lookquote }'`"
   	        else
                    arglist="$arglist ""'$1'"
   	        fi
   	        shift
	        quote="FALSE"
	    else
                arglist="$arglist $1"
   	        shift
	    fi
	fi
    done
#
    if [ "$stat" = "" ]; then
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   echo ''
   echo '    Usage: lmstat [-a] [-A] [-c licensefile] [-f [feature]]'
   echo '                  [-l [regular_expression]] [-s [server]]'
   echo '                  [-S [daemon]] [-t timeout]'
   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
#
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo '------------------------------------------------------------------'
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

    if [ $set_licensefile = "TRUE" ]; then
        eval $MATLAB/etc/$Arch/lmstat $arglist
    else
        eval $MATLAB/etc/$Arch/lmstat -c $licensefile $arglist
    fi
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo '------------------------------------------------------------------'
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
