#! /bin/sh
#
#  Name:
#
#     lmhostid  determines the FLEXlm host identifier
#     
#  Usage:
#
#     lmhostid 	[-h|-help] | [-arch] [-w] [ether]
#
#  Description:
#
#     This Bourne Shell script prints out a message containing
#     the FLEXlm host identifier.
#
#  Options:
#
#     -h | -help 	- Help. Print usage.
#
#     -arch             - Assume local host has architecture arch.
#
#     -w                - Produce standard Globetrotter output
#			  in all cases.
#
#     ether             - Select the ethernet port for the hostid.
#			  (HP 9000 series PA-RISC - otherwise ignored)
#
# note(s):
#
#     1. This script executes without changing the current directory.
#        This means that relative pathnames can be used in command
#        options.
#
#  Copyright (c) 1986-1998 by The MathWorks, Inc.
#  $Revision: 1.27 $  $Date: 1999/10/29 22:08:24 $
#-----------------------------------------------------------------------
#23456789012345678901234567890123456789012345678901234567890123456789012
#
    arg0_=$0
#
    trap "rm -f /tmp/$$a; exit 1" 1 2 3 15
#
# Do not use ARCH if it exists in the environment
#
    if [ "$LM_SET_ENV" = "" -a "$ARCH" != "" ]; then
        ARCH=""
    fi
#
#========================= archlist.sh (start) ============================
#
# usage:        archlist.sh
#
# abstract:     This Bourne Shell script creates the variable ARCH_LIST.
#
# note(s):      1. This file is always imbedded in another script
#
# Copyright 1997-2000 The MathWorks, Inc.
# $Revision: 1.8 $  $Date: 2000/06/16 16:08:59 $
#----------------------------------------------------------------------------
#
    ARCH_LIST='sol2 hpux hp700 alpha ibm_rs sgi glnx86'
#=======================================================================
# Functions:
#   check_archlist ()
#=======================================================================
    check_archlist () { # Sets ARCH. If first argument contains a valid
			# arch then ARCH is set to that value else
		        # an empty string. If there is a second argument
			# do not output any warning message. The most
			# common forms of the first argument are:
			#
			#     ARCH=arch
			#     MATLAB_ARCH=arch
			#     argument=-arch
			#
                        # Always returns a 0 status.
                        #
                        # usage: check_archlist arch=[-]value [noprint]
                        #
	if [ $# -gt 0 ]; then
	    arch_in=`expr "$1" : '.*=\(.*\)'`
	    if [ "$arch_in" != "" ]; then
	        ARCH=`echo "$ARCH_LIST EOF $arch_in" | awk '
#-----------------------------------------------------------------------
	{ for (i = 1; i <= NF; i = i + 1)
	      if ($i == "EOF")
		  narch = i - 1
	  for (i = 1; i <= narch; i = i + 1)
		if ($i == $NF || "-" $i == $NF) {
		    print $i
		    exit
		}
	}'`
#-----------------------------------------------------------------------
	       if [ "$ARCH" = "" -a $# -eq 1 ]; then
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
echo ' '
echo "    Warning: $1 does not specify a valid architecture - ignored . . ."
echo ' '
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	       fi
	    else
		ARCH=""
	    fi
	else
	    ARCH=""
	fi
#
	return 0
    }
#=======================================================================
#========================= archlist.sh (end) ==============================
#
#=======================================================================
#
# Functions:
#
#   scriptpath ()
#   standalone_lm ()
#
#=======================================================================
    scriptpath () { # Returns path of this script as a directory,
                    # ROOTDIR, and command name, CMDNAME.
		    #
		    # Returns a 0 status unless an error occurred.
		    #
                    # usage: scriptpath
                    #
#
	filename=$arg0_
#
# 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
	    if [ ! -d $newdir ]; then
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
echo ''
echo 'Internal error 1: Could not determine the path of the command.'
echo ''
echo "                  original command path = $arg0_"
echo "                  current  command path = $filename"
echo ''
echo '                  Please contact:'
echo '' 
echo '                      MathWorks Technical Support'
echo ''
echo '                  for further assistance.'
echo ''
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	        return 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 command.'
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 ''
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	        return 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"
	        CMDNAME="$newbase"
                LM_ROOTdefault=`/bin/pwd`
	        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 ''
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	    return 1
        fi
        cd $cpath
        return 0
    }
#=======================================================================
    standalone_lm () { # If install_matlab does not exist in the
		       # parent directory then it is standalone
		       # and output a 1 else 0.
		       #
		       # Always returns 0 status.
                       #
                       # usage: standalone_lm
                       #
	if [ ! -f $LM_ROOTdefault/../install_matlab ]; then
	    echo 1
	else
	    echo 0
	fi
	return 0
    }
#=======================================================================
#
#
# Determine the default license manager root directory (LM_ROOTdefault)
# and the current directory (cpath)
#
    scriptpath
    if [ $? -ne 0 ]; then
        exit 1
    fi
#
# Verify input
#
    stat="OK"
    msg=""
#
    help=
    arglist=
    pureglobes=0
#
    while [ "$stat" = "OK" -a $# -gt 0 ]; do
	case "$1" in
	    -h|-help)		# -help: Help option.
	        help=1
		;;
	    -w)
		pureglobes=1
		;;
            -*)
                arch=$ARCH
                check_archlist argument=$1 noprint
                if [ "$ARCH" = "" ]; then
                    ARCH=$arch
                    arglist="$arglist $1"
                fi
                ;;
	    *)
                arglist="$arglist $1"
                ;;
	esac
	shift
    done
#
    if [ "$stat" = "" -o "$help" != "" ]; then
	if [ "$msg" != "" ]; then
#-----------------------------------------------------------------------
    echo " "
    echo "    Error: $msg"
    echo " "
#-----------------------------------------------------------------------
	fi
#-----------------------------------------------------------------------
echo "------------------------------------------------------------------------"
echo " "
echo "    Usage: lmhostid [-h|-help] | [-arch] [-w] [ether]"
echo " "
echo "    -h|-help          - Help."
echo "    -arch             - Assume local host has architecture arch."
echo "    -w                - Produce standard Globetrotter output"
echo "                        in all cases."
echo "    ether             - Select the ethernet port for the hostid."
echo "                        (For HP 9000 series PA-RISC only. The default"
echo "                         for the PA-RISC is the 32-bit id."
echo " "
echo "    Prints out a message containing the FLEXlm host identifier."
echo " "
echo "------------------------------------------------------------------------"
#-----------------------------------------------------------------------
        exit 1
    fi
#
    . $LM_ROOTdefault/util/arch.sh
    if [ "$ARCH" = "unknown" ]; then
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
echo ' '
echo '    Error: Could not determine the machine architecture for this'
echo '           host. If your host is a supported architecture please'
echo '           contact:'
echo ' '
echo '               MathWorks Technical Support'
echo ' '
echo '           for further assistance.'
echo ' '
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        exit 1
    fi
#
# Determine valid lmhostid executable
#
# Try: 1. $LM_ROOTdefault/$ARCH/lmhostid
#      2. $MATLAB/update/bin/$ARCH/lmhostid (not standalone)
#
# Handle the error case.
#
    if [ -f $LM_ROOTdefault/$ARCH/lmhostid ]; then
        lmhostid_exec="$LM_ROOTdefault/$ARCH/lmhostid"
    elif [ "`standalone_lm`" = "0" ]; then
#
# If part of MATLAB then update directory is off the root directory
# which is assumed to be one level up
#
        if [ -f $LM_ROOTdefault/../update/bin/$ARCH/lmhostid ]; then
            lmhostid_exec="$LM_ROOTdefault/../update/bin/$ARCH/lmhostid"
	else
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
echo "Error: lmhostid ($ARCH)"
echo '       "No lmhostid binary"'
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	fi
    else
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
echo "Error: lmhostid ($ARCH)"
echo '       "No lmhostid binary"'
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        exit 1
    fi
#
# Assume that if there are any arguments you know what you are doing
#
    if [ "$pureglobes" != "0" ]; then
#
# Product standard Globetrotter output in all cases
#
	$lmhostid_exec $arglist
        exit $?
    fi
#
# lmhostid - Copyright (C) 1989-1994 Globetrotter Software, Inc.
# The FLEXlm host ID of this machine is ""
# lmhostid: Cannot find ethernet device (-29,175:2) No such file or directory
#
    $lmhostid_exec $arglist > /tmp/$$a 2>&1
    status=$?
#
# For linux also pass the hostname which could be 'localhost' so it can
# be used in case there is no ethernet card
#
    if [ "$ARCH" = "lnx86" ]; then
	if [ -f /bin/hostname ]; then
	    HOSTNAME=`/bin/hostname`
	else
	    HOSTNAME='localhost'
	fi
	status=0
    else
	HOSTNAME=''
    fi
#
    echo $ARCH $HOSTNAME | cat - /tmp/$$a | awk '
#-----------------------------------------------------------------------
    BEGIN { state = 0
            dquote = sprintf ("%c", 34)   # set double quote
	  }
    NR == 1 { arch = $1; hostname = ""; if (NF == 2) hostname = $2; next }
    NR == 2 { line1 = $0; next }
    NR == 3 { line2 = $0; next }
    NR == 4 { state = 1
	      if (arch != "lnx86") {
	          ix1 = index($0,$2)
	          ix2 = index($0,"(")
	          print "Error: lmhostid (" arch ")"
	          print "       " dquote substr($0,ix1,ix2-ix1-1) dquote 
	      } else {
		  ix1=index(line2,dquote)
		  print line1
		  print substr(line2,1,ix1) "HOSTNAME=" hostname dquote
	      }
	      exit
	    }
    END { if (state == 0) {
	      print line1
	      print line2
	  }
        }'
#-----------------------------------------------------------------------
    rm -f /tmp/$$a
    exit $status
