#! /bin/sh
#
#  Name:
#     lmdlog   script file for starting up a diagnostic log file
#
#  Usage:
#     lmdlog [[-r matlabdir] | [-c licensefile] | -m [marker]] | [-l dlogfile]
#
#  Description:
#     lmdlog starts up a diagnostic logfile independent of the standard
#     log file specified to lmgrd by lmstart/lmboot. If lmdlog is called
#     twice with the same logfile the logging from the first call is lost.
#     To stop logging simply specify /dev/null as the logfile.
#     You do NOT need to be root to run this command. You can start this
#     on any host with access to the SERVER, however the log file is
#     create ON the SERVER NOT the host where you initiated this command.
#     This script will error if you are not on a SERVER.
#
#  Options:
#
#     -r matlabdir
#
#          Provide the MATLAB root directory path. Should be used when
#          MATLAB is in an automounted file system.
#
#     -c licensefile
#
#	   Path of the license file.
#	   DEFAULT: $MATLAB/etc/license.dat
#
#     -m [marker]
#
#	   Check the license file in /usr/tmp/marker.dat on the
#	   current host. This is created by lmstart/lmboot.
#
#	   If marker is not given then it defaults to:  lm_TMW4
#
#     -l logfile
#
#           Path of the log file.
#           DEFAULT: dlog4 (in the current directory)
#
#  Copyright (c) 1993-95 by The MathWorks, Inc.
#  $Revision: 1.6 $  $Date: 1995/05/01 16:11:05 $
#__________________________________________________________________________
#
#**************************************************************************
# 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 '    lmdlog 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
    marker="lm_TMW4"
    markerpath=/usr/tmp
    dlogfile=dlog4
#
    set_licensefile="FALSE"
    arglist=
#
# Parse the arguments
#
#     -r matlabdir
#     -c licensefile
#     -m [marker]
#     -l logfile
#
    stat="OK"
    while [ "$stat" = "OK" -a  $# -gt 0 ]; do
        case "$1" in
            -r)
                shift
                if [ $# -gt 0 ]; then
                    case "$1" in
                        -*)
                            stat=""
                            ;;
                         *)
#
# Minimum check - must be a valid directory
#
                            (cd $1) > /dev/null 2>&1
                            if [ $? -ne 0 ]; then
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo ''
    echo '    Sorry! MATLAB directory provided with -r option does not exist.'
    echo ''
    echo "           MATLAB = $1"
    echo ''
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
                                exit 1
                            elif [ ! -f "$1/etc/lmboot" ]; then
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo ''
    echo '    Sorry! MATLAB directory provided with -r option is bad.'
    echo ''
    echo "           MATLAB = $1"
    echo ''
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
                                exit 1
                            fi
#
                            MATLAB="$1"
                            shift
                            ;;
                    esac
                else
                    stat=""
                fi
                ;;
            -c)
                shift
		if [ "$usermarker" = "1" ]; then
		    stat=""
		elif [ $# -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=""
                            ;;
                         *)
                            dlogfile="$1"
                            shift
                            ;;
                    esac
                else
                    stat=""
                fi
                ;;
            -m)
                shift
		if [ "$set_licensefile" = "TRUE" ]; then
		    stat=""
		else
		    usemarker=1
                    if [ $# -gt 0 ]; then
                        case "$1" in
                            -*)
                                ;;
                             *)
		    	        marker="$1"
		    	        shift
                                ;;
                        esac
		    fi
		fi
                ;;
	     *)
		stat="";
		;;
	esac
    done
    if [ "$stat" = "" ]; then
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   echo ''
   echo '    Usage: lmdlog [[-r matlabdir] | [-c licensefile] | -m [marker]] |'
   echo '                   [-l dlogfile]'
   echo ''
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	exit 1
    fi
#
    if [ $set_licensefile = "FALSE" -a "$usemarker" != "1" -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
    elif [ "$usemarker" = "1" -a ! -f $markerpath/$marker.dat ]; then
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo ''
    echo '    Sorry! License file does not exist on this host.'
    echo ''
    echo "           file = $markerpath/$marker.dat"
    echo ''
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        exit 1
    fi
#
# Determine final license file
#
    if [ $set_licensefile = "FALSE" -a "$usemarker" != "1" ]; then
	   licensefile=$MATLAB/etc/license.dat
    elif [ $set_licensefile = "FALSE" ]; then
 	   licensefile=$markerpath/$marker.dat
    fi
#
# Verify that you are on the right host - if not error
#
# Get the local hostid by calling lmhostid.
# Get the hostid's from the license.dat file and see if one matches
#
   localhostid=`$MATLAB/etc/lmhostid | sed -e "1d" -e '2s/^[^"]*"//' -e '2s/".*//'`
   lmfilehostids=`cat $licensefile | awk '/SERVER/ {print $3}'`
   found=0
   for i in $lmfilehostids
   do
	if [ "$localhostid" = "$i" ]; then
	    found=1
	    break
	fi
   done 
   if [ "$found" = "0" ]; then
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo '------------------------------------------------------------------'
    echo ''
    echo '    Sorry! According to your license.dat file this host'
    echo '           with hostid:'
    echo ''
    echo "               $localhostid"
    echo ''
    echo '           does not match any SERVER hostid. The SERVER lines are'
    echo ''
    echo '-----------------------------------------------'
    grep SERVER $licensefile
    echo '-----------------------------------------------'
    echo ''
    echo '           You must be on one of the SERVER hosts to perform this'
    echo '           license manager operation!'
    echo ''
    echo '------------------------------------------------------------------'
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	exit 1
    fi
#
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo '------------------------------------------------------------------'
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    LM_LICENSE_FILE=$licensefile; export LM_LICENSE_FILE
    $MATLAB/etc/$Arch/lmswitchr MATLAB $dlogfile
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo ''
    echo 'Starting diagnostic logging . . .'
    echo ''
    echo "    diagnostic logfile = $dlogfile"
    echo ''
    echo '------------------------------------------------------------------'
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#
