#
# usage:        mklicdat.sh
#
# abstract:     This Bourne Shell script creates a license file at
#		a specified path.
#
# note(s):      1. This routine must be called using a . (period)
#
# Copyright (c) 1992-1998 by The MathWorks, Inc.
# $Revision: 1.11 $  $Date: 1998/12/31 20:54:04 $
#----------------------------------------------------------------------------
#
#=======================================================================
# Outside Globals:
#
#   licdat_path - get_license_data [options.sh]
#   etc_lmhostid_exe - get_license_data [mapname.sh]
#   ROOTDIR - get_license_data [install_matlab, main.sh]
#   etc_license_dat - get_license_data [mapname.sh]
#
# Inside Globals:
#
#   sfile    - creation_error [get_license_data]
#   Hostname -         [get_license_data]  
#   HostID   -         [get_license_data]
#   filetype -         [get_license_data]
#   basedir  - create_license_template        [get_license_data]   
#   basefile - create_license_template        [get_license_data]
#   msg - template_lic_error [create_license_template]
#   
# Main function:
#
#   create2_license_file ()
#
# Functions:
#
#   template_lic_error ()
#   template_lic_success ()
#   lm_txt_merge ()   
#   license_exists_user ()
#   license_exists ()
#   get_license_data ()
#   create_license_template ()
#
#=======================================================================
    template_lic_error () { # Output a license template error message.
			    #
                      	    # Always returns a 0 status.
                      	    #
                      	    # usage: template_error
                      	    #
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo ''
    echo '    Error: could not create template license file in . . .'
    echo ''
    echo "        file = $sfile" | sed 's/\$ROOTDIR/$MATLAB/'
    echo ''
    echo "$msg" | awk '
#----------------------------------------------------------------------------
    BEGIN { syn = sprintf ("%c", 22) }
	  { printf "%c    %s\n", syn, $0}'
#----------------------------------------------------------------------------
    echo ''
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	return 0
    }
#=======================================================================
    template_lic_success () { # Output a license template success message.
			      #
                      	      # Always returns a 0 status.
                      	      #
                      	      # usage: template_lic_success
                      	      #
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo ''
    echo '    Created template license file in . . .'
    echo ''
    echo "        file = $sfile" | sed 's/\$ROOTDIR/$MATLAB/'
    echo ''
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	return 0
    }
#=======================================================================
    lm_txt_merge () { # Output a license file merge message.
		      #
                      # Always returns a 0 status.
                      #
                      # usage: lm_txt_merge
                      #
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo "    Merged in 'license.dat' file found in "'$ROOTDIR . . .' | \
	      sed 's/\$ROOTDIR/$MATLAB/'
    echo ''
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	return 0
    }
#=======================================================================
    license_exists_user () { # Output license file already exists message
			     # for the case that there is a user license
			     # file in $ROOTDIR.
			     #
                      	     # Always returns a 0 status.
                      	     #
                      	     # usage: license_exists_user
                      	     #
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo ''
    echo '    A file already exists at'
    echo '' 
    echo "        file = $sfile" | sed 's/\$ROOTDIR/$MATLAB/'
    echo ''
    echo '    Did not create template license file . . .'
    echo ''
    echo "    Found a 'license.dat' file in "'$ROOTDIR'" to merge . . ." | \
	      sed 's/\$ROOTDIR/$MATLAB/'
    echo ''
    echo "    Merge not done since template file was not created . . ."
    echo ''
    echo "    Either delete the 'file' above and try again or merge the"
    echo "    two files together manually."
    echo ''
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	return 0
    }
#=======================================================================
    license_exists () { # Output license file already exists message
			# for the case that there is no user license
			# file in $ROOTDIR.
			#
                      	# Always returns a 0 status.
                      	#
                      	# usage: license_exists_user
                      	#
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo ''
    echo '    A file already exists at'
    echo '' 
    echo "        file = $sfile" | sed 's/\$ROOTDIR/$MATLAB/'
    echo ''
    echo '    Did not create template license file . . .'
    echo ''
    echo '    If you want the license file there, please delete the current'
    echo '    file and try again.'
    echo ''
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	return 0
    }
#=======================================================================
     create_license_data () { # Determines the following license data:
			      #
			      # sfile      - license path in terms of
			      #              ROOTDIR if possible
			      # Hostname   - name of host
			      # HostID     - Host identificaion for FLEXlm
			      # basedir    - dir part of license path
			      # basefile   - file part of license path
			      #
                	      # Returns a 0 status unless an error occurred.
                	      #
                	      # usage: create_license_data
                	      #
#
#                       Get template license only [-o option]
#                       -------------------------
#
# We assume that we have not changed directories since the orginal
# invocation (at $WD).
#
	file="$licdat_path"
	sfile=`echo $file $ROOTDIR | awk '
#----------------------------------------------------------------------------
    { ix = index($1,$2)
      if (ix == 1)
	   print "$ROOTDIR" substr($1,length($2)+1)
      else
	   print $1
    }'`
#----------------------------------------------------------------------------
#
	if [ "`search_path hostname`" = "" ]; then
            Hostname=`uname -n`
        else
            Hostname=`hostname`
        fi
#
        HostID=`$etc_lmhostid_exe | sed -n -e '/FLEXlm host ID/s/^[^"]*"//p' \
                                  | sed -n -e '/"/s/".*//p'`
#
# Need to construct a full file path.
# 1. Automounting cannot be handled here so we will not deal with it.
# 2. Check the license path to see if it already exists as a directory.
#    a. Yes. That is the basedir. Assume a basefile of license.dat.
#    b. No.  Determine the basedir and basefile. Create the basedir.
#	     Error if the basedir cannot be created. Note that if it
#	     ends in a /, /., or /.. we assume that it is a directory
#            and basefile will be license.dat.
# 3. Use basedir for LM_ROOT in the skeleton license file.
#    
        (cd $file) > /dev/null 2>&1
        if [ $? -eq 0 ]; then
	    basedir=`(cd $file; /bin/pwd)`
	    basefile='license.dat'
	    if [ `expr "//$file" : '//.*/[ 	]*$'` -ne 0 ]; then
	        sfile=${sfile}license.dat
	    else
	        sfile=$sfile/license.dat
	    fi
        else
	    if [ `expr "//$file" : '//.*/[ 	]*$'` -ne 0 ]; then
#
# Ends in a /
#
		basedir=$file
	        basefile='license.dat'
		sfile=${sfile}license.dat
	    elif [ `expr "//$file" : '//.*/\.[ 	]*$'` -ne 0  -o \
	           `expr "//$file" : '//[ 	]*\.[ 	]*$'` -ne 0 ]; then
#
# Ends in a /. or just .
#
		basedir=$file
	        basefile='license.dat'
		sfile=$sfile/license.dat
	    elif [ `expr "//$file" : '//.*/\.\.[ 	]$*'` -ne 0 -o \
	           `expr "//$file" : '//[ 	]*\.\.[ 	]$*'` -ne 0 ]; then
#
# Ends in a /.. or just ..
#
		basedir=$file
	        basefile='license.dat'
		sfile=$sfile/license.dat
	    else
#
#  Relative with one "/" or absolute with more than one "/" [up to last /],
#  or absolute but only one "/" [/], or no "/" [.]
#
	        basedir=`expr "//$file" : '//[ 	]*\(.*\)/.*' \|  "//$file" : '//[ 	]*\(/\)[^/]*' \| '.'`
#
# Has at least one / or no /
#
                basefile=`expr "//$file" : '.*/\(.*\)' \| "$file"`
	    fi
	    make_path $basedir
	    if [ $? -ne 0 ]; then
		return 1
	    fi
	    basedir=`(cd $basedir; /bin/pwd)`
	fi
	return 0
    }
#=======================================================================
    create_license_template () { # Creates a license template.
		         	 #
                         	 # Returns a 0 status unless an error.
                         	 #
                         	 # usage: create_license_template
                         	 #
	(if [ `expr "//$basedir" : '//.*/[ 	]*$'` -ne 0 ]; then
	     filepath=$basedir$basefile
	 else
	     filepath=$basedir/$basefile
	 fi
	 if [ "$CD_LM_INSTALL" != "" ]; then
	    lmroot=$basedir
	 else
	    lmroot=$ROOTDIR/etc
	 fi
	 sed -e "s%|>HOSTNAME<|%$Hostname%g" \
   	     -e "s%|>HOST_ID<|%$HostID%g" \
	     -e "s%|>LM_ROOT<|%$lmroot%g" \
   	     $etc_license_dat > $filepath) 2>$temp_file
	state=$?
        msg="`cat $temp_file`"
	rm -f $temp_file
	if [ $state -ne 0 ]; then
	    template_lic_error
	    return 1
	else
	    chmod 644 $basedir/$basefile
	    template_lic_success
	    return 0
	fi
}
#=======================================================================
    create2_license_file () { # Creates a license file. If creating for
			      # the first time merge in any license.dat
			      # file in $ROOTDIR.  It is more general
			      # than create_license_file in lm.sh. 
		              #
                              # Returns a 0 status unless an error.
                              #
                              # usage: create2_license_file
                              #
#set -x
	create_license_data
	if [ $? -ne 0 ]; then
	    return 1
        fi
        if [ ! -f $basedir/$basefile ]; then
	    create_license_template
	    if [ $? -ne 0 ]; then
	        return 1
	    fi
#
# If the license template is not the assumed default location of the
# users initial license file, $ROOTDIR/license.dat, then try to merge
# in any existing initial license file.
#
	    if [ "$ROOTDIR/license.dat" != "$basedir/$basefile" ]; then
                if [ "`actualpath $ROOTDIR/license.dat`" != "" ]; then
                    cat $basedir/$basefile > $temp_file
                    echo 'EOF' >> $temp_file
                    merge_license $temp_file $ROOTDIR/license.dat > $basedir/$basefile
		    lm_txt_merge
                    rm -f $temp_file $ROOTDIR/license.dat > /dev/null 2>&1
	        fi
	    fi
	    return 0
	else
	    if [ -f $ROOTDIR/license.dat -a \
		   "$ROOTDIR/license.dat" != "$basedir/$basefile" ]; then
#
# User license file exists
#
		license_exists_user
	    else
#
# No user license file exists
#
		license_exists
	    fi
	    return 1
	fi
    }
#=======================================================================
