#
# usage:        fwelcome.sh
#
# abstract:     This Bourne Shell script prints the welcome screen,
#		followed by the TMW_Archive FEATURE line screen. If you
#		do not continue then print the exit screen and toggle with
#		the TMW_Archive FEATURE line screen.
#
# note(s):      1. This routine must be called using a . (period)
#
#		2. Welcome screen:
#
#		   case: ftp_select=1
#	           
#                    Generate List of FTP Files to Download
#  ----------------------------------------------------------------------------
#  | To install MATLAB using FTP requires you to select and download          |
#  | component files for the various MATLAB products you are licensed for.    |
#  | This interface allows you select those products and to have the list of  |
#  | component files generated automatically for you.                         |
#  |                                                                          |
#  | All toolboxes are encrypted. You need a 'license.dat' file,              |
#  | $MATLAB/etc/license.dat, with the correct 'TMW_Archive' FEATURE line to  |
#  | decrypt your toolboxes during installation.                              |
#
#		   case: FTP_TBX=1
#
#                        Checking Your 'license.dat' File
#  ----------------------------------------------------------------------------
#  | All toolboxes are encrypted. You need a 'license.dat' file,              |
#  | $MATLAB/etc/license.dat, with the correct 'TMW_Archive' FEATURE line to  |
#  | decrypt your toolboxes during installation.                              |
#
#----------------
#  |                                                                          |
#  | Creating skeleton 'license.dat' file . . . [please wait]                 | 
#----------------
#  | Merging in 'license.dat' file found in $MATLAB . . . [please wait]       |
#----------------
#  | Checking TMW_Archive FEATURE line . . . . [valid/invalid]                |
#----------------
#  | Checking TMW_Archive FEATURE line(s) . . . [please wait]                 |
#  |                                                                          |
#        1  valid   TMW_Archive FEATURE line(s)
#        0  invalid TMW_Archive FEATURE line(s)
#  |                                                                          |
#  | Here,                                                                    |
#  |                                                                          |
#       MATLAB =  ...                  
#  |                                                                          |
#  ----------------------------------------------------------------------------
#
#		2a. TWM_Archive line(s) screen
#
#  Please press <return> to continue: 
#
#		     TMW_Archive FEATURE Line Summary
#  ----------------------------------------------------------------------------
#  cat <TMW_Archive FEATURE line Error file>
#----------------
#
#    'license.dat' file, $MATLAB/etc/license.dat, does not exist . . .
#
#    MATLAB =  ...                  
#
#----------------
#
#    No TMW_Archive FEATURE lines in $MATLAB/etc/license.dat . . .
#
#    MATLAB =  ...                  
#
#----------------
#
#    No decryption software found. Looked for . . .
#
#--------- tape (ufs)
#
#        $MATLAB/update/tape/bin/$Arch/dear
#        $MATLAB/update/bin/$Arch/dear
#
#--------- non tape (ufs)
#
#        $MATLAB/update/bin/$Arch/dear
#
#    MATLAB =  ...                  
#
#    Found decryption software for other MATLAB supported architectures:
#
#	 sun4 dec_risc
#
#    If decryption is required, please use a system with one of these other
#    architectures to install MATLAB.
#
#---------------- If no other architectures then Contact the MathWorks.
#
#    Found NO decryption software for any other MATLAB supported architectures.
#
#    Please contact:    The MathWorks, Inc. Technical Support:
#         	        Phone: 508-653-1415
#         		FAX:   508-653-2997
#         		Email: support@mathworks.com
#----------------
#  ----------------------------------------------------------------------------
#  -> Only toolboxes listed under valid TMW_Archive FEATURE lines will be 
#  -> decrypted. To make changes type 'n' at the prompt and follow the directions
#  -> on the next screen.
#  ----------------------------------------------------------------------------
#  Continue? ([y]/n)
#----------------
#  (ftp_select=1)
#  ----------------------------------------------------------------------------
#  -> Only toolboxes listed under valid TMW_Archive FEATURE lines will appear
#  -> selected on the 'Select MATLAB Products' screens that follow.
#  ----------------------------------------------------------------------------
#  Please press <return> to continue: 
#----------------
#
#		3. Merging license.dat files
#
# Skeleton license.dat file:
# 
# #License file for use with FlexLM version 2.4c
# SERVER gerbil 7260d091 1700
# DAEMON MLM <path . . .>
# FEATURE TWM_Archive   MLM 4 01-jan-1994   0 0123456789ABCDEFGHIJ "81" 7260d091
# FEATURE MATLAB        MLM 1 01-jan-1994   1 0123456789ABCDEFGHIJ ""
# ...
#
# Generated license.dat file: 
#
# # BEGIN-------cut here-------CUT HERE-------BEGIN
# # MATLAB license passcode file for use with FLEXlm 2.4c
# # SiteID: unknown       Hostid: 7260d091      TMW_Archive Hostid: DEMO
# FEATURE TMW_Archive    MLM 4 01-feb-1996   0 3B0D539384183D29B4D9 "81" DEMO
# FEATURE MATLAB           MLM 1 01-feb-1996   1 2B3D1333C2453DDDEFBC ""
# FEATURE Signal_Toolbox   MLM 1 01-feb-1996   1 4BEDD323EB6136A05CBC ""
# # END---------cut here-------CUT HERE-------END
#
# Algorithm:
#
# 1. orig - remove the license file line
# 2. new  - add # MATLAB license line
# 3. new  - add # SiteID license line
# 4. orig - SERVER
# 5. orig - DAEMON
# 6. new - all FEATURE lines
#
#
#		4. Exit screen:
#
#               Directions for Preparing Your 'license.dat' File
#  ----------------------------------------------------------------------------
#  | 1. If there are toolboxes licensed to you that you cannot decrypt then   |
#  |    you will need a correct TMW_Archive FEATURE line. Type 'b' at the     |
#  |    prompt to review the TMW_Archive FEATURE Line Summary screen again.   |
#  |    To get a new 'license.dat' file first get the 'lmhostid' string for   |
#  |    your license server.                                                  |
#  |                                                                          |
#       The 'lmhostid' for this host (gerbil) is:  7260d091
#  |    If your license server is another host, login to that host and in     |
#  |    the MATLAB root directory run the command, './install_matlab -l'.     |
#  |                                                                          |
#  |    Then contact:         The MathWorks, Inc.        (508) 653-1415       |
#  |                   email: service@mathworks.com  FAX (508) 653-2997       |
#  |                                                                          |
#  | 2. Add or replace any outdated or incorrect FEATURE lines including the  |
#  |    TMW_Archive FEATURE line in the $MATLAB/etc/license.dat file using    |
#  |    your most recent 'license.dat' file.                                  |
#  |                                                                          |
#       MATLAB = . . .
#  |                                                                          |
#  | 3. Run the 'install_matlab' command again.                               |
#  ----------------------------------------------------------------------------
#  >
#
# Copyright (c) 1994-95 by The MathWorks, Inc.
# $Revision: 1.4 $  $Date: 1995/04/17 13:29:26 $
#----------------------------------------------------------------------------
#
#                               Welcome message
#                               ---------------
#
    if [ "$ftp_select" = "1" ]; then
#----------------------------------------------------------------------------
echo "                  Generate List of FTP Files to Download"
echo "  ----------------------------------------------------------------------------"
echo '  | To install MATLAB using FTP requires you to select and download          |'
echo '  | component files for the various MATLAB products you are licensed for.    |'
echo '  | This interface allows you select those products and to have the list of  |'
echo '  | component files generated automatically for you.                         |'
echo '  |                                                                          |'
echo "  | All toolboxes are encrypted. You need a 'license.dat' file,              |"
echo '  | $MATLAB'"/etc/license.dat, with the correct 'TMW_Archive' FEATURE line to  |"
echo '  | decrypt your toolboxes during installation.                              |'
#----------------------------------------------------------------------------
    else
#----------------------------------------------------------------------------
echo "                       Checking Your 'license.dat' File"
echo "  ----------------------------------------------------------------------------"
echo "  | All toolboxes are encrypted. You need a 'license.dat' file,              |"
echo '  | $MATLAB'"/etc/license.dat, with the correct 'TMW_Archive' FEATURE line to  |"
echo '  | decrypt your toolboxes during installation.                              |'
#----------------------------------------------------------------------------
    fi
    licdat_path=$MATLAB/etc/license.dat
    (cd $MATLAB/etc) > /dev/null 2>&1
    if [ $? -ne 0 ]; then
	mkdir $MATLAB/etc
	create_skeleton=1
    else
	lcmd=`ls -ld $MATLAB/etc/license.dat 2>/dev/null`
        if [ "$lcmd" = "" ]; then
	    create_skeleton=1
        fi
    fi 
#----------------------------------------------------------------------------
echo "  |                                                                          |"
#----------------------------------------------------------------------------
    if [ "$create_skeleton" = "1" ]; then
        mesg="  | Creating skeleton 'license.dat' file . . . . . . . . [please wait] "
        . $dir/$echon_sh
#****************************************************************************
#----------------------------------------------------------------------------
# usage:        mklicdat.sh
#----------------------------------------------------------------------------
#
#                       Get template license only [-o option]
#                       -------------------------
#
# We assume that we have not changed directories since the orginal
# invocation (at $WD).
#
    file="$licdat_path"
#
    cmd="hostname"
    . $dir/$searchp_sh
    if [ "$pathlist" = "" ]; then
        Hostname=`uname -n`
    else
        Hostname=`hostname`
    fi
#
    HostID=`$etc_lmhostid_exe | sed -e "1d" -e '2s/^[^"]*"//' -e '2s/".*//'`
#
# Look at full file path. Automounting can't be handled here so we will
# not deal with it. If the path is of the form $dir/etc/file then dir
# will be used as the value of '$MATLAB' when filling out the template file.
# If it is a directory then the same algorithm is used where file
# is license.dat. Otherwise, a value of "<unknown>" will be used for '$MATLAB'.
#
    filetype=0
    (cd $file) > /dev/null 2>&1
    if [ $? -eq 0 ]; then
	filetype=1
	basedir=`(cd $file; /bin/pwd)`
	basefile='license.dat'
    else
        basedir=`expr "//$file" : '//\(.*\)/.*' \| '.'`
	basedir=`(cd $basedir; /bin/pwd)`
        basefile=`expr "//$file" : '.*/\(.*\)' \| "$file"`
    fi
    if [ "$file" = "/" ]; then
	pmatlab='<unknown>'
    else
        pmatlab=`expr "//$basedir" : '//\(.*\)/.*'`
	if [ "$pmatlab" = "" ]; then 
	    pmatlab=/
	fi
        basefile2=`expr "//$basedir" : '.*/\(.*\)'`
	if [ "$basefile2" != "etc" ]; then
	    pmatlab='<unknown>'
	fi
    fi
#
    if [ "$file" = "/" ]; then
	file=""
    fi
    if [ "$basedir" = "/" ]; then
	basedir=""
    fi
    if [ ! -f $basedir/$basefile ]; then
	(sed -e "s%|>HOSTNAME<|%$Hostname%g" \
   	     -e "s%|>HOST_ID<|%$HostID%g" \
	     -e "s%|>MATLAB_ROOT_DIR<|%$pmatlab%g" \
   	    $etc_license_dat > $basedir/$basefile) 2>$temp_file
	state=$?
        msg="`cat $temp_file`"
	rm -f $temp_file
	if [ $state -ne 0 ]; then
	    :
	else
	    chmod 644 $basedir/$basefile
#----------------------------------------------------------------------------
echo '      |'
#----------------------------------------------------------------------------
	    created=1
	fi
    else
	:
    fi
#****************************************************************************
    fi
#----------------------------------------------------------------------------
#
# Try to merge in a $MATLAB/license.dat file if the skeleton has been just
# been created.
#
    if [ "$created" = "1" ]; then
	File=$MATLAB/license.dat
	. $dir/$actualp_sh
	if [ "$Filepath" != "" ]; then
            mesg="  | Merging in 'license.dat' file found in "'$MATLAB . . . [please wait] '
            . $dir/$echon_sh
	    merged=1
	    cat $MATLAB/etc/license.dat > $temp_file
	    echo 'EOF' >> $temp_file
	    cat $temp_file $MATLAB/license.dat | awk '
#----------------------------------------------------------------------------
    BEGIN { state = 0; nc = 0; nf = 0 }
    $1 == "EOF" { state = 1; next }
	{ if (state == 0 ) { 
	      if ($1 == "SERVER") server = $0
	      if ($1 == "DAEMON") daemon = $0
	  }
	  else if (substr($1,1,1) == "#" && (index($2,"BEGIN") != 1 && index($2,"END") != 1)) {
	      nc = nc + 1
	      comment[nc] = $0
	  }
	  else if ($1 == "FEATURE") {
	      nf = nf + 1
	      feature[nf] = $0
	  }
	  next
	}
    END { if (nc > 0) for (i = 1; i <= nc; i = i + 1) print comment[i]
          if (server != "") print server
	  if (daemon != "") print daemon
          if (nf > 0) for (i = 1; i <= nf; i = i + 1) print feature[i]
	}' > $temp_file2
#----------------------------------------------------------------------------
	    mv $temp_file2 $MATLAB/etc/license.dat
#----------------------------------------------------------------------------
echo '      |'
#----------------------------------------------------------------------------
	    rm -f $MATLAB/license.dat > /dev/null 2>&1
	fi
    fi
#
# Check TMW_Archive lines. Variables returned:
#
#    n_archivel = the number of possible TMW_Archive FEATURE
#                 lines
#
#    ngood_archivel = the number of good TMW_Archive FEATURE
#                     lines
#
#    temp_file4 = all TMW_Archive lines and any dear output
#
#    dear_exist = 0 if dear does not exist
#                 1 if dear does exists
#
#    license_file_exist = 0 if license file does not exist
#			  1 if license file exists
#
#----------------------------------------------------------------------------
    . $dir/$cktmw_al_sh
     if [ "$n_archivel" = "1" ]; then
	if [ "$ngood_archivel" = "1" ]; then
#----------------------------------------------------------------------------
echo "  | Checking TMW_Archive FEATURE line. . . . . . . . . . [valid]             | "
#----------------------------------------------------------------------------
	else
#----------------------------------------------------------------------------
echo "  | Checking TMW_Archive FEATURE line. . . . . . . . . . [invalid]           | "
#----------------------------------------------------------------------------
	fi
#----------------------------------------------------------------------------
    else
        nfail_archivel=`expr $n_archivel - $ngood_archivel`
#----------------------------------------------------------------------------
echo "  | Checking TMW_Archive FEATURE line(s) . . . . . . . .                     | "
echo "  |                                                                          |"
echo "        $ngood_archivel    valid TMW_Archive FEATURE line(s)"
echo "        $nfail_archivel  invalid TMW_Archive FEATURE line(s)"
#----------------------------------------------------------------------------
    fi
echo "  |                                                                          |"
echo "    MATLAB =  $MATLAB"
echo "  |                                                                          |"
echo "  ----------------------------------------------------------------------------"
#----------------------------------------------------------------------------
#
    if [ "$batch" = "0" ]; then
        mesg='Please press <return> to continue: '
        . $dir/$echon_sh
        read ans
    else
        echo 'Please press <return> to continue: '
    fi
    firstpass=1
#
# Loop for flipping back to the Summary
#
    while true
    do
        . $dir/$clearsc_sh
#----------------------------------------------------------------------------
echo ' 		         TMW_Archive FEATURE Line Summary'
echo '--------------------------------------------------------------------------------'
#----------------------------------------------------------------------------
        if [ "$license_file_exist" = "0" ]; then
#----------------------------------------------------------------------------
echo ''
echo "    'license.dat'"' file, $MATLAB/etc/license.dat, does not exist . . .'
echo ''
echo "    MATLAB =  $MATLAB"
echo ''
#----------------------------------------------------------------------------
        elif [ "$dear_exist" = "0" ]; then
	    cat $temp_file4
#----------------------------------------------------------------------------
echo ''
echo '    No decryption software found. Looked for . . .'
echo ''
#----------------------------------------------------------------------------
	    if [ "$MODE" = "tape" ]; then
#----------------------------------------------------------------------------
echo '        $MATLAB/update/tape/bin/'"$Arch"'/dear'
echo '        $MATLAB/update/bin/'"$Arch"'/dear'
echo ''
echo "    MATLAB =  $MATLAB"
#----------------------------------------------------------------------------
	    else
#----------------------------------------------------------------------------
echo '        $MATLAB/update/bin/'"$Arch"'/dear'
echo ''
echo "    MATLAB =  $MATLAB"
#----------------------------------------------------------------------------
	    fi
#
# Look for other architectures in $MATLAB/update/bin
#
            other_dear=0
	    (cd $MATLAB/update/bin) > /dev/null 2>&1
            if [ $? -eq 0 ]; then
	        start_archlist=`(cd $MATLAB/update/bin; ls)`
	        if [ "$start_archlist" != "" ]; then
		    archlist=
		    for single_arch in $start_archlist
		    do
		        if [ -f $MATLAB/update/bin/$single_arch/dear ]; then
			    archlist="$archlist $single_arch"
		        fi
		    done
		    if [ "$archlist" != "" ]; then
		        other_dear=1
		    fi
	        fi
	    fi
	    if [ "$other_dear" != "0" ]; then
#----------------------------------------------------------------------------
echo ''
echo '    Found decryption software for other MATLAB supported architectures:'
echo ''
echo "       $archlist"
echo ''
echo '    If decryption is required, please use a system with one of these other'
echo '    architectures to install MATLAB.'
#----------------------------------------------------------------------------
	    else
#----------------------------------------------------------------------------
echo ''
echo '    Found NO decryption software for any other MATLAB supported architectures.'
echo ''
echo '    Please contact:    The MathWorks, Inc. Technical Support:'
echo '                       Phone: 508-653-1415' 
echo '                       FAX:   508-653-2997'
echo '                       Email: support@mathworks.com'
#----------------------------------------------------------------------------
	    fi
	elif [ "$n_archivel" = "0" ]; then
#----------------------------------------------------------------------------
echo ''
echo '    No TMW_Archive FEATURE lines in $MATLAB/etc/license.dat . . .'
echo ''
echo "    MATLAB =  $MATLAB"
echo ''
#----------------------------------------------------------------------------
	else
	    cat $temp_file4
        fi
	if [ "$firstpass" = "1" ]; then
	    if [ "$ftp_select" = "0" ]; then
#----------------------------------------------------------------------------
echo '--------------------------------------------------------------------------------'
echo "-> Only toolboxes listed under valid TMW_Archive FEATURE lines will be"
echo "-> decrypted. To make changes type 'n' at the prompt and follow the directions"
echo "-> on the next screen."
echo '--------------------------------------------------------------------------------'
#----------------------------------------------------------------------------
                if [ "$batch" = "0" ]; then
	            mesg='Continue? ([y]/n) '
                    . $dir/$echon_sh
                    read ans
                    if [ `expr "//$ans" : '//[Nn].*'` -eq 0 ]; then
		        break
	            fi
		else
	            echo 'Continue? ([y]/n) y'
		    break
		fi
	    else
#----------------------------------------------------------------------------
echo '--------------------------------------------------------------------------------'
echo "-> Only toolboxes listed under valid TMW_Archive FEATURE lines will appear"
echo "-> selected on the 'Select MATLAB Products' screens that follow."
echo '--------------------------------------------------------------------------------'
#----------------------------------------------------------------------------
    	        mesg='Please press <return> to continue: '
    	        . $dir/$echon_sh
    	        read ans
		break
	    fi
	else
#----------------------------------------------------------------------------
echo '--------------------------------------------------------------------------------'
    	    mesg='Please press <return> to continue: '
    	    . $dir/$echon_sh
    	    read ans
#----------------------------------------------------------------------------
	fi
	. $dir/$clearsc_sh
#----------------------------------------------------------------------------
echo "               Directions for Preparing Your 'license.dat' File"
echo "  ----------------------------------------------------------------------------"
echo "  | 1. If there are toolboxes licensed to you that you cannot decrypt then   |"
echo "  |    you will need a correct TMW_Archive FEATURE line. Type 'b' at the     |"
echo "  |    prompt to review the TMW_Archive FEATURE Line Summary screen again.   |"
echo "  |    To get a new 'license.dat' file first get the 'lmhostid' string for   |"
echo "  |    your license server.                                                  |"
echo "  |                                                                          |"
#----------------------------------------------------------------------------
    if [ "$firstpass" = "1" ]; then
#
# Code taken from getlmhid.sh
#
        file=license.dat
#
        cmd="hostname"
        . $dir/$searchp_sh
        if [ "$pathlist" = "" ]; then
            Hostname=`uname -n`
        else
            Hostname=`hostname`
        fi
#
        HostID=`$etc_lmhostid_exe | sed -e "1d" -e '2s/^[^"]*"//' -e '2s/".*//'`
        hostidl="       The 'lmhostid' for this host ($Hostname) is:  $HostID"
	firstpass=0
    fi
#----------------------------------------------------------------------------
echo "$hostidl"
#----------------------------------------------------------------------------
echo "  |    If your license server is another host, login to that host and in     |"
echo "  |    the MATLAB root directory run the command, './install_matlab -l'.     |"
echo "  |                                                                          |"
echo "  |    Then contact:         The MathWorks, Inc.        (508) 653-1415       |"
echo "  |                   email: service@mathworks.com  FAX (508) 653-2997       |"
echo "  |                                                                          |"
echo "  | 2. Add or replace any outdated or incorrect FEATURE lines including the  |"
echo '  |    TMW_Archive FEATURE line in the $MATLAB'"/etc/license.dat file using    |"
echo "  |    your most recent 'license.dat' file.                                  |"
echo "  |                                                                          |"
echo "       MATLAB =  $MATLAB"
echo "  |                                                                          |"
echo "  | 3. Run the 'install_matlab' command again.                               |"
echo "  ----------------------------------------------------------------------------"
#----------------------------------------------------------------------------
	mesg="To go back a screen (type b) or to exit (press <return>) >"
        . $dir/$echon_sh
        read ans
        if [ `expr "//$ans" : '//[b].*'` -eq 0 ]; then
	    . $dir/$cleanup_sh
            exit 1
	fi
    done
