#
# usage:        cktmw_al.sh
#
# abstract:     This Bourne Shell script determines:
#
#		VENDOR_STRINGS = a space separated list of vendor strings
#				 from valid TMW_Archive INCREMENT without
#				 any quotes
#				 
#		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 dear exists
#			     1 dear does not exist 
#
#    		license_file_exist = 0 if license file does not exist
#                         	     1 if license file exists
#
# note(s):      1. This routine must be called using a . (period)
#
#		2. Format of temp_file4
#
#---------------------------------------------------------------------------
# 1. INCREMENT TMW_Archive MLM 5 01-feb-1996 0 DB1DC3FAB7D6BE932644 "10003" 7260d091
#
#        OK . . .
#
# 2. INCREMENT TMW_Archive MLM 5 01-jan-1994 0 0123456789ABCDEFGHIJ "81" 7260d091
#
#        Error: License checkout failed.
#               Software Expired.
#
#---------------------------------------------------------------------------
#
#		3. dear is used to check whether the TMW_Archive is valid.
#
#
#		   CDROM   (SHOULD NEVER HAPPEN!)
#
#        		$dear
#
#		   tape (ufs)
#
#        		$MATLAB/update/tape/bin/$ARCH/dear
#        		$MATLAB/update/bin/$ARCH/dear
#
#		   non tape (ufs)
#
#        		$MATLAB/update/bin/$ARCH/dear
#
# file(s):	1. temp_file2a - all the TMW_Archive lines
#	   	2. temp_file2b - license file for each individual TMW_Archive
#				 line
#		3. temp_file3  - a. output from dear
#				 b. at the end file to source to assign
#				    variables in the current environment
#		4. temp_file4  - all TMW_Archive lines and any dear output
#
# Copyright (c) 1995-1998 The Mathworks, Inc. All Rights Reserved.
# $Revision: 1.19 $  $Date: 1998/12/24 17:31:28 $
#----------------------------------------------------------------------------
#
#                       Check TMW_Archive lines
#                       -----------------------
#
    VENDOR_STRINGS=
    n_archivel=0
    ngood_archivel=0
    dear_exist=1
    license_file_exist=1
# 
    if [ "$MODE" = "tape" ]; then
	File=$MATLAB/etc/license.dat
	. $dir/$actualp_sh
        if [ "$Filepath" != "" ]; then
#
# Get all TMW_Archive lines that do not start with a #.
#
            cat /dev/null > $temp_file2a 2>&1
	    cat $MATLAB/etc/license.dat | awk '
#--------------------------------------------------------------------------
    BEGIN { block = 0; line = ""
            backslash = sprintf ("%c", 92)   # set backslash
	  }
          { if (substr($NF,length($NF),1) != backslash) {
		if (block == 1) {
		    print line $0
		    line = ""
		    block = 0
		}
		else
		    print
		next
	    }
# find index of last backlash
	    for (i = length($0); i > 0; i = i - 1)
		if (substr($0,i,1) == backslash)
		    break
	    if (block == 0)
		block = 1
	    line = line substr($0,1,i-1)
	}
    END { if (block == 1) print line }' | awk '
#--------------------------------------------------------------------------
    $1 == "FEATURE" && $2 == "TMW_Archive"   { print }
    $1 == "INCREMENT" && $2 == "TMW_Archive" { print }' > $temp_file2a
#--------------------------------------------------------------------------
#
# Check on dear
#
	    if [ -f $TAPE_DIR/bin/$ARCH/dear.cmp ]; then
		(cd $TAPE_DIR/bin/$ARCH
		 mv dear.cmp dear.Z
		 uncompress dear.Z
		 chmod +x dear)
		dear_cmd=$TAPE_DIR/bin/$ARCH/dear
	    elif [ -f $TAPE_DIR/bin/$ARCH/dear ]; then
		dear_cmd=$TAPE_DIR/bin/$ARCH/dear
	    elif [ -f $MATLAB/update/bin/$ARCH/dear ]; then
		dear_cmd=$MATLAB/update/bin/$ARCH/dear
	    else
    		dear_exist=0
	    fi
#
# Call dear:
#
#    MATLAB is in the environment already.
#    Set LM_LICENSE_FILE to $temp_file2b and put in the environment.
#
            LM_LICENSE_FILE=$temp_file2b; export LM_LICENSE_FILE
#
            cat /dev/null > $temp_file4 2>&1
	    (while read line
	     do
		 n_archivel=`expr $n_archivel + 1`
		 echo "$line" > $temp_file2b
    		 echo "$n_archivel. $line" | awk '
#--------------------------------------------------------------------------
    { printf "%d.", $1
      for (i = 2; i <= NF; i = i + 1) printf " %s", $i 
      printf "\n" }' >> $temp_file4
#--------------------------------------------------------------------------
	         if [ "$dear_exist" = "1" ]; then
#--------------------------------------------------------------------------
    echo '' >> $temp_file4
#--------------------------------------------------------------------------
		     status=0
		     $dear_cmd > $temp_file3 2>&1
		     if [ $? -ne 0 ]; then
			status=1
		     fi
		     vendor_string=`echo "$line" | awk '
#--------------------------------------------------------------------------
    BEGIN { squote = sprintf ("%c", 39)   # set single quote
	    dquote = sprintf ("%c", 34)   # set double quote
           }
    { vendorstring = $8
      ix = index(vendorstring,squote)
      while ( ix != 0 ) {
	  vendorstring = substr(vendorstring,1,ix-1) substr(vendorstring,ix + 1)
          ix = index(vendorstring,squote)
      }
      ix = index(vendorstring,dquote)
      while ( ix != 0 ) {
	  vendorstring = substr(vendorstring,1,ix-1) substr(vendorstring,ix + 1)
          ix = index(vendorstring,dquote)
      }
      print vendorstring }'` 
#--------------------------------------------------------------------------
		     . $dir/$decryptl_sh			
		     if [ "$status" = "1" ]; then
    			 cat $temp_file3 | awk '
#--------------------------------------------------------------------------
    NR == 1       { printf "   Error: %-s\n", $0; next}
    $1 != "Press" { printf "          %-s\n", $0 }' >> $temp_file4
#--------------------------------------------------------------------------
			 mesg="   Cannot decrypt:"
		     else
#--------------------------------------------------------------------------
    echo '   Valid . . .' >> $temp_file4
#--------------------------------------------------------------------------
			 mesg="   Can decrypt:"
		         ngood_archivel=`expr $ngood_archivel + 1`
		         if [ "$ngood_archivel" = "1" ]; then
			     VENDOR_STRINGS="$vendor_string"
		         else
			     VENDOR_STRINGS="$VENDOR_STRINGS $vendor_string"
		         fi
		     fi
#--------------------------------------------------------------------------
# maximum of four items per line
#
		     if [ "$DECRYPT_PRODUCTS" != "" ]; then
		         ni=1 
		         for prod in $DECRYPT_PRODUCTS
			 do
			     if [ "$ni" = "5" ]; then
				 ni=1
#--------------------------------------------------------------------------
    echo "$mesg" >> $temp_file4
#--------------------------------------------------------------------------
				 if [ "$status" = "1" ]; then
			             mesg="                  "
				 else
			 	     mesg="               "
				 fi
			     else
				 ni=`expr $ni + 1`
			     fi
			     mesg="$mesg $prod"
			 done
		     fi
#--------------------------------------------------------------------------
    echo "$mesg" >> $temp_file4
    echo '' >> $temp_file4
#--------------------------------------------------------------------------
		 fi
	     done
#--------------------------------------------------------------------------
echo '#!/bin/sh' > $temp_file3
echo n_archivel=$n_archivel >> $temp_file3
echo ngood_archivel=$ngood_archivel >> $temp_file3
echo VENDOR_STRINGS="'"$VENDOR_STRINGS"'" >> $temp_file3
#--------------------------------------------------------------------------
            ) < $temp_file2a
	    chmod +x $temp_file3
	    . $temp_file3
	else
    	    license_file_exist=0
	fi
    else
#
# Other than 'collective tape'
#
	if [ "$CD_ROOT" != "" ]; then
            . $dir/$clearsc_sh
            File=license.dat
            Filex='license.dat'
            . $dir/$actualp_sh
            if [ "$Filepath" = "" ]; then
                File=etc/license.dat
                Filex='etc/license.dat'
                . $dir/$actualp_sh
            fi
            if [ "$Filepath" != "" ]; then
#--------------------------------------------------------------------------
    echo ''
    echo 'Please specify the path to your license file? ['"$Filex"']'
    echo 'Press return to search the default . . .'
    echo ''
#--------------------------------------------------------------------------
            else
#--------------------------------------------------------------------------
    echo ''
    echo 'Please specify the path to your license file? []'
    echo 'Press return to exit . . .'
    echo ''
#--------------------------------------------------------------------------
            fi
	    Filepath_default=$Filepath
            while true
            do
                mesg='Path>  '
                . $dir/$echon_sh
                read ans
                if [ `expr "//$ans" : '//[      ]*$'` -eq 0 ]; then
                    File=$ans
                elif [ "$Filepath" != "" ]; then
                    break
                else
                    . $dir/$cleanup_sh
                    exit 0
                fi
                . $dir/$actualp_sh
                if [ "$Filepath" != "" ]; then
                    break
                else
#--------------------------------------------------------------------------
    echo ''
    echo '    Sorry! Path does not exist . . .'
    echo '           Please try again.'
    echo ''
#--------------------------------------------------------------------------
		    Filepath=$Filepath_default
                fi
            done
        else
            File=$MATLAB/license.dat
            Filex='$MATLAB/license.dat'
            . $dir/$actualp_sh
            if [ "$Filepath" = "" ]; then
                File=$MATLAB/etc/license.dat
                Filex='$MATLAB/etc/license.dat'
                . $dir/$actualp_sh
            fi
	    if [ "$batch" != "1" -a "$check_tmw_al" = "1" ]; then
                . $dir/$clearsc_sh
                if [ "$Filepath" != "" ]; then
#--------------------------------------------------------------------------
    echo ''
    echo 'Please specify the path to your license file? ['"$Filex"']'
    echo 'Press return to search the default . . .'
    echo ''
#--------------------------------------------------------------------------
                else
#--------------------------------------------------------------------------
    echo ''
    echo 'Please specify the path to your license file? []'
    echo 'Press return to exit . . .'
    echo ''
#--------------------------------------------------------------------------
                fi
	        Filepath_default=$Filepath
                while true
                do
                    mesg='Path>  '
                    . $dir/$echon_sh
                    read ans
                    if [ `expr "//$ans" : '//[      ]*$'` -eq 0 ]; then
                        File=$ans
                    elif [ "$Filepath" != "" ]; then
                        break
                    else
                        . $dir/$cleanup_sh
                        exit 0
                    fi
                    . $dir/$actualp_sh
                    if [ "$Filepath" != "" ]; then
                        break
                    else
#--------------------------------------------------------------------------
    echo ''
    echo '    Sorry! Path does not exist . . .'
    echo '           Please try again.'
    echo ''
#--------------------------------------------------------------------------
		        Filepath=$Filepath_default
                    fi
                done
            fi
	fi
        if [ "$Filepath" != "" ]; then
#
# Get all TMW_Archive lines that do not start with a #.
#
            cat /dev/null > $temp_file2a 2>&1
	    grep 'TMW_Archive' $Filepath | awk '
#--------------------------------------------------------------------------
    $1 == "FEATURE" && $2 == "TMW_Archive" { print } 
    $1 == "INCREMENT" && $2 == "TMW_Archive" { print }' > $temp_file2a
#--------------------------------------------------------------------------
#
# Check on dear
#
	    if [ "$CD_ROOT" != "" ]; then
	        if [ -f "$dear" ]; then
		    dear_cmd="$dear"
	        elif [ -f $MATLAB/update/bin/$ARCH/dear ]; then
		    dear_cmd=$MATLAB/update/bin/$ARCH/dear
		else
		    dear_exist=0
		fi
	    elif [ -f $MATLAB/update/bin/$ARCH/dear ]; then
		dear_cmd=$MATLAB/update/bin/$ARCH/dear
	    else
		dear_exist=0
	    fi
#
# Call dear:
#
#    MATLAB is in the environment already.
#    Set LM_LICENSE_FILE to $temp_file2b and put in the environment.
#
            LM_LICENSE_FILE=$temp_file2b; export LM_LICENSE_FILE
#
            cat /dev/null > $temp_file4 2>&1
	    (while read line
	     do
		 n_archivel=`expr $n_archivel + 1`
		 echo "$line" > $temp_file2b
    		 echo "$n_archivel. $line" | awk '
#--------------------------------------------------------------------------
    { printf "%d.", $1
      for (i = 2; i <= NF; i = i + 1) printf " %s", $i 
      printf "\n" }' >> $temp_file4
#--------------------------------------------------------------------------
	    	 if [ "$dear_exist" = "1" ]; then
#--------------------------------------------------------------------------
    echo '' >> $temp_file4
#--------------------------------------------------------------------------
		     status=0
		     (eval "'$dear_cmd'") > $temp_file3 2>&1
		     if [ $? -ne 0 ]; then
			status=1
		     fi
		     vendor_string=`echo "$line" | awk '
#--------------------------------------------------------------------------
    BEGIN { squote = sprintf ("%c", 39)   # set single quote
	    dquote = sprintf ("%c", 34)   # set double quote
           }
    { vendorstring = $8
      ix = index(vendorstring,squote)
      while ( ix != 0 ) {
	  vendorstring = substr(vendorstring,1,ix-1) substr(vendorstring,ix + 1)
          ix = index(vendorstring,squote)
      }
      ix = index(vendorstring,dquote)
      while ( ix != 0 ) {
	  vendorstring = substr(vendorstring,1,ix-1) substr(vendorstring,ix + 1)
          ix = index(vendorstring,dquote)
      }
      print vendorstring }'` 
#--------------------------------------------------------------------------
		     . $dir/$decryptl_sh			
		     if [ "$status" = "1" ]; then
    			 cat $temp_file3 | awk '
#--------------------------------------------------------------------------
    NR == 1       { printf "   Error: %-s\n", $0; next}
    $1 != "Press" { printf "          %-s\n", $0 }' >> $temp_file4
#--------------------------------------------------------------------------
			 mesg="   Cannot decrypt:"
		     else
#--------------------------------------------------------------------------
    echo '   Valid . . .' >> $temp_file4
#--------------------------------------------------------------------------
			 mesg="   Can decrypt:"
		         ngood_archivel=`expr $ngood_archivel + 1`
		         if [ "$ngood_archivel" = "1" ]; then
			     VENDOR_STRINGS="$vendor_string"
		         else
			     VENDOR_STRINGS="$VENDOR_STRINGS $vendor_string"
		         fi
		     fi
#--------------------------------------------------------------------------
# maximum of four items per line
#
		     if [ "$DECRYPT_PRODUCTS" != "" ]; then
		         ni=1 
		         for prod in $DECRYPT_PRODUCTS
			 do
			     if [ "$ni" = "5" ]; then
				 ni=1
#--------------------------------------------------------------------------
    echo "$mesg" >> $temp_file4
#--------------------------------------------------------------------------
				 if [ "$status" = "1" ]; then
			             mesg="                  "
				 else
			 	     mesg="               "
				 fi
			     else
				 ni=`expr $ni + 1`
			     fi
			     mesg="$mesg $prod"
			 done
		     fi
#--------------------------------------------------------------------------
    echo "$mesg" >> $temp_file4
    echo '' >> $temp_file4
#--------------------------------------------------------------------------
	    	 fi
	     done
#--------------------------------------------------------------------------
echo '#!/bin/sh' > $temp_file3
echo n_archivel=$n_archivel >> $temp_file3
echo ngood_archivel=$ngood_archivel >> $temp_file3
echo VENDOR_STRINGS="'"$VENDOR_STRINGS"'" >> $temp_file3
#--------------------------------------------------------------------------
            ) < $temp_file2a
	    chmod +x $temp_file3
	    . $temp_file3
	else
    	    license_file_exist=0
	fi
    fi
