#! /bin/sh
#set -x
#
#  Name:
#     lmhostid   script file for determining the user's SiteID for
#		 licensing on Linux systems.
#
#  Usage:
#     lmhostid  -c licensefile
#
#  Description:
#     Searches the licensefile for the value of SiteID. Returns
#     the value found or "unknown".
#
#  Options:
#
#     -c licensefile
#
#           Path of the license file.
#
#  Note(s): 1. The format of the output is:
#
#	Copyright (c) 1995 The MathWorks, Inc.
#	The SiteID is "12345"
#
#  Copyright (c) 1995 by The MathWorks, Inc.
#  $Revision: 1.2 $  $Date: 1995/05/01 21:44:10 $
#__________________________________________________________________________
#
# Verify input
#
    stat="OK"
    msg=""
    if [ $# -ne 2 ]; then
	stat=""
    fi
    while [ "$stat" = "OK" -a  $# -gt 0 ]; do
	case "$1" in
	    -c)
		shift
                File=$1
#==============================================================================
#
# Usage:	actualp
#
# abstract:	This Bourne Shell script returns the actual path for
#		a file in variable Filepath. Only a real existing file
#		returns a non-null value.
#
# note(s):      1. This routine must be called using a . (period)
#		2. The global variable 'File' is the initial filepath.
#
# Copyright (c) 1992-94 by The MathWorks, Inc.
# Revision: 1.2   Date: 1994/03/23 21:34:39
#------------------------------------------------------------------------
#
    Filepath=
#
    lsCmd=`ls -ld $File 2>/dev/null`
    if [ "$lsCmd" ]; then
#
# Check for link portably
#
	if [ `expr "$lsCmd" : '.*->.*'` -eq 0 ]; then
    	    if [ ! -d $File ]; then
	        Filepath=$File
            fi
        else
#
# A directory link?
#
            (cd $File) > /dev/null 2>&1
            if [ $? -eq 0 ]; then
	        :
            else
#
# Now it is either a file, link to a file, or still a bad path.
#
	        cpath=`/bin/pwd`
                localFile=$File
#
# Follow up to 8 links before giving up. Same as BSD 4.3
#
	        n=1
    	        while [ $n -le 8 ]
    	        do
#
# Get directory correctly!
#
   	            newDir=`echo "$localFile" | 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
		        break
	            fi
	            cd $newDir
	            newDir=`/bin/pwd`
	            newBase=`expr //$localFile : '.*/\(.*\)' \| $localFile`
                    lsCmd=`ls -l $newBase 2>/dev/null`
	            if [ ! "$lsCmd" ]; then
		        break
	            fi
#
# Check for link portably
#
	            if [ `expr "$lsCmd" : '.*->.*'` -ne 0 ]; then
	                localFile=`echo "$lsCmd" | awk '{ print $NF }'`
	            else
#
# It's a file
#
	                Filepath=$newDir/$newBase
	            fi
		    n=`expr $n + 1`
    	        done
    	        cd $cpath
	    fi
	fi
    fi
#==============================================================================
		if [ "$Filepath" = "" ]; then
		    stat=""
		    msg="licensefile ($File) does not exist"
		fi
		;;
	    *)
		stat=""
		;;
	esac
	shift
    done
#
    if [ "$stat" = "" ]; then
	if [ "$msg" != "" ]; then
#------------------------------------------------------------------------------
    echo ""
    echo "    Error: $msg"
#------------------------------------------------------------------------------
	fi
#------------------------------------------------------------------------------
    echo "--------------------------------------------------------------"
    echo " "
    echo "    usage: lmhostid -c licensefile"
    echo " "
    echo "    -c licensefile    - Specify the path of the license file."
    echo " "
    echo "--------------------------------------------------------------"
#------------------------------------------------------------------------------
	exit 1
    fi
#
# ...
# # SiteID: 12345    HostID: 7260d091
# ...
# FEATURE MATLAB    MLM 1 01-jul-1995 0 LEA7B467D9519AC6618A9 "" 12345
# ...
#
# Algorithm: (whichever comes first)
#
#   1. A comment line with SiteID: value
#   2. A FEATURE line with the passcode starting with L and the last
#      field not DEMO.
#
    SiteID=`cat $File | awk '
#------------------------------------------------------------------------------
substr($1,1,1) == "#" { for (i = 1; i <= NF; i = i + 1)
	  		    if ($i == "SiteID:") {
	      			print $(i+1)
	      			exit
	  		    } }
$1 == "FEATURE" && substr($7,1,1) == "L" && $NF != "DEMO" { print $NF; exit }'`
#------------------------------------------------------------------------------
#
    if [ "$SiteID" = "" ]; then
	SiteID="unknown"
    fi
#
#------------------------------------------------------------------------------
    echo "lmhostid - Copyright (C) 1995 The MathWorks, Inc."
    echo "The SiteID is "'"'$SiteID'"'
#------------------------------------------------------------------------------
    exit 0
