#
# usage:        twelcome.sh
#
# abstract:     This Bourne Shell script prints the welcome screen,
#		followed by the TMW_Archive INCREMENT line screen. If you
#		do not continue then print the exit screen and toggle with
#		the TMW_Archive INCREMENT line screen.
#
# note(s):      1. This routine must be called using a . (period)
#
#		2. Welcome screen:
#
#  ----------------------------------------------------------------------------
#  | This install program has two parts:                                      |
#  |                                                                          |
#  |     *  MATLAB Product Selection and File Extraction                      |
#  |     *  MATLAB Installation                                               |
#  |                                                                          |
#  | You may select and extract any product. However, all toolboxes are       |
#  | encrypted and require a further decryption step during MATLAB            |
#  | Installation. You need a 'license.dat' file, $MATLAB/etc/license.dat,    |
#  | with the correct 'TMW_Archive' INCREMENT line to decrypt your toolboxes.   |
#----------------
#  |                                                                          |
#  | Creating skeleton 'license.dat' file . . . [please wait]                 | 
#----------------
#  | Merging in 'license.dat' file found in $MATLAB . . . [please wait]       |
#----------------
#  | Checking TMW_Archive INCREMENT line . . . . [valid/invalid]                |
#----------------
#  | Checking TMW_Archive INCREMENT line(s) . . . [please wait]                 |
#  |                                                                          |
#        1  valid   TMW_Archive INCREMENT line(s)
#        0  invalid TMW_Archive INCREMENT line(s)
#  |                                                                          |
#  | Here,                                                                    |
#  |                                                                          |
#       MATLAB =  ...                  
#  |                                                                          |
#  ----------------------------------------------------------------------------
#
#		2a. TWM_Archive line(s) screen
#
#  Please press <return> to continue: 
#
#		     TMW_Archive INCREMENT Line Summary
#  ----------------------------------------------------------------------------
#  cat <TMW_Archive INCREMENT line Error file>
#----------------
#
#    'license.dat' file, $MATLAB/etc/license.dat, does not exist . . .
#
#    MATLAB =  ...                  
#
#----------------
#
#    No TMW_Archive INCREMENT 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-647-7000
#         		FAX:   508-647-7201
#         		Email: support@mathworks.com
#----------------
#  ----------------------------------------------------------------------------
#  -> Only toolboxes listed under valid TMW_Archive INCREMENT lines will be 
#  -> decrypted. To make changes type 'n' at the prompt and follow the directions
#  -> on the next screen.
#  ----------------------------------------------------------------------------
#  Continue? ([y]/n)
#----------------
#  ----------------------------------------------------------------------------
#----------------
#
#		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 . . .>
# INCREMENT TWM_Archive   MLM 5 01-jan-1994   0 0123456789ABCDEFGHIJ "81" 7260d091
# INCREMENT MATLAB        MLM 5 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
# INCREMENT TMW_Archive      MLM 5 01-feb-1996   0 3B0D539384183D29B4D9 "81" DEMO
# INCREMENT MATLAB           MLM 5 01-feb-1996   1 2B3D1333C2453DDDEFBC ""
# INCREMENT Signal_Toolbox   MLM 5 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/INCREMENT 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 INCREMENT line. Type 'b' at the     |
#  |    prompt to review the TMW_Archive INCREMENT 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) 647-7000       |
#  |                   email: service@mathworks.com                           |
#  |                                                                          |
#  | 2. Add or replace any outdated or incorrect INCREMENT lines including the  |
#  |    TMW_Archive INCREMENT 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-1998 The Mathworks, Inc. All Rights Reserved.
# $Revision: 1.16 $  $Date: 1998/12/24 17:31:50 $
#----------------------------------------------------------------------------
#
#                               Welcome message
#                               ---------------
#
#----------------------------------------------------------------------------
echo "  ----------------------------------------------------------------------------"
echo "  | This install program has two parts:                                      |"
echo "  |                                                                          |"
echo "  |     *  MATLAB Product Selection and File Extraction                      |"
echo "  |     *  MATLAB Installation                                               |"
echo "  |                                                                          |"
echo "  | You may select and extract any product. However, all toolboxes are       |"
echo "  | encrypted and require a further decryption step during MATLAB            |"
echo "  | Installation. You need a 'license.dat'"' file, $MATLAB/etc/license.dat,    |'
echo "  | with the correct 'TMW_Archive' INCREMENT line to decrypt your toolboxes. |"
#----------------------------------------------------------------------------
    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") || ($1 == "INCREMENT") ){
	      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 INCREMENT
#                 lines
#
#    ngood_archivel = the number of good TMW_Archive INCREMENT
#                     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 INCREMENT line. . . . . . . . . . [valid]           | "
#----------------------------------------------------------------------------
	else
#----------------------------------------------------------------------------
echo "  | Checking TMW_Archive INCREMENT line. . . . . . . . . . [invalid]         | "
#----------------------------------------------------------------------------
	fi
#----------------------------------------------------------------------------
    else
        nfail_archivel=`expr $n_archivel - $ngood_archivel`
#----------------------------------------------------------------------------
echo "  | Checking TMW_Archive INCREMENT line(s) . . . . . . . .                   | "
echo "  |                                                                          |"
echo "        $ngood_archivel    valid TMW_Archive INCREMENT line(s)"
echo "        $nfail_archivel  invalid TMW_Archive INCREMENT line(s)"
#----------------------------------------------------------------------------
    fi
echo "  |                                                                          |"
echo "    MATLAB =  $MATLAB"
echo "  |                                                                          |"
echo "  ----------------------------------------------------------------------------"
#----------------------------------------------------------------------------
#
    mesg='Please press <return> to continue: '
    . $dir/$echon_sh
    read ans
    firstpass=1
#
# Loop for flipping back to the Summary
#
    while true
    do
        . $dir/$clearsc_sh
#----------------------------------------------------------------------------
echo ' 		         TMW_Archive INCREMENT 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-647-7000' 
echo '                       FAX:   508-647-7201'
echo '                       Email: support@mathworks.com'
#----------------------------------------------------------------------------
	    fi
	elif [ "$n_archivel" = "0" ]; then
#----------------------------------------------------------------------------
echo ''
echo '    No TMW_Archive INCREMENT lines in $MATLAB/etc/license.dat . . .'
echo ''
echo "    MATLAB =  $MATLAB"
echo ''
#----------------------------------------------------------------------------
	else
	    cat $temp_file4
        fi
	if [ "$firstpass" = "1" ]; then
#----------------------------------------------------------------------------
echo '--------------------------------------------------------------------------------'
echo "-> Only toolboxes listed under valid TMW_Archive INCREMENT lines will be"
echo "-> decrypted. To make changes type 'n' at the prompt and follow the directions"
echo "-> on the next screen."
echo '--------------------------------------------------------------------------------'
#----------------------------------------------------------------------------
	    mesg='Continue? ([y]/n) '
            . $dir/$echon_sh
            read ans
            if [ `expr "//$ans" : '//[Nn].*'` -eq 0 ]; then
		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 INCREMENT line. Type 'b' at the   |"
echo "  |    prompt to review the TMW_Archive INCREMENT 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) 647-7000       |"
echo "  |                   email: service@mathworks.com                           |"
echo "  |                                                                          |"
echo "  | 2. Add or replace any outdated or incorrect INCREMENT lines including    |"
echo '  |    the TMW_Archive INCREMENT line in the $MATLAB'"/etc/license.dat file    |"
echo "  |    using 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
