#! /bin/sh
#
#  Name:
#     lmboot	script file for starting the FLEXlm License Manager at
#		boot time
#     
#  Usage:
#     lmboot [-r matlabdir]
#            [[-s] | [[-c licensefile] [-d] [-l logfile] [-m marker]]]
#
#   Description:
#     lmboot when called will attempt to start up the license manager
#     in a way that allows for lmdown to take down the license manager
#     reliably. It is designed to be flexible and allow for inclusion of
#     vendor daemons other than for just MATLAB.
#
#     IMPORTANT: To include vendor daemons other than MATLAB REQUIRES
#		 modification of this script. Locate the string
#		 'ADD OTHER DAEMONS HERE' below to place the changes.
#		 More information is in the file:
#
#			MATLAB/etc/README.boot
#
#     Let $MATLAB be the MATLAB root directory path and $Arch be the machine
#     architecture and using the marker it creates the following link:
#
#	ln -s $MATLAB/etc/$Arch/lmgrd	/usr/tmp/marker.ld
#
#     Then it makes a copy of the license file 
#
#	cp -p licensefile		/usr/tmp/marker.dat
#	
#     Now given a license file of the form:
#
#----------------------------------------------------------------------------
#     #. . .
#     SERVER . . .
#     SERVER . . .
#     . . .
#     DAEMON name1 daemonfile optionsfile
#     DAEMON name2 daemonfile optionsfile
#     . . .
#     FEATURE . . .
#     . . .
#----------------------------------------------------------------------------
#
#     it creates a license file of the form
#      
#----------------------------------------------------------------------------
#     #. . .
#     SERVER . . .
#     SERVER . . .
#     . . .
#     DAEMON name1 /usr/tmp/marker.vd1 optionsfile
#     DAEMON name2 /usr/tmp/marker.vd2 optionsfile
#     . . .
#     FEATURE . . .
#     . . .
#----------------------------------------------------------------------------
#
#     This requires links to me made.  The FORM of the links are critical!
#     They must access the actual executable daemons, NOT scripts! This is
#     so that the entries in the process table get the proper names. So for
#     example,
#
#     on the sun4 we might have
#
#     name1 = MLM then
#
#	ln -s $MATLAB/etc/sun4/lm_matlab  	/usr/tmp/marker.vd1
#
#     name2 = PVI 
#     
#       ln -s $PVI/license/bin/bin.sun4/PVI	/usr/tmp/marker.vd2
#
#     . . .
#
#     It is recommended that you use the Arch variable to make the
#     the code more platform independent. The code is clearly marked
#     below where to change it.
#
#  Options:
#
#     -r matlabdir
#
#	   Provide the MATLAB root directory path when MATLAB is in an
#	   automounted file system. matlabdir/etc/lmboot MUST be this
#	   script. matlabdir MUST cause a mount to occur!
#
#     -s
#
#	   Create a symbolic link /etc/lmboot -> $MATLAB/etc/lmboot ONLY.
#	   Only the -r option may be used with -s.
#
#     -c licensefile
#
#	    Path of the license file.
#	    DEFAULT: $MATLAB/etc/license.dat
#
#     -d
#
#	    The hostnames that are read from the license file should have
#	    the domain name appended to them before sending to a client.
#	    IMPORTANT: This means that the license file used to start up the
#	    	       the license manager has hostnames in the server lines
#		       WITHOUT full domain names while the license file used
#		       when running MATLAB has hostnames in the server lines
#		       WITH full domain names. You must use TWO license files.
#
#     -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:08:40 $
#__________________________________________________________________________
#
#**************************************************************************
# 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 '    lmboot 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
    if [ $Arch != "sol2" ]; then
        lmlogfile=/usr/adm/license.log4
    else
        lmlogfile=/usr/sadm/license.log4
    fi
    marker="lm_TMW4"
#
    set_licensefile="FALSE"
    set_linkonly="FALSE"
    set_other="FALSE"
#
# Parse the arguments
#
#     -r matlabdir
#     -s /etc/lmboot link
#     -c licensefile
#     -d
#     -l logfile
#     -m marker
#
#     NOTE: -r can go with -s XOR -c, -l, -m, -d
#
    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 and have lmboot file in it
#
			    (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
                ;;
	    -s)
		shift
		set_linkonly="TRUE"
		if [ "$set_other" = "TRUE" ]; then
		   stat=""
		fi
		;;
            -c)
                shift
		set_other="TRUE"
		if [ "$set_linkonly" = "TRUE" ]; then
		   stat=""
		fi
		if [ $# -gt 0 ]; then
                    case "$1" in
                        -*)
                            stat=""
                            ;;
                         *)
                            licensefile="$1"
			    shift
                            set_licensefile="TRUE"
                            ;;
                    esac
		else
		    stat=""
		fi
                ;;
	    -d)
		shift
		arglist="$arglist -d"
		;;
            -l)
                shift
		set_other="TRUE"
		if [ "$set_linkonly" = "TRUE" ]; then
		   stat=""
		fi
		if [ $# -gt 0 ]; then
                    case "$1" in
                        -*)
                            stat=""
                            ;;
                         *)
		    	    lmlogfile="$1"
		    	    shift
                            ;;
                    esac
		else
		    stat=""
		fi
                ;;
            -m)
                shift
		set_other="TRUE"
		if [ "$set_linkonly" = "TRUE" ]; then
		   stat=""
		fi
		if [ $# -gt 0 ]; then
                    case "$1" in
                        -*)
                            stat=""
                            ;;
                         *)
		    	    marker="$1"
		    	    shift
                            ;;
                    esac
		else
		    stat=""
		fi
                ;;
	    *)
		stat="";
		;;
	esac
    done
    if [ "$stat" = "" ]; then
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   echo ''
   echo '    Usage: lmboot [-r matlabdir]'
   echo '                  [[-s] | [[-c licensefile] [-d] [-l logfile] [-m marker]]]'
   echo ''
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	exit 1
    fi
    if [ "$set_linkonly" = "TRUE" ]; then
	if [ ! -f $MATLAB/etc/lmboot ]; then
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo ''
    echo '    Sorry! No lmboot script file found in MATLAB/etc directory.'
    echo ''
    echo "           MATLAB = $MATLAB"
    echo ''
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	    exit 1
	else
	    rm -rf /etc/lmboot > /dev/null 2>&1
	    ln -s $MATLAB/etc/lmboot /etc/lmboot
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo ''
    echo '    /etc/lmboot -> $MATLAB/etc/lmboot link created . . .'
    echo ''
    echo "           MATLAB = $MATLAB"
    echo ''
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	    exit 0
	fi
    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
#
# Create temporary license.dat file by copying and modifying the original file
#
    rm -f /usr/tmp/$marker.dat > /dev/null 2>&1
    rm -f /usr/tmp/$marker.vd* > /dev/null 2>&1
    ndaemon=0;
    (while read line
     do
	field1=`echo $line | awk '{print $1}'`
        if [ "$field1" = "DAEMON" ]; then
	    daemonName=`echo $line | awk '{print $2}'` # field 2
	    daemonPath=`echo $line | awk '{print $3}'` # field 3
	    ndaemon=`expr $ndaemon + 1`
	    case "$daemonName" in
		MLM)	# Always assume $MATLAB/etc/$Arch/lm_matlab
			# Must use the binary - NO SCRIPTS
		    ln -s $MATLAB/etc/$Arch/lm_matlab /usr/tmp/$marker.vd$ndaemon
		    ;;
#===========================================================================
#  ADD OTHER DAEMONS HERE. Example, PV-WAVE. If you are using PV-WAVE
#			   uncomment the three lines starting with the
#			   'PVI)' line. Uncomment means remove the
#			   '#' from column 1 of each line.
#			   For each additional daemon duplicate those
#			   three lines, uncomment them, and replace PVI
#			   by the daemon name associated with the appropriate
#			   DAEMON line in your license file. In almost
#			   all cases this should work. The script will
#			   use the license file to determine $dameonPath.
#			   It is helpful to know something about the Bourne
#			   Shell if special changes are required.
#
#		PVI)    # Use the daemonPath - arch binary already
#		    ln -s $daemonPath /usr/tmp/$marker.vd$ndaemon
#		    ;;
#===========================================================================
		  *)	# daemons you forgot
	    	    ndaemon=`expr $ndaemon - 1`
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo ''
    echo '    Sorry! Skipped following DAEMON line. Fix $MATLAB/etc/lmboot script.'
    echo ''
    echo "$line"
    echo ''
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
		    continue
		    ;;
	    esac    
	    echo "$line" | sed "s%$daemonPath%/usr/tmp/$marker.vd$ndaemon%" >> /usr/tmp/$marker.dat
	else
	    echo "$line" >> /usr/tmp/$marker.dat
	fi
     done) < $licensefile
#
# Create link for lmgrd. Always assume $MATLAB/etc/$Arch/lmgrd
#
    rm -f /usr/tmp/$marker.ld > /dev/null 2>&1
    ln -s $MATLAB/etc/$Arch/lmgrd /usr/tmp/$marker.ld
#	    
# Start daemons
#
    case "$Arch" in
        apollo)
   	    /etc/server /usr/tmp/$marker.ld -c /usr/tmp/$marker.dat $arglist >> $lmlogfile &
	   if [ $? -ne 0 ]; then
	       exit 1
	   fi
           ;;
	sgi|sol2)
   	   /usr/tmp/$marker.ld -c /usr/tmp/$marker.dat $arglist >> $lmlogfile &
	   if [ $? -ne 0 ]; then
	       exit 1
	   fi
#
# link to lmdown required for boot script
#
	   rm -f /usr/tmp/lmdown > /dev/null 2>&1
	   ln -s $MATLAB/etc/lmdown /usr/tmp/lmdown
	   ;;
        *)
   	   /usr/tmp/$marker.ld -c /usr/tmp/$marker.dat $arglist >> $lmlogfile &
	   if [ $? -ne 0 ]; then
	       exit 1
	   fi
           ;;
    esac
#
    exit 0
