#
# usage:        lm.sh
#
# abstract:     This Bourne Shell script installs the license manager.
#
# note(s):      1. This routine must be called using a . (period)
#
# Copyright (c) 1992-2000 The MathWorks, Inc.
# $Revision: 1.56 $  $Date: 2000/07/27 19:42:15 $
#----------------------------------------------------------------------------
#
#=======================================================================
# Outside Global variables:
#
#   ARCH - check_to_install_lm [install_matlab]
#   LM_ROOT - merge_lsfile, check_to_install_lm, install_etc_scripts,
#	      build_lm_links [main.sh]
#   LM_SCRIPTS - check_to_install_lm, install_etc_scripts [main.sh]	
#   MATLAB - lm_txt [verifyp.sh]
#   origMATLAB - install_lm_scripts [verifyp.sh]
#   batch - check_to_install_lm, build_lm_links [options.sh, batch1.sh]
#   minimal_install - main [options.sh]
#   temp_file, temp_file2 - merge_lsfile 
#   temp_file3, temp_file4  - merge_lsfile [main.sh]
#   usermode - build_lm_links [options.sh, ruser.sh]
#
# Inside Global variables:
#
#   ExpireDate - lm_txt [check_to_install_lm]
#
# Outside Functions:
#
#   clear_screen ()
#   premature_finish ()
#   get_oldname ()
#   query_to_continue ()
#   search_path ()
#
# Main function:
#
#   install_license_manager ()
#
# Functions:
#
#   build_lm_links ()
#   check_to_install_lm ()
#   install_lm_scripts ()
#   lm_txt ()
#   merge_lsfile  ()
#   start_stop_mesg ()
#
#=======================================================================
    install_license_manager  () { # Install the FLEXlm license manager.
                    		  #
                	 	  # Returns a 0 status or exits.
                	 	  #
                	 	  # usage: install_license_manager
                	 	  #
        check_to_install_lm
        if [ $? -ne 0 ]; then
	    query_to_continue
	    return 1
        fi
#
        install_lm_scripts
#
        if [ "$minimal_install" != "1" ]; then
	    build_lm_links
        fi
#
        start_stop_mesg
#
	return 0
    }
#=======================================================================
    build_lm_links () { # Build symbolic links for lmboot and lmdown
			# if desired and possible.
                	#
                	# Returns a 0 status or exits.
                	#
                	# usage: build_lm_links
                	#
	clear_screen
	lm_txt 'links'
        if [ "$usermode" != "1" ]; then
	    lm_txt 'root_links'
            echon 'Do you want to create the symbolic links now? ([y]/n) '
            read ans
            if [ "$batch" = "1" ]; then
                echo $ans
            fi
            if [ `expr "//$ans" : '//[Nn].*'` -ne 0 ]; then
	        lm_txt 'root_links2'
		query_to_continue
            else
                rm -rf /etc/lmboot_TMW12 > /dev/null 2>&1
                ln -s $LM_ROOT/lmboot /etc/lmboot_TMW12
		lm_txt 'root_links3'
                rm -rf /etc/lmdown_TMW12 > /dev/null 2>&1
                ln -s $LM_ROOT/lmdown /etc/lmdown_TMW12
		lm_txt 'root_links4'
		query_to_continue
	    fi
        else
	    lm_txt 'user_links'
	    query_to_continue
        fi
	return 0
    }
#=======================================================================
    check_to_install_lm () { # Check to install license manager.
                	     #
                	     # Returns a 1 status if on return we are
			     # to finish up else a 0 status.
                	     #
                	     # usage: check_to_install_lm
                	     #
#
#		Install License Manager [1, 3, 4]
#               -----------------------
#
	clear_screen
        lm_txt 'install'
#
# Install lmhostid script since the matlab script requires it.
#
	if [ ! -d $LM_SCRIPTS ]; then
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo ''
    echo '    License manager scripts are missing . . .'
    echo '    License manager installation skipped . . .'
    echo ''
    echo '    To install the license manager scripts try:'
    echo ''
    echo '    CD-ROM: Reinstall the item: FLEXlm '
    echo '    FTP:    Download the matlab.common file and reinstall.'
    echo ''
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
		return 1
	fi
	cd $LM_SCRIPTS
        file=lmhostid
        if [ -f $file ]; then
	    etcscript='lmhostid'
	    if [ -f $LM_ROOT/$etcscript ]; then
	        oldname=`get_oldname $LM_ROOT $etcscript`
	        mv -f $LM_ROOT/$etcscript $LM_ROOT/old/$oldname
	    fi
	    cp $file $LM_ROOT/$etcscript
	    chmod 755 $LM_ROOT/$etcscript
        fi
        if [ ! -d $LM_ROOT/$ARCH ]; then
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo ''
    echo '    License manager executables are missing . . .'
    echo '    License manager installation skipped . . .'
    echo ''
    echo '    To install the license manager executables try:'
    echo ''
    echo '    CD-ROM: Reinstall the item: FLEXlm'
    echo "    FTP:    Download the matlab.$ARCH file and reinstall."
    echo ''
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	    return 1
        fi
        lm_txt 'preserve'
#
# Check for a demo license. The condition is that all noncommented
# FEATURE (or INCREMENT) lines except TWM_Archive must have DEMO in the last 
# field to be considered a DEMO license. Return the date on the first FEATURE
# line other than TWM_Archive if a DEMO file.
#
	if [ -f $LM_ROOT/license.dat ]; then
            ExpireDate=`cat $LM_ROOT/license.dat | awk '
#----------------------------------------------------------------------------
		BEGIN { state = 1; first = 1 }
		$1 != "FEATURE" && $1 != "INCREMENT" { next }
		$2 == "TMW_Archive" { next }
		    { if ( $NF != "DEMO" ) {
			  state = 0
			  exit
		      }
		      else if (first == 1) {
			  expiredate = $5
			  first = 0 
		      }
		    }
		END { if (state == 1) print expiredate }'`
#----------------------------------------------------------------------------
            if [ "$ExpireDate" ]; then
	        lm_txt 'demo'
	    fi
        fi
#
	echon 'Install the FLEXlm License Manager for MATLAB? ([y]/n) '
        read ans
        if [ "$batch" = "1" ]; then
            echo $ans
        fi
        if [ `expr "//$ans" : '//[Nn].*'` -gt 0 ]; then
	    clear_screen
	    return 1
	fi
#
	return 0
    }
#=======================================================================
    install_lm_scripts () { # Install the license manager scripts.
                	    #
                	    # Returns a 0 status or exits.
                	    #
                	    # usage: install_lm_scripts
                	    #
	clear_screen
	lm_txt 'scripts'
	cd $LM_SCRIPTS
#
        update=0
#
# Assume you cannot write in $LM_SCRIPTS
#
# 1. lmvendor is the 8.3 name of lm_matlab. Change it to lm_matlab if you
#    find it.
# 2. lmswitch is the 8.3 name of lmswitchr. Change it to lmswitchr if you
#    find it. 
#
# Watch out for ISO 9660 names.
#
        for file in *
        do
	    if [ -f $file ]; then
	        etcscript=`expr "//$file" : '.*/\([^.;]*\).*'`
	        etcscript=`echo $etcscript |  tr 'A-Z' 'a-z'`
	        case "$etcscript" in
		    lm*)
		        :
		        ;;
		    *)
		        continue
		        ;;
	        esac
	        if [ "$etcscript" = "lmvendor" ]; then
		    etcscript=lm_matlab
	        fi
	        if [ "$etcscript" = "lmswitch" ]; then
		    etcscript=lmswitchr
	        fi
	        if [ "$etcscript" = "lmopts" ]; then
		    etcscript=lmopts.sh
	        fi
                mesg=`echo "$etcscript" | awk '{ printf ("    %-15s", $1) }'`
	        case "$etcscript" in
		    lmopts.sh)
		        echon "$mesg"
                        merge_lsfile "$file" $LM_ROOT $etcscript
                        if [ $? -ne 0 ]; then
                            update=1
		        fi
		        ;;
		    *)
			echon "$mesg"
                        if [ -f $LM_ROOT/$etcscript ]; then
#----------------------------------------------------------------------------
                            cat "$file" | sed \
                                -e "s%|>LM_ROOT<|%$origMATLAB%g" \
                                -e "s%|>AUTOMOUNT_MAP<|%$AUTOMOUNT_MAP%g" \
                                > $temp_file
#----------------------------------------------------------------------------
                            diff $LM_ROOT/$etcscript $temp_file > /dev/null 2>&1
                            if [ $? -ne 0 ]; then
	        	        oldname=`get_oldname $LM_ROOT $etcscript`
                                mv -f $LM_ROOT/$etcscript $LM_ROOT/old/$oldname
                                mv $temp_file $LM_ROOT/$etcscript
                                chmod 755 $LM_ROOT/$etcscript
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo "Replaced       $oldname"
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
                            else
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo "  No"
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
			    fi
			    rm -f $temp_file
                        else
                            cp "$file" $LM_ROOT/$etcscript
                            chmod 755 $LM_ROOT/$etcscript
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo "(Initial Copy)"
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
                        fi
		        ;;
	        esac
            fi
        done 
        if [ "$update" != "0" ]; then
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo ' '    
    echo '    * '"'diff'"' output saved at bottom of any Updated script file.'
    echo '      Review old local changes at bottom and fix top of file if'
    echo '      necessary.'
    echo ' '    
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        else 
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo ' '    
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        fi
#
	query_to_continue
#
	return 0
    }
#=======================================================================
    lm_txt () { # Outputs license manager text
                #  and command name, CMDNAME.
                #
                # Returns a 0 status unless msg_num out of range
                #
                # usage: lm_txt msg_pointer
                #
	msg_pointer=$1
	case "$msg_pointer" in
	    install)
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo '             B2. Install the FLEXlm Network License Manager'
    echo '             ----------------------------------------------'
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
		;;
	    preserve)
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
echo ' '
echo '    ----------------------------------------------------------------------'
echo '    | NOTE: A pre-existing license file (license.dat) in the $MATLAB/etc |'
echo '    |       directory will be PRESERVED by this installation.            |'
echo '    |                                                                    |'
echo '    |       If you need to install the license manager at a later time   |'
echo '    |       or on another host run this install program again selecting  |'
echo "    |       the 'LICENSE MANAGER ONLY' install option.                   |"
echo '    ----------------------------------------------------------------------'
echo ' '
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
		;;
	    demo)
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo '    There is a EXISTING license.dat file and it appears to be a' 
    echo '    DEMO license with expiration date:'
    echo ' '
    echo "                          $ExpireDate"
    echo ' '
    echo '    You are NOT required to install the license manager for a DEMO'
    echo '    license.'
    echo ' '
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
		;;
	    scripts)
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo '                C1. Create scripts in $MATLAB/etc directory'
    echo '                -------------------------------------------'
    echo ' '
    echo 'Creating $MATLAB/etc scripts . . .'
    echo ''
    echo '    Script         Changed?       Old Copy in etc/old'
    echo '    ------         -------        ------------------- '
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
		;;
	    links)
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo '       C2. Build symbolic links /etc/lmboot_TMW12, /etc/lmdown_TMW12'
    echo '       -----------------------------------------------------------'
    echo ' '
    echo '    ------------------------------------------------------------------------'
    echo '    | If you want to be able to reboot your machine and have the license   |'
    echo '    | manager running after the reboot you need to create link files       |'
    echo '    |             /etc/lmboot_TMW12 -> $MATLAB/etc/lmboot                  |'
    echo '    |             /etc/lmdown_TMW12 -> $MATLAB/etc/lmdown                  |'
    echo '    | These links are OUTSIDE the MATLAB root directory and REQUIRE        |'
    echo '    | superuser privilege to create.                                       |'
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
		;;
	    root_links)
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo '    |                                                                      |' 
    echo '    | You have the choice of creating the symbolic links:                  |'
    echo '    |      [a] Now.                                                        |'     
    echo '    |      [b] After the install is finished.                              |'
    echo '    ------------------------------------------------------------------------'
    echo ' '
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
		;;
	    root_links2)
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo ' '
    echo '    ------------------------------------------------------------------------'
    echo '    |  To create the links after the install is finished, execute the      |'
    echo '    |  the following UNIX command as superuser:                            |'
    echo '    |                                                                      |'
    echo '    |                         $MATLAB/etc/lmboot -s                        |'
    echo '    |                                                                      |'
    echo '      $MATLAB = '$MATLAB
    echo '    ------------------------------------------------------------------------'
    echo ' '
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
		;;
	    root_links3)
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo ''
    echo '    /etc/lmboot_TMW12 -> $MATLAB/etc/lmboot link created . . .'
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
		;;
	    root_links4)
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo '    /etc/lmdown_TMW12 -> $MATLAB/etc/lmdown link created . . .'
    echo ''
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
		;;
	    user_links)
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo '    ------------------------------------------------------------------------'
    echo '    Skipped. Not superuser . . .'
    echo '    ------------------------------------------------------------------------'
    echo '    | To create the links after the install is finished, first become      |'
    echo '    | superuser, and then execute the following command at the UNIX        |'
    echo '    | prompt:                                                              |'
    echo '    |                                                                      |'
    echo '    |                       $MATLAB/etc/lmboot -s                          |'
    echo '    |                                                                      |'
    echo '      $MATLAB = '$MATLAB
    echo '    ------------------------------------------------------------------------'
    echo ' '
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
		;;
	    start_stop)
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
echo "                   Starting/Stopping the License Manager"
echo ' '
echo '   ----------------------------------------------------------------------'
echo '   | If your license file requires you to run the license manager then  |'
echo '   | at the UNIX prompt, login to your license manager host. Change     |'
echo '   | your directory to $MATLAB/etc and execute the command:             |'
echo '   |                                                                    |'
echo '   |         lmstart         (to start the license manager)             |'
echo '   |         lmdown          (to stop the license manager)              |'
echo '   |                                                                    |'
echo "   | The same user that executed 'lmstart' should ALWAYS execute        |"
echo "   | 'lmdown'.                                                          |"
echo '    ---------------------------------------------------------------------'
echo ''
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
		;;
	    *)
		return 1
		;;
	    esac
	return 0
    }
#=======================================================================
    merge_lsfile () { # Construct license script new file. If current
		      # file does not exist save new file and you are
		      # done. Otherwise, diff current file with newly
		      # built one. If the RCS Revision has not change
		      # then nothing to do. Otherwise add any
		      # differences to the end as comments. For
		      # example,
		      # 
		      # current:   base
		      # 	   #EOF-----------------------------
		      #            # diff ...
		      #	           # date: ....
		      #	           # > ...		
		      #	           # < ...
		      #
		      # 	   base
		      #	           differences
		      #
		      # new:	   file
		      #
		      # Strip off #EOF---- ... tail of file
		      #
		      # diff file base > new_differences
		      #
		      # If new_differences then replace current by:
		      #
		      # 	   file
		      # 	   <differences>
		      # 	   #EOF-----------------------------
		      #	           # diff file base
		      #	           # date: `date`
		      #	           # <new_differences>
		      #
		      # else
		      #
		      #     current is unchanged
                      #
                      # Returns a status of 1 if updated else 0.
                      #
                      # usage: merge_sfile name target_dir target_name
                      #
	name="$1"
	target_dir=$2
	target_name=$3
	current_path=$target_dir/$target_name
#
	if [ ! -f $current_path ]; then
	    cat "$name" | sed \
    		-e "s%|>LM_ROOT<|%$LM_ROOT%g" \
    		-e "s%|>AUTOMOUNT_MAP<|%$AUTOMOUNT_MAP%g" > $current_path
	    chmod 755 $current_path
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo "(Initial copy)"
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	else 
	    cat "$name" | sed \
    		-e "s%|>LM_ROOT<|%$LM_ROOT%g" \
    		-e "s%|>AUTOMOUNT_MAP<|%$AUTOMOUNT_MAP%g" > $temp_file
	    revnew=`cat $temp_file | awk '
#----------------------------------------------------------------------------
        BEGIN { crev = "#$" "Revision"; rev = "$" "Revision" }
            crev == $1 { print $2; exit }
             rev == $2 { print $3; exit }'`
#----------------------------------------------------------------------------
	    cat $current_path | awk '
#----------------------------------------------------------------------------
	BEGIN { state = 1 }
	/^#EOF----/ { state = 0 }
		    { if (state == 1) print }' >  $temp_file2
#----------------------------------------------------------------------------
	    rev=`cat $temp_file2 | awk '
#----------------------------------------------------------------------------
        BEGIN { crev = "#$" "Revision"; rev = "$" "Revision" }
            crev == $1 { print $2; exit }
             rev == $2 { print $3; exit }'`
#----------------------------------------------------------------------------
	    if [ "$rev" = "$revnew" -a "$rev" != "" ]; then
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo "  No"
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
		return 0
	    fi
	    cat $current_path | awk '
#----------------------------------------------------------------------------
	BEGIN { state = 0 }
	/^#EOF----/ { state = 1 }
		    { if (state == 1) print }' >  $temp_file3
#----------------------------------------------------------------------------
	    diff $temp_file $temp_file2 > $temp_file4 2>/dev/null
	    if [ -s $temp_file4 ]; then
		oldname=`get_oldname $target_dir $target_name`
		mv -f $current_path $target_dir/old/$oldname
#
		cat $temp_file $temp_file3 > $current_path
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo "#EOF--------------------------------------------------------------------" >> $current_path
    echo "# diff $name(new) $name(old)" >> $current_path
    echo "# date: `date`" >> $current_path
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	        cat $temp_file4 | sed 's/^/# /' >> $current_path
	        chmod 755 $current_path
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo 'Updated*       '"$oldname"
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
		return 1
	    else
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo "  No"
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	    fi
	fi
        return 0
    }
#=======================================================================
    start_stop_mesg () { # Output message about starting and stopping
			 # the license manager.
                	 #
                	 # Returns a 0 status or exits.
                	 #
                	 # usage: start_stop_mesg
                	 #
	clear_screen
	lm_txt 'start_stop'
	query_to_continue
#
	return 0
    }
#=======================================================================
