#! /bin/sh
#
#  Name:
#     lmhostid   script file for invoking FLEXlm License Manager Identifier
#
#  Usage:
#     lmhostid
#     lmhostid  [long | ether]		(HP 9000 series 300, 400, 700)
#     lmhostid  -c licensefile		(Linux)
#
#  Description:
#     lmhostid prints out a machine's hostid or other unique identifier.
#     On the HP 9000 series, if no argument is passed we first try with no
#     argument. If a '0' is returned, we try again with the the argument of
#     ether. If an argument is given we assume the user is knowedgeable.
#     On Linux it searches the licensefile for the value of SiteID.
#
#  Options:
#
#     long
#
#           Select the id module for the hostid.
#	    (HP 9000 series 300, 400, 700 only - otherwise ignored)
#
#     ether
#
#           Select the ethernet port for the hostid. (default)
#	    (HP 9000 series 300, 400, 700 only - otherwise ignored)
#
#     -c licensefile
#
#	    Path of the license file.
#	    (Linux)
#
#  Copyright (c) 1986-95 by The MathWorks, Inc.
#  $Revision: 1.15 $  $Date: 1995/05/02 12:26:50 $
#__________________________________________________________________________
#
#**************************************************************************
# 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
    fi
#
# Determine valid lmhostid executable
#
# Try: 1. $MATLAB/etc/$Arch/lmhostid
#      2. $MATLAB/update/bin/$Arch/lmhostid
#
    if [ -f $MATLAB/etc/$Arch/lmhostid ]; then
        lmhostid_exec="$MATLAB/etc/$Arch/lmhostid"
    elif [ -f $MATLAB/update/bin/$Arch/lmhostid ]; then
        lmhostid_exec="$MATLAB/update/bin/$Arch/lmhostid"
    else
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo "Error: lmhostid ($Arch)"
    echo '"No lmhostid binary"'
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        exit 1
    fi
#
# If Linux, and there are no arguments, and not on CD, then pass
# -c $MATLAB/etc/license.dat
#
    if [ "$Arch" = "lnx86" -a $# -eq 0 -a "$CD_ROOT" = "" ]; then
	exec $lmhostid_exec -c $MATLAB/etc/license.dat
    fi
#
# Assume that if there are any arguments you know what you are doing
#
    if [ $# -ne 0 ]; then
	exec $lmhostid_exec $*
    else
#
# Special error cases for HP machines
#
	if [ "$Arch" = "hp300" -o "$Arch" = "hp700" ]; then
	    hostid=`$lmhostid_exec 2>&1 |
		   sed -e "1d" -e '2s/^[^"]*"//' -e '2s/".*//'`
	    if [ "$hostid" != "" ]; then
	        nf=`echo "$hostid" | awk '{ print NF }'`
		nf=`expr $nf + 0`
		if [ $nf -eq 1 ]; then
	    	    if [ "$hostid" = "0" -o "$hostid" = "1000000" ]; then
	   		exec $lmhostid_exec ether
	    	    else
	        	exec $lmhostid_exec
	    	    fi
		else
	   	    exec $lmhostid_exec ether
		fi
	    else
	   	exec $lmhostid_exec ether
	    fi
	else
	    exec $lmhostid_exec
	fi
    fi
