#
# usage:        fpdselct.sh
#
# abstract:     This Bourne Shell script selects which products
#		are to be selected for ftp and determines the ftp
#		download list.
#
# note(s):      1. This routine must be called using a . (period)
#
#		2. External screen format
#
#------------------------------------------------------------------------------
#                 Select MATLAB Products [screen 1 of 3]
#-------------------------------------------------------------------------
#  1      - arch.sun4                  13   * - tbx.matlab (5.1)          
#  2    * - arch.sol2                  14     - tbx.simulink (2.2)        
#  3      - arch.hp700                 15     - tbx.control (4.0.1)       
#  4      - arch.alpha                 16     - tbx.ident (4.0.3)         
#  5      - arch.ibm_rs                17     - tbx.robust (2.0.4)        
#  6      - arch.sgi                   18     - tbx.optim (1.5.2)         
#  7      - arch.sgi64                 19     - tbx.splines (1.1.4)       
#  8      - arch.lnx86                 20     - tbx.signal (4.1)          
#                                      21     - tbx.mutools (3.0.2)       
#  9    * - product                    22     - tbx.map (1.0)             
#  10   * - help.english (5.2)         23     - tbx.nnet (2.0.4)          
#  11     - help.japanese (5.1)        24     - tbx.rtw (2.2.0)           
#                                      25     - tbx.symbolic (2.0.1)      
#  12   * - matlab (5.2beta1)          26     - tbx.images (2.1)          
#                                      [next screen]                      
#-------------------------------------------------------------------------
#To select a product type:    the item number, a '*' appears next to it  
#To deselect a product type:  minus the item number, and the '*' disappears
#For help type:               h
#For the next screen press:   <return>
#-------------------------------------------------------------------------
#>
#    
#		3. Internal screen file format
#
#-------------------------------------------------------------------------
# 1   , 13  | 1      - arch.sun4                  13     - tbx.matlab (5.1)          
# 2   , 14  | 2      - arch.sol2                  14     - tbx.simulink (2.2)        
# 3   , 15  | 3      - arch.hp700                 15     - tbx.control (4.0.1)       
# 4   , 16  | 4      - arch.alpha                 16     - tbx.ident (4.0.3)         
# 5   , 17  | 5      - arch.ibm_rs                17     - tbx.robust (2.0.4)        
# 6   , 18  | 6      - arch.sgi                   18     - tbx.optim (1.5.2)         
# 7   , 19  | 7      - arch.sgi64                 19     - tbx.splines (1.1.4)       
# 8   , 20  | 8      - arch.lnx86                 20     - tbx.signal (4.1)          
# 0   , 21  |                                     21     - tbx.mutools (3.0.2)       
# 9   , 22  | 9      - product                    22     - tbx.map (1.0)             
# 10  , 23  | 10     - help.english (5.2)         23     - tbx.nnet (2.0.4)          
# 11  , 24  | 11     - help.japanese (5.1)        24     - tbx.rtw (2.2.0)           
# 0   , 25  |                                     25     - tbx.symbolic (2.0.1)      
# 12  , 26  | 12     - matlab (5.2beta1)          26     - tbx.images (2.1)          
# 0   , 0   |                                     [next screen]                      
#-------------------------------------------------------------------------
#		   |<- (18)                            |<- (54)
# 12345678901
# 123456789012345678901234567890123456789012345678901234
#
#		The
#
#		4. Variables
#
#		   Supplied:
#
#		   DECRYPT_PRODUCTS = list of products by name that can be
#				      decrypted. It could be empty.
#                  DEFAULT_MAP = size is NPRODUCTS. If default selected
#                                than number in product list (file
#                                screen.map) else 0
#                  NSCREENS = number of screens
#                  NARCHES = number of arches
#		   NHELP = number of help languages
#                  NPRODUCTS = number of products
#                  RANGE = endi  i=1,. . .,NSCREENS (top end of each screen)
#                  ARCH_DEPEND_MAP = size is NPRODUCTS. If product
#                                    depends on arch then number in
#                                    product list (file screen.map) else 0
#		   HELP_DEPEND_MAP = size is NPRODUCTS. If product
#                                    depends on doc then number in
#                                    product list (file screen.map) else 0
#
#		   Determined:
#		   
#		   PRODUCTS = size is NPRODUCTS. If product is selected
#			      then number in product list (file screen.map)
#			      else 0
#		   ARCHES = selected arch numbers or empty
#		   
#-------------------------------------------------------------------------
# DEFAULT_MAP='0 2 0 0 0 0 0 0 9 10 0 12 13 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
#	       0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0'
# NSCREENS=3
# NARCHES=8
# NHELP=2
# NPRODUCTS=56
# RANGE='26 54 56'
# ARCH_DEPEND_MAP='0 0 0 0 0 0 0 0 0 0 0 12 13 14 0 0 0 0 0 20 21 0 23 24 25 26
#		   0 0 0 0 0 32 33 34 0 36 37 38 0 0 41 42 0 44 45 46 0 0 0 0 0
#	           0 0 0 0 0'
# HELP_DEPEND_MAP='0 0 0 0 0 0 0 0 0 0 0 12 0 14 15 16 0 18 0 20 0 22 0 24 25
#                  26 27 28 0 0 0 32 33 34 0 0 37 0 0 0 41 0 43 44 45 46 0 48'
#		   0 0 0 0 0 0 0 0'
#-------------------------------------------------------------------------
#
#		5. Errors
#
#	           Basic idea: Make any changes until an error occurs.
#			       Stop at the error and output a message.
#			       Ask the user to continue.
#
#			 + = (select all)
#        		 n = (select n)
#		   	      Out of range
#			      Arch must be set for this selection
#      		       m-n = (select m->n) (either direction)
#			      Bad range
#			      Arch must be set for this range of selectors
#      		      +m-n = (select m->n) (either direction)
#			      Bad range
#			      Arch must be set for this range of selectors
#        	         l = (license defaults)
#        	         - = (deselect all)
#       	        -n = (deselect n)
#		   	      Out of range
#      		      -m-n = (deselect m->n) (either direction)
#			      Bad range
#			      Arch must be set for this range of selectors
#			 b = (back one screen)
# 		  <return> = (next screen if alone)
#			
#		6. Summary screen format
#
#-------------------------------------------------------------------------
#	           Select MATLAB Products [Summary]
# ----------------------------------------------------------------------------
# select:   product help
# arches:   alpha
# products: tbx.mmle3 tbx.symbolic
#	    -> tbx.symbolic(alpha) - not available
# help:     [english]  tbx.mmle3 . . .
#		       ...
#           [japanese] ...
# ----------------------------------------------------------------------------
# '*' means product cannot be decrypted without an appropriate TMW_Archive
#     feature line in your 'license.dat' file.
#
# diskspace:
#
#    FTP files:        14.039 megabytes  (required and recovered if deleted) 
#    installed files:  28.774 megabytes* (less if installed over an old MATLAB)
#
# '*' assumes all extracted toolboxes have been decrypted.
#
# ----------------------------------------------------------------------------
# Creating list of FTP files ($MATLAB/files.ftp) . . .
#
#       MATLAB =  ...
# ----------------------------------------------------------------------------
#
#    or if no products
#
#  
#                  Select MATLAB Products [Summary]
# ----------------------------------------------------------------------------
# arches:
# help:
# products:
# ----------------------------------------------------------------------------
# 
#     No product files!
#  
# ------------------------------------------------------------------------------
# Return to the first screen:    press <return>
# To quit:                       type q
# ------------------------------------------------------------------------------
# >
# 
#-------------------------------------------------------------------------
#
# Note: encrypted files CANNOT be tallied accurately since a few smaller
#       pieces are encrypted. For example, tbx.matlab.common
#
#    encrypted files:   x.xxx megabytes (will remain and cannot be installed)
#
# IMPORTANT NOTE: either matlab for the local arch ($ARCH) or decrypt.$ARCH
#                 for the local arch ($ARCH) must be selected if any
#		  tbx is selected.
#
#		7. Format of screen.map
#
# 1.0        matlab.common                1687552 -> 3110870
#
#
#		8. Support one or more help screens
#
#--------------------------------------------------------------------------
# <press return> (next screen)  b (back a screen)  s (selection screen)
#--------------------------------------------------------------------------
#>
#
# Copyright (c) 1994-1998 by The MathWorks, Inc.
# $Revision: 1.14 $  $Date: 1998/11/11 23:43:55 $
#----------------------------------------------------------------------------
#======================= archlist_all.sh (start) ==========================
#
# usage:        archlist_all.sh
#
# abstract:     This Bourne Shell script creates the variable ARCH_LIST_ALL.
#
# note(s):      1. This file is always imbedded in another script
#
# Copyright (c) 1998 by The MathWorks, Inc.
# $Revision: 1.1 $  $Date: 1998/11/11 23:44:17 $
#----------------------------------------------------------------------------
#
    ARCH_LIST_ALL='sun4 sol2 hp700 alpha ibm_rs sgi sgi64 lnx86'
#======================= archlist_all.sh (end) ============================
#
#//////////////////////////////////////////////////////////////////////////
#                               Main loop
#//////////////////////////////////////////////////////////////////////////
#
#
    PRODUCTS=$DEFAULT_MAP
    skip_rest=0
    while true
    do
#
#//////////////////////////////////////////////////////////////////////////
#                    Screen loop for product selection
#//////////////////////////////////////////////////////////////////////////
#
# Loop for each screen
#
	ns=1
	while [ $ns -le $NSCREENS ]
	do
#
	    while true
	    do
#
# Output the screen
#
#--------
		. $dir/$clearsc_sh
#--------------------------------------------------------------------------
echo "                 Select MATLAB Products [screen $ns of $NSCREENS]"
echo '------------------------------------------------------------------------------'
#//////////////////////////////////////////////////////////////////////////
#                           Output a screen
#//////////////////////////////////////////////////////////////////////////
#--------------------------------------------------------------------------
#
# Input file:
#
#   line:  $PRODUCTS    product list
#   file:  $SCREEN$ns   screen number $ns 
#
# Output: standard output
#
#   Filled in product screen
#
		echo $PRODUCTS | cat - $SCREEN$ns | awk '
#--------------------------------------------------------------------------
    NR == 1 { for (i = 1; i <= NF; i = i + 1) product[i] = $i; next }
            { ncol1 = $1; ncol2 = $3
	      line=$0
	      if ( ncol1 != 0 && product[ncol1] != 0 )
		  line=substr(line,1,17) "*" substr(line,19)
	      if ( ncol2 != 0 && product[ncol2] != 0 )
		  line=substr(line,1,53) "*" substr(line,55)
	      print " " substr(line,12)
	    }'
#--------------------------------------------------------------------------
echo '------------------------------------------------------------------------------'
echo "To select a product:    type the item number, a '*' appears next to it"
echo "To deselect a product:  type minus the item number, and the '*' disappears"
echo "For help:               type h"
echo "For the next screen:    press <return>"
echo '------------------------------------------------------------------------------'
#--------------------------------------------------------------------------
    		if [ "$batch" = "0" ]; then
		    mesg="> "
    		    . $dir/$echon_sh
		    read ans
		else
		    if [ "$skip_rest" != "1" ]; then
		        mesg="> "
    		        . $dir/$echon_sh
		        read ans
		        echo "$ans"
		    else
		        echo "> "
		        ans=""
		    fi
		fi
#		
                if [ "$ans" = "" ]; then
		    if [ "$batch" = "1" ]; then
			skip_rest=1
		    fi
		    break
	        elif [ `expr "//$ans" : '//[ 	]+'` -ne 0 ]; then
		    break
	        elif [ `expr "//$ans" : '//[ 	]*b[ 	]*'` -ne 0 ]; then
		    if [ "$ns" = "1" ]; then
		       ns=0
		    else
		       ns=`expr $ns - 2`
		    fi
		    break
	        elif [ `expr "//$ans" : '//[ 	]*h[ 	]*'` -ne 0 ]; then
#
# loop for help screens
#
		     nhs=1
		     while [ $nhs -le $NHSCREENS ] 
		     do
	    	        . $dir/$clearsc_sh
		        cat ${SCREEN}h$nhs
    			if [ "$batch" = "0" ]; then
			    mesg="> "
    		            . $dir/$echon_sh
		            read ans
		        else
		    	    echo "> "
		    	    ans=""
			fi
#
                        if [ "$ans" = "" ]; then
			    nhs=`expr $nhs + 1`
		            continue
	                elif [ `expr "//$ans" : '//[ 	]+'` -ne 0 ]; then
			    nhs=`expr $nhs + 1`
		            continue
	                elif [ `expr "//$ans" : '//[ 	]*b[ 	]*'` -ne 0 ]; then
		            if [ "$nhs" = "1" ]; then
		               break
		            else
		               nhs=`expr $nhs - 1`
		            fi
	                elif [ `expr "//$ans" : '//[ 	]*s[ 	]*'` -ne 0 ]; then
			    break
			else
#-----------------------------------------------------------------------------
    echo "Error: invalid input ($ans) . . ."
    echo ''
#-----------------------------------------------------------------------------
    			    if [ "$batch" = "0" ]; then
            		        mesg='Please press <return> to continue: '
            		        . $dir/$echon_sh
            		        read ans
			    else
            		        echo 'Please press <return> to continue: '
				ans=""
			    fi
		            continue
			fi
		    done
		    continue
		fi
#//////////////////////////////////////////////////////////////////////////
#                         Apply changes to screen
#//////////////////////////////////////////////////////////////////////////
#
# Attempt to apply changes and check for errors.
#
# Input file: $SCREEN
#
#   See list of variables below.
#
# Output file: ${SCREEN}x
#
#   Sourced after creation.
#
#--------------------------------------------------------------------------
    echo $ns > $SCREEN			# line 1
    echo $NSCREENS >> $SCREEN		# line 2
    echo $RANGE >> $SCREEN		# line 3
    echo $NARCHES >> $SCREEN		# line 4
    echo $NHELP >> $SCREEN              # line 5
    echo $NPRODUCTS >> $SCREEN		# line 6
    echo $DEFAULT_MAP >> $SCREEN	# line 7	
    echo $ARCH_DEPEND_MAP >> $SCREEN	# line 8
    echo $HELP_DEPEND_MAP >> $SCREEN	# line 9
    echo $PRODUCTS >> $SCREEN		# line 10
    echo $ans >> $SCREEN		# line 11
#--------------------------------------------------------------------------
		cat $SCREEN | awk '
#--------------------------------------------------------------------------
    BEGIN { squote = sprintf ("%c", 39)   # set single quote
            dquote = sprintf ("%c", 34)   # set double quote
	  }
    NR == 1 { iscreen = $1; next}
    NR == 2 { nscreens = $1; next}
    NR == 3 { for (i = 1; i <= nscreens; i = i + 1) range[i] = $i
	      if ( iscreen == 1)
		  low = 1
	      else
		  low = range[iscreen-1] + 1
	      high = range[iscreen]
	      next
	    }
    NR == 4 { narches = $1; next }
    NR == 5 { nhelp = $1; next }
    NR == 6 { nproducts = $1; next }
    NR == 7 { for (i = 1; i <= NF; i = i + 1) default[i] = $i; next }
    NR == 8 { for (i = 1; i <= NF; i = i + 1) depend_arch[i] = $i; next }
    NR == 9 { for (i = 1; i <= NF; i = i + 1) depend_help[i] = $i; next }
    NR == 10 { for (i = 1; i <= NF; i = i + 1) product[i] = $i
               for (i = 1; i <= NF; i = i + 1) orig[i] = product[i]; next }
    NR == 11 { for (i = 1; i <= NF; i = i + 1) ans[i] = $i
              nans = NF; answer = $0;  next }
    END {
	  nstart_products = narches + nhelp + 2
#
# Rules: 1. Determine the following before processing each token.
#	    Arch set.
#	    Product set.
#	    Help set.
#	 2. General rules:
#	    product depends on: no arch, no help
#			 select if product on
#			 no dependency on arch or help
#	    product depends on: arch, no help
#			 select if product on and arch on
#			 no dependency on help
#	    product depends on: no arch, help
#			 select if product on or help on
#			 no dependency on arch
#	    product depends on: arch, help
#			 select if product on and arch on 
#			 select if product off and help on
#	 3. +number - check rules
#
    print "#!/bin/sh"
    print "#"
#
	  founderr = 0
 	  for (ni = 1; ni <= nans; ni = ni + 1) { 
#------------------------------------------------------------------------------
#
# Determine if arch, product, and help are set
#
              archset = 0
	      for (i = 1; i <= narches; i = i + 1)
	          if (product[i] != 0) { 
		      archset = 1
		      break
		  }
	      productset = 0
	      if (product[narches+1] != 0) productset = 1
	      helpset = 0
	      for (i = 1; i <= nhelp; i = i + 1)
	          if (product[narches+1+i] != 0) { 
		      helpset = 1
		      break
		  }
#------------------------------------------------------------------------------
	      tok = ans[ni]
#print tok
	      if ( tok ~ /^[+]?[0-9]+$/ ) {	   # + number
#******************************************************************************
#print 1
		  if (substr(tok,1,1) == "+")
		      num = substr(tok,2) + 0
		  else
		      num = tok
		  if (num >= low && num <= high) {
#
# If a real product number do further checking
#
		      if (num >= nstart_products) {
			  if (productset == 0 && helpset == 0) {
			      mesg="Either product or help.* must be set for this selection"
		   	      founderr = 1
			      break
			  }
		          else if (productset != 0 && depend_arch[num] != 0 && archset == 0) {
			      mesg="An arch.* must be set for this selection"
		   	      founderr = 1
			      break
		          }
			  else
			      product[num] = num
		      }
		      else
			  product[num] = num
		  }
		  else {
		      mesg="out of range"
		      founderr = 1
		      break
		  }
	      }
	      else if ( tok ~ /^-[0-9]+$/ ) {	   # - number
#******************************************************************************
#print 2
		  if (substr(tok,1,1) == "-")
		      num = substr(tok,2) + 0
		  else
		      num = tok
		  if (num >= low && num <= high) {
		      if (num < nstart_products) {
		          for (i = 1; i <= nproducts; i = i + 1)
		              save[i] = product[i]
		          save[num] = 0
#
# Determine the new value of archset, productset, and helpset
#
                          archset = 0
		          for (i = 1; i <= narches; i = i + 1)
			      if (save[i] != 0) { 
			          archset = 1
			          break
			      }
	      	          productset = 0
	      	          if (save[narches+1] != 0) productset = 1
	      		  helpset = 0
	      		  for (i = 1; i <= nhelp; i = i + 1)
	          	      if (save[narches+1+i] != 0) { 
		      	          helpset = 1
		      	          break
			      }
#
		          for (i = nstart_products; i <= nproducts; i = i + 1) {
			      if (save[i] == 0) continue
			      if (depend_arch[i] != 0 && archset == 0 && productset != 0) {
			          mesg="An arch.* still must be set for this deselection"
		                  founderr = 1
		                  break
			      }
			      else if ((depend_arch[i] != 0 || depend_help[i] != 0) && archset == 0 && productset == 0 && helpset == 0) {
			          mesg="Must have at least one of arch.*, product, or help.* set"
		                  founderr = 1
		                  break
			      }
			      else if ((depend_arch[i] != 0 || depend_help[i] != 0) && archset != 0 && productset == 0 && helpset == 0) {
			          mesg="Must have at either product, or help.* set"
		                  founderr = 1
		                  break
			      }
			  }
			  if (founderr == 0)
			      product[num] = 0
		      }
		      else
		          product[num] = 0
		  }
		  else {
		      mesg="out of range"
		      founderr = 1
		      break
		  }
	      }
	      else if ( tok ~ /^[+]?[0-9]+-[0-9]+$/ ) {  # + range
#******************************************************************************
#print 3
		  if (substr(tok,1,1) == "+")
		      num = substr(tok,2)
		  else
		      num = tok
		  nf = split(num,bounds,"-")
		  l = bounds[1] + 0 ; u = bounds[2] + 0
		  if (l > u) {
		      save = u
		      u = l
		      l = save
		  }
		  if (l >= low && l<= high && u >= low && u <= high) {
		      for (i = 1; i <= nproducts; i = i + 1)
		          save[i] = product[i]
		      for (i = l; i <= u; i = i + 1)
			  save[i] = i
#
# Determine the new value of archset, productset, and helpset
#
                      archset = 0
		      for (i = 1; i <= narches; i = i + 1)
			  if (save[i] != 0) { 
			      archset = 1
			      break
			  }
	      	      productset = 0
	      	      if (save[narches+1] != 0) productset = 1
	      	      helpset = 0
	      	      for (i = 1; i <= nhelp; i = i + 1)
	          	  if (save[narches+1+i] != 0) { 
		      	      helpset = 1
		      	      break
			  }
#
		      for (i = nstart_products; i <= nproducts; i = i + 1)
		          if (productset != 0 && depend_arch[num] != 0 && archset == 0) {
			      mesg="An arch.* must be set for this selection"
		   	      founderr = 1
			      break
			  }
		      if (founderr == 0)
		          for (i = 1; i <= nproducts; i = i + 1)
		              product[i] = save[i]
		  }
		  else {
		      mesg="Bad range"
		      founderr = 1
		      break
		  }
	      }
	      else if ( tok ~ /^-[0-9]+-[0-9]+$/ ) { # - range
#******************************************************************************
#print 4
		  if (substr(tok,1,1) == "-")
		      num = substr(tok,2)
		  else
		      num = tok
		  nf = split(num,bounds,"-")
		  l = bounds[1] + 0; u = bounds[2] + 0
		  if (l > u) {
		      save = u
		      u = l
		      l = save
		  }
		  if (l >= low && l<= high && u >= low && u <= high) {
		      for (i = 1; i <= nproducts; i = i + 1)
		          save[i] = product[i]
		      for (i = l; i <= u; i = i + 1)
			  save[i] = 0
#
# Determine the new value of archset, productset, and helpset
#
                      archset = 0
		      for (i = 1; i <= narches; i = i + 1)
			  if (save[i] != 0) { 
			      archset = 1
			      break
			  }
	      	      productset = 0
	      	      if (save[narches+1] != 0) productset = 1
	      	      helpset = 0
	      	      for (i = 1; i <= nhelp; i = i + 1)
	          	  if (save[narches+1+i] != 0) { 
		      	      helpset = 1
		      	      break
			  }

		      for (i = nstart_products; i <= nproducts; i = i + 1) {
			  if (save[i] == 0) continue
			  if (depend_arch[i] != 0 && archset == 0 && productset != 0) {
			      mesg="An arch.* still must be set for this deselection"
		              founderr = 1
		              break
			  }
			  else if ((depend_arch[i] != 0 || depend_help[i] != 0) && archset == 0 && productset == 0 && helpset == 0) {
			      mesg="Must have at least one of arch.*, product, or help.* set"
		              founderr = 1
		              break
			  }
			  else if ((depend_arch[i] != 0 || depend_help[i] != 0) && archset != 0 && productset == 0 && helpset == 0) {
			      mesg="Must have at either product, or help.* set"
		              founderr = 1
		              break
			  }
		      }
		      if (founderr == 0)
		          for (i = 1; i <= nproducts; i = i + 1)
		              product[i] = save[i]
		  }
		  else {
		      mesg="Bad range"
		      founderr = 1
		      break
		  }
	      }
	      else if ( tok ~ /^\+$/ ) {	# + deselect all
#******************************************************************************
#print 5
		  for (i = low; i <= high; i = i + 1)
		      product[i] = i
	      }
	      else if ( tok ~ /^\-$/ ) {	# - deselect all
#******************************************************************************
#print 6
		  for (i = low; i <= high; i = i + 1)
		      product[i] = 0
	      }
	      else if ( tok ~ /^\l$/ ) {	# l (license defaults)
#******************************************************************************
#print 7
		  for (i = low; i <= high; i = i + 1)
		      product[i] = default[i]
	      }
	      else {				# - invalid selector
#******************************************************************************
#print 8
		   mesg = "invalid selector"
		   founderr = 1
		   break
	      }
	  }
#
	  final = ""
	  for (i = 1; i <= nproducts; i = i + 1 )
	      final = final " " product[i]
    print "    PRODUCTS=" squote substr(final,2) squote
#
	  if ( founderr != 0 ) {	# error
#
#   print "    echo " squote " " squote
    print "    echo " squote "Error: (" tok ") - " mesg squote  
    print "    echo " squote " " squote
    print "    if [ " dquote "$batch" dquote " = " dquote "0" dquote " ]; then"
    print "        mesg=" squote "Please press <return> to continue: " squote
    print "        . $dir/$echon_sh"
    print "        read ans"
    print "    else"
    print "        . $dir/$cleanup_sh"
    print "        exit 1"
    print "    fi"
	  }
	}' > ${SCREEN}x
#--------------------------------------------------------------------------
	        chmod +x ${SCREEN}x
	        . ${SCREEN}x
	    done
	    ns=`expr $ns + 1`
	done
#
#//////////////////////////////////////////////////////////////////////////
#                        Output Product summary
#//////////////////////////////////////////////////////////////////////////
#
# Output what was selected
# 
	. $dir/$clearsc_sh
#
#--------------------------------------------------------------------------
echo "                 Select MATLAB Products [Summary]"
echo '----------------------------------------------------------------------------'
#
# Input file:
#
#   line:  $PRODUCTS
#   line:  $ARCH
#   line:  $ARCH_LIST_ALL
#   line:  $DECRYPT_PRODUCTS
#   file:  screen.map
#   line:  EOF          (separator)
#   file:  piece.map
#
# Output: standard output
#
#   Product summary
#
#--------------------------------------------------------------------------
	echo $PRODUCTS > $temp_file2
        echo $ARCH >> $temp_file2
        echo $ARCH_LIST_ALL >> $temp_file2
	echo $DECRYPT_PRODUCTS >> $temp_file2
#
	echo 'EOF' > ${SCREEN}x
	cat $MAPS_DIR/piece.map >> ${SCREEN}x
	cat > $temp_file3 << 'EOF'
#--------------------------------------------------------------------------
    BEGIN { state = 0
	    npiece = 0
            na = 0
	    nh = 0
	    np = 0; npmatlab = 0
            nplmarch = 0; ilocalarch = 0 }
    NR == 1 { for (i = 1; i <= NF; i = i + 1) product[i] = $i; next }
    NR == 2 { localarch = $1; lmarch = "lm." localarch; next }
    NR == 3 { for (i = 1; i <= NF; i = i + 1) archlist[i] = $i
	      narchlist = NF
	      next
            }
#
# decryptprod can be empty
#
    NR == 4 { for (i = 1; i <= NF; i = i + 1) decryptprod[$i] = 1; next }
    NF == 0 { next }
    $1 ~ /^#.*/ { next }
    $1 == "EOF"     { state = 1; next }
#**************************************************************************
# Cases for piece.map
#
		    { if (state == 1) {
			 npiece = npiece + 1
			 piecenum[npiece] = $1
		         piece[$1] = 1
			 piecename[$2] = 1
			 next
		      }
		    }
#**************************************************************************
# Cases for screen.map
#
    $1 ~ /^arch\..*/ { na = na + 1; np = np + 1
		      arch[na] = substr($1,6) 	# arch.xxxx
		      next }
    $1 ~ /^product/  { np = np + 1; next }
    $1 ~ /^help\..*/ { nh = nh + 1; np = np + 1
		       l = length("help.")
		       helpname[nh] = substr($1,l+1)
		       next }
    $1 ~ /^matlab/   { np = np + 1; prod[np] = $1
		       npmatlab = np
		       i = index($0,$2)
		       piecelist[np] = substr($0,i)
		       next }
    $1 ~ /^lm\..*/   { np = np + 1; prod[np] = $1
		       i = index($0,$2)
		       piecelist[np] = substr($0,i)
		       if ($1 == lmarch) nplmarch = np
		       next }
#
# Rest of the products: they may or may not start with "tbx"
#
                     { np = np + 1; prod[np] = $1
		       i = index($0,$2)
		       piecelist[np] = substr($0,i)
		       next }
    END {
#
# determine arch cross reference
#
	  for (i = 1; i <= na; i = i + 1)
	      for (j = 1; j <= narchlist; j = j + 1)
	          if (archlist[j] == arch[i]) {
		      archref[i] = j
		      break
		  }
#**************************************************************************
# select:   product help
# arches:   alpha
# products: tbx.mmle3 tbx.symbolic
#	    -> tbx.symbolic(alpha) - not available
# help:     [english]  tbx.mmle3 . . .
#		       ...
#           [japanese] ...
#**************************************************************************
# select:
	  select="select:   "
	  if (product[na+1] != 0) select = select "product "
	  for (i = 1; i <= nh; i = i + 1)
	      if (product[na+1+i] != 0) {
		  select = select "help"
		  break
	      }
          printf "%-s\n", select
#
# local arch selected
#
	  for (i = 1; i <= na; i = i + 1)
	      if (arch[i] == localarch && product[i] != 0)
		 ilocalarch = i
#
#**************************************************************************
# arches: Maximum of 5 per line
#
          aperline = 5
#
	  ia = 0
	  for (i = 1; i <= na; i = i + 1)
	      if (product[i] != 0)
		  ia = ia + 1
	  rem = ia % aperline
          nalines = (ia - rem ) / aperline
          if (rem != 0 || ia == 0) nalines = nalines + 1
          afront1="arches:   "
	  afront="          "
#--------------------------------------------------------------------------
	  k=0
          for (i = 1; i <= nalines; i = i + 1) {
	      if (i == 1)
		  printf "%-s", afront1
	      else
                  printf "%-s", afront
	      if (i == nalines && rem != 0)
		  high = rem
	      else
		  high = aperline
	      if (ia == 0) high = 0
	      for (j = 1; j <= high; j = j + 1) {
		  k = k + 1
		  while (product[k] == 0)
		     k = k + 1
		  printf "%-s ", arch[k]
	      }
	      printf "\n"
	  }
#
          pfront1 = "products: "
          pfront  = "          "
	  printf "%-s", pfront1
#
# "product" must be selected
#
	  if (product[na+1] != 0) {
#--------------------------------------------------------------------------
#
# Check that each product has all the pieces for each arch. If any are
# missing then output a message about that product for that arch. Start
# counting all over again on the next line.
#
# products: tbx.mmle3 tbx.control tbx.symbolic
#	    -> tbx.symbolic(alpha) - not available	   (one line per arch)
#	    tbx.fdident . . .
#
# lm.$arch     is dropped if matlab is selected and arch.$arch is selected
#
#           -> lm.$arch - already in matlab, dropped
#
# Products: Maximum of 5 per line
#--------------------------------------------------------------------------
#
              pperline = 5
              newline = 1
	      k = na + 1 + nh
	      while (k < np) {
	          k = k + 1
#
# 1. "matlab" and "tbx.matlab" are both associated with decrypt string
#    "tbx.matlab"
# 2. Potentially add a * to any product except those that start with
#    "lm." if product cannot be decrypted
#
	          prodk = prod[k]
		  if (prodk == "matlab" && decryptprod["tbx.matlab"] != 1)
		      prodk = prodk "*"
	          else if (prodk != "matlab" && index(prodk,"lm.") != 1 && decryptprod[prodk] != 1)
		      prodk = prodk "*"
#
	          if (nplmarch != 0 && k == nplmarch) {
#
# Handle lm.$arch - it is always assumed to be after matlab and all
#		    toolboxes.
#
	              if (product[k] != 0) {
		          if (newline > pperline) {
		              newline = 1
		              printf "\n%-s", pfront
		          }
		          printf "%-s ", prodk
		          if (ilocalarch != 0 && product[npmatlab] != 0) {
			      printf "\n"
                  	      printf "%-s", pfront
			      printf "-> %-s - already in matlab, dropped", prod[k]
			      newline = pperline
		          }
		      }
		      else
		          continue
	          }
	          else if (product[k] == 0) 
		      continue
	          else { 
		      if (newline > pperline) {
		          newline = 1
		          printf "\n%-s", pfront
		      }
		      printf "%-s ", prodk
#
#----------------------------------------------------------------------------
# Syntax of screen line: (do not worry about *.h entries here)
#
#   name        num | num(.*) | num:arch,...,arch   | num(.*):arch,...,arch |
#                     num:~arch,...,~arch | num(.*):~arch,...,~arch |
#                     num(.a) | num(.a,.*) |  
#                     num(.h) | num(.h,.*) 
#----------------------------------------------------------------------------
#
		      nf = split(piecelist[k],pe)
# Fix a HP bug. Be sure everything is a string
		      for (jj = 1; jj <= nf; jj = jj + 1) pe[jj] = pe[jj] ""
#
		      if (index(piecelist[k],".a") != 0 || index(piecelist[k],":") != 0) {
#
# Handle any arch dependencies
#
		          for (i = 1; i <= na; i = i + 1) {
			      if (product[i] == 0) continue
			      bad = 0
			      for (j = 1; j <= nf; j = j + 1) {
				  if (index(pe[j],".a") != 0 && index(pe[j],".*") == 0) {
# num(.a) - only one a
				      ix = index(pe[j],".a")
				      if ((ix + 1) == length(pe[j]))
				          pej = substr(pe[j],1,ix) archref[i]
				      else
				          pej = substr(pe[j],1,ix) archref[i] substr(pe[j],ix+2)
				  }
				  else if (index(pe[j],".a") != 0) {
# num(.a,.*) - only one a and *			      
# Break into three parts - front star rear (OLD awk cannot handle regular expressions as strings)!
				      lstart = 1
				      front = ""
				      star  = ""
				      rear  = "" 
				      for (l = lstart; l <= length(pe[j]); l = l + 1) {
					  c = substr(pe[j],l,1)
				          if (c == "*") {
					      lstart = l
					      break
				          }
					  else if (c == "a")
					      front = front archref[i]
					  else
					      front = front c
				      }
				      star = "*"
				      lstart = l + 1
				      for (l = lstart; l <= length(pe[j]); l = l + 1) {
					  c = substr(pe[j],l,1)
					  if (c == "a")
					      rear = rear archref[i]
					  else
					      rear = rear c
				      }
				      status = 1
				      minlen = length(front) + 1 + length(rear)
				      for (l = 1; l <= npiece; l = l + 1) {
					  if (length(piecenum[l]) < minlen) continue
					  srear = length(piecenum[l]) - length(rear) + 1 
					  if (length(rear) == 0 && substr(piecenum[l],1,length(front)) == front) {
					      status = 0
					      break
					  }
				          else if (length(rear) != 0 && substr(piecenum[l],1,length(front)) == front && substr(piecenum[l],srear) == rear) {

					      status = 0
					      break
					  }
				      }
				      if (status)
					  bad = 1
				      break
				  }
#------------------------------------------------------------------------------
			          else if (index(pe[j],":") != 0) {
# num:arch,...,arch  | num(.*):arch,...,arch | num:~arch,...,~arch | num(.*):~arch,...,~arch |
# only one .*  
				      ix = index(pe[j],":")
				      pej = substr(pe[j],1,ix-1)
				      list2 = substr(pe[j],ix+1)
				      nf2 = split(list2,pe2,",")
				      if (index(pe[j],"*") == 0) {
# no .*
				          if (index(list2,"~") == 0) {
# positive list
				              foundarch = 0
				              for (m = 1; m <= nf2; m = m + 1)
				                  if (pe2[m] == arch[i]) {
					              foundarch = 1
					              break
				                  }
				              if (foundarch == 0) continue   # loop over piece list on line
				          }
				          else {
# negative list
				              foundarch = 1
				              for (m = 1; m <= nf2; m = m + 1)
				                  if (substr(pe2[m],2) == arch[i]) {
					              foundarch = 0
					              break
				                  }
				              if (foundarch == 0) continue   # loop over piece list on line
				          }
				      }
				      else {
# with .* - only one *
				          if (index(list2,"~") == 0) {
# positive list
				              foundarch = 0
				              for (m = 1; m <= nf2; m = m + 1)
				                  if (pe2[m] == arch[i]) {
					              foundarch = 1
					              break
				                  }
				              if (foundarch == 0) continue
				          }
				          else {
# negative list
				              foundarch = 1
				              for (m = 1; m <= nf2; m = m + 1)
				                  if (substr(pe2[m],2) == arch[i]) {
					              foundarch = 0
					              break
				                  }
				              if (foundarch == 0) continue
				          }
# Break into three parts - front star rear (OLD awk cannot handle regular expressions as strings)!
				          lstart = 1
				          front = ""
				          star  = ""
				          rear  = "" 
				          for (l = lstart; l <= length(pe[j]); l = l + 1) {
					      c = substr(pe[j],l,1)
				              if (c == "*") {
					          lstart = l
					          break
				              }
					      else if (c == "a")
					          front = front i
					      else
					          front = front c
				          }
				          star = "*"
				          lstart = l + 1
				          for (l = lstart; l <= length(pe[j]); l = l + 1) {
					      c = substr(pe[j],l,1)
					      if (c == "a")
					          rear = rear archref[i]
					      else
					          rear = rear c
				          }
# Loop through all pieces to try to match the *
				          status = 1
				          minlen = length(front) + 1 + length(rear)
				          for (l = 1; l <= npiece; l = l + 1) {
					      if (length(piecenum[l]) < minlen) continue
					      srear = length(piecenum[l]) - length(rear) + 1 
					      if (length(rear) == 0 && substr(piecenum[l],1,length(front)) == front) {
					          status = 0
					          break
					      }
				              else if (length(rear) != 0 && substr(piecenum[l],1,length(front)) == front && substr(piecenum[l],srear) == rear) {

					          status = 0
					          break
					      }
				          }
				          if (status) {
					      bad = 1
				              break
					  }
					  continue
				      }
			          }
#------------------------------------------------------------------------------
			          else if (index(pe[j],".h") != 0)
				      continue
			          else {
				      if (index(pe[j],".*") == 0 ) {
# no *
			                  pej = pe[j]
				      }
				      else {
# has a *
				          lstart = 1
				          front = ""
				          star  = ""
				          rear  = "" 
				          for (l = lstart; l <= length(pe[j]); l = l + 1) {
					      c = substr(pe[j],l,1)
				              if (c == "*") {
					          lstart = l
					          break
				              }
					      else
					          front = front c
				          }
				          star = "*"
				          lstart = l + 1
				          for (l = lstart; l <= length(pe[j]); l = l + 1) {
					      c = substr(pe[j],l,1)
					      rear = rear c
				          }
# Loop through all pieces to try to match the *
				          status = 1
				          minlen = length(front) + 1 + length(rear)
				          for (l = 1; l <= npiece; l = l + 1) {
					      if (length(piecenum[l]) < minlen) continue
					      srear = length(piecenum[l]) - length(rear) + 1 
					      if (length(rear) == 0 && substr(piecenum[l],1,length(front)) == front) {
					          status = 0
					          break
					      }
				              else if (length(rear) != 0 && substr(piecenum[l],1,length(front)) == front && substr(piecenum[l],srear) == rear) {

					          status = 0
					          break
					      }
				          }
				          if (status) {
					      bad = 1
				              break
				          }
				          continue
				      }
				  }
			          if (piece[pej] != 1) {
				      bad = 1
			              break
			          }
			      } 	# end loop of piece list on line
			      if (bad == 1) {
			          printf "\n"
                  	          printf "%-s", pfront
			          printf "-> %-s %-10s - not available", prod[k] , "(" arch[i] ")"
			          newline = pperline
			      }
		          } 		# end loop over arches
		      }
		      else {
# only num or num(.*) - only one *
		          if (newline > pperline) {
		              newline = 1
		              printf "\n%-s", pfront
		          }
		          bad = 0
		          for (j = 1; j <= nf; j = j + 1)
			      if (index(pe[j],".h") == 0) {
				  if (index(pe[j],".*") == 0 ) {
# no *
			              pej = pe[j]
			              if (piece[pej] != 1) {
			                  bad = 1
			                  break
			              }
				  }
				  else {
# has a *
				      lstart = 1
				      front = ""
				      star  = ""
				      rear  = "" 
				      for (l = lstart; l <= length(pe[j]); l = l + 1) {
					  c = substr(pe[j],l,1)
				          if (c == "*") {
					      lstart = l
					      break
				          }
					  else
					      front = front c
				      }
				      star = "*"
				      lstart = l + 1
				      for (l = lstart; l <= length(pe[j]); l = l + 1) {
					  c = substr(pe[j],l,1)
					  rear = rear c
				      }
# Loop through all pieces to try to match the *
				      status = 1
				      minlen = length(front) + 1 + length(rear)
				      for (l = 1; l <= npiece; l = l + 1) {
					  if (length(piecenum[l]) < minlen) continue
					  srear = length(piecenum[l]) - length(rear) + 1 
					  if (length(rear) == 0 && substr(piecenum[l],1,length(front)) == front) {
					      status = 0
					      break
					  }
				          else if (length(rear) != 0 && substr(piecenum[l],1,length(front)) == front && substr(piecenum[l],srear) == rear) {

					      status = 0
					      break
					  }
				      }
				      if (status) {
					  bad = 1
				          break
				      }
				      continue
			          }
		              }
		          if (bad == 1) {
			      printf "\n"
                  	      printf "%-s", pfront
			      printf "-> %-s - not available", prod[k]
			      newline = pperline
		          }
		      }
	          }
		  newline = newline + 1
              }
	      printf "\n"
	  }
	  else
	      printf "\n"
#
#**************************************************************************
# help:     [english]    tbx.mmle3 . . .
#		         ...
#           [japanese]   ...
# 123456780 123456789012
#**************************************************************************
          hfront  = "help:    "
          hfront1 = "         "
	  hfront2 = "                     "
#
# At least one "help" must be selected
#
	  helpset = 0
	  for (i = 1; i <= nh; i = i + 1)
	      if (product[na+1+i] != 0) { 
		  helpset = 1
		  break
	      }
	  if (helpset != 0) {
	      for (i = 1; i <= nh; i = i + 1) {
	          if (product[na+1+i] == 0) continue
	  	  printf "%-10s%-11s", hfront, "[" helpname[i] "]"
		  hfront = hfront1
#
# Products: Maximum of 4 per line
#
                  hperline = 4
                  newline = 1
	          k = na + 1 + nh
	          while (k < np) {
		      k = k + 1
#
#----------------------------------------------------------------------------
# Syntax of screen line: (only worry about *.h entries here)
#
#   name        num | num:arch,...,arch   | num(.*):arch,...,arch |
#                     num:~arch,...,~arch | num(.*):~arch,...,~arch |
#                     num(.a) | num(.a,.*) |  
#                     num(.h) | num(.h,.*) 
#----------------------------------------------------------------------------
#
	              if (product[k] == 0) continue
		      nf = split(piecelist[k],pe)
# Fix a HP bug. Be sure everything is a string
		      for (jj = 1; jj <= nf; jj = jj + 1) pe[jj] = pe[jj] ""
#
		      if (index(piecelist[k],".h") != 0) {
			  found = 0
			  for (j = 1; j <= nf; j = j + 1)
			      if (index(pe[j],".h") != 0 && index(pe[j],".*") == 0) {
# no .* - only one h
				  ix = index(pe[j],".h")
				  if ((ix + 1) == length(pe[j]))
				      pej = substr(pe[j],1,ix) i
				  else
				      pej = substr(pe[j],1,ix) i substr(pe[j],ix+2)
				  if (piece[pej] == 1) {
				      found = 1
				      break
				  }
			      else if (index(pe[j],".h") != 0) {
# with .* - only one h and *
# Break into three parts - front star rear (OLD awk cannot handle regular expressions as strings)!
				  lstart = 1
				  front = ""
				  star  = ""
				  rear  = "" 
				  for (l = lstart; l <= length(pe[j]); l = l + 1) {
				      c = substr(pe[j],l,1)
				      if (c == "*") {
					  lstart = l
					  break
				      }
				      else if (c == "h")
					  front = front i
				      else
					  front = front c
				  }
				  star = "*"
				  lstart = l + 1
				  for (l = lstart; l <= length(pe[j]); l = l + 1) {
				      c = substr(pe[j],l,1)
				      if (c == "h")
					  rear = rear i
				      else
					  rear = rear c
				  }
# Loop through all pieces to try to match the *
				  found = 0
				  minlen = length(front) + 1 + length(rear)
				  for (l = 1; l <= npiece; l = l + 1) {
				      if (length(piecenum[l]) < minlen) continue
				      srear = length(piecenum[l]) - length(rear) + 1 
				      if (length(rear) == 0 && substr(piecenum[l],1,length(front)) == front) {
					  found = 1
					  break
				      }
				      else if (length(rear) != 0 && substr(piecenum[l],1,length(front)) == front && substr(piecenum[l],srear) == rear) {

				          found = 1
					  break
				      }
				  }
			      }
			  }
			  if (found == 1) {
	                      prodk = prod[k]
		              if (prodk == "matlab" && decryptprod["tbx.matlab"] != 1)
		      		  prodk = prodk "*"
	          	      else if (prodk != "matlab" && decryptprod[prodk] != 1)
		      		  prodk = prodk "*"
		  	      if (newline > hperline) {
		      		  newline = 1
		          	  printf "\n%-s", hfront2
		      	      }
		      	      printf "%-s ", prodk
			      newline = newline + 1
			  }
		      }
	          }
	      printf "\n"
	      }
	  }
#--------------------------------------------------------------------------
	}
EOF
	cat $temp_file2 $MAPS_DIR/screen.map ${SCREEN}x | awk -f $temp_file3
	rm -f $temp_file3
#--------------------------------------------------------------------------
echo '----------------------------------------------------------------------------'
#--------------------------------------------------------------------------
#
#//////////////////////////////////////////////////////////////////////////
#                   Remove products with missing pieces
#//////////////////////////////////////////////////////////////////////////
#
# Remove any products from PRODUCTS list that don't have all their pieces
#
# Input file:
#
#   line:  $PRODUCTS
#   line:  $ARCH
#   line:  $ARCH_LIST_ALL
#   file:  screen.map
#   line:  EOF          (separator)
#   file:  piece.map
#
# Output: variable PRODUCTS
#
#   product list
#
	echo $PRODUCTS > $temp_file2
        echo $ARCH >> $temp_file2
        echo $ARCH_LIST_ALL >> $temp_file2
#
	PRODUCTS=`cat $temp_file2 $MAPS_DIR/screen.map ${SCREEN}x | awk '
#--------------------------------------------------------------------------
    BEGIN { state = 0
	    npiece = 0
            na = 0
	    nh = 0
	    np = 0; npmatlab = 0
            nplmarch = 0; ilocalarch = 0 }
    NR == 1 { for (i = 1; i <= NF; i = i + 1) product[i] = $i; next }
    NR == 2 { localarch = $1; lmarch = "lm." localarch; next }
    NR == 3 { for (i = 1; i <= NF; i = i + 1) archlist[i] = $i
              narchlist = NF
              next
            }
    NF == 0 { next }
    $1 ~ /^#.*/ { next }
    $1 == "EOF"     { state = 1; next }
#**************************************************************************
# Cases for piece.map
#
		    { if (state == 1) {
                         npiece = npiece + 1
                         piecenum[npiece] = $1
		         piece[$1] = 1
			 piecename[$2] = 1
			 next
		      }
		    }
#**************************************************************************
# Cases for screen.map
#
    $1 ~ /^arch\..*/ { na = na + 1; np = np + 1
		      arch[na] = substr($1,6) 	# arch.xxxx
		      next }
    $1 ~ /^product/  { np = np + 1; next }
    $1 ~ /^help\..*/ { nh = nh + 1; np = np + 1; next }
    $1 ~ /^matlab/   { np = np + 1; prod[np] = $1
		       npmatlab = np
		       i = index($0,$2)
		       piecelist[np] = substr($0,i)
		       next }
    $1 ~ /^lm\..*/   { np = np + 1; prod[np] = $1
		       i = index($0,$2)
		       piecelist[np] = substr($0,i)
		       if ($1 == lmarch) nplmarch = np
		       next }
#
# Rest of the products: they may or may not start with "tbx"
#
                     { np = np + 1; prod[np] = $1
		       i = index($0,$2)
		       piecelist[np] = substr($0,i)
		       next }
    END {
#
# determine arch cross reference
#
          for (i = 1; i <= na; i = i + 1)
              for (j = 1; j <= narchlist; j = j + 1)
                  if (archlist[j] == arch[i]) {
                      archref[i] = j
                      break
                  }
#
# local arch selected
#
	  for (i = 1; i <= na; i = i + 1)
	      if (arch[i] == localarch && product[i] != 0)
		 ilocalarch = i
#
# any help selected
#
	  helpset = 0
	  for (i = 1; i <= nh; i = i + 1)
	      if (product[na+1+i] != 0) { 
		  helpset = 1
		  break
	      }
#--------------------------------------------------------------------------
#
# Check that each product has all the pieces. This means
# 1. If product set then delete the product if there are any missing
#    arch pieces for all arches. No worry about whether there is help or
#    not.
# 2. If product is not set and help is set then delete the product if
#    all help pieces are missing.
# 3. Otherwise leave it unchanged.
#
# drop:
#     lm.$arch     if matlab is selected and arch.$arch is selected
#
#--------------------------------------------------------------------------
	  k = na + 1 + nh
	  while (k < np) {
	      k = k + 1
	      if (nplmarch != 0 && k == nplmarch) {
#
# Handle lm.$arch - it is always assumed to be after matlab and all
#		    toolboxes.
#
	          if (product[k] != 0) {
		      if (ilocalarch != 0 && product[npmatlab] != 0)
			  product[k] = 0
		  }
		  else
		     continue
	      }
	      else if (product[k] == 0) 
		  continue
	      else if (product[na+1] != 0) {
#
# "product" must be selected
#
#----------------------------------------------------------------------------
# Syntax of screen line: (do not worry about *.h entries here)
#
#   name        num | num(.*) | num:arch,...,arch   | num(.*):arch,...,arch |
#                     num:~arch,...,~arch | num(.*):~arch,...,~arch |
#                     num(.a) | num(.a,.*) |  
#                     num(.h) | num(.h,.*) 
#----------------------------------------------------------------------------
#
		  nf = split(piecelist[k],pe)
# Fix a HP bug. Be sure everything is a string
		  for (jj = 1; jj <= nf; jj = jj + 1) pe[jj] = pe[jj] ""
#
		  if (index(piecelist[k],".a") != 0 || index(piecelist[k],":") != 0) {
#
# Handle any arch dependencies
#
		      ngood = na
		      for (i = 1; i <= na; i = i + 1) {
			  if (product[i] == 0) {
			      ngood = ngood - 1
			      continue
			  }
			  for (j = 1; j <= nf; j = j + 1) {
			      if (index(pe[j],".a") != 0 && index(pe[j],".*") == 0) {
# num(.a) - only one a
				  ix = index(pe[j],".a")
				  if ((ix + 1) == length(pe[j]))
				      pej = substr(pe[j],1,ix) archref[i]
				  else
				      pej = substr(pe[j],1,ix) archref[i] substr(pe[j],ix+2)
			      }
			      else if (index(pe[j],".a") != 0) {
# num(.a,.*) - only one a and *			      
# Break into three parts - front star rear (OLD awk cannot handle regular expressions as strings)!
			          lstart = 1
				  front = ""
				  star  = ""
				  rear  = "" 
				  for (l = lstart; l <= length(pe[j]); l = l + 1) {
				      c = substr(pe[j],l,1)
				      if (c == "*") {
					  lstart = l
					  break
				      }
				      else if (c == "a")
					  front = front archref[i]
				      else
				  	  front = front c
				  }
				  star = "*"
				  lstart = l + 1
				  for (l = lstart; l <= length(pe[j]); l = l + 1) {
				      c = substr(pe[j],l,1)
				      if (c == "a")
					  rear = rear archref[i]
				      else
					  rear = rear c
				  }
				  status = 1
				  minlen = length(front) + 1 + length(rear)
				  for (l = 1; l <= npiece; l = l + 1) {
				      if (length(piecenum[l]) < minlen) continue
				      srear = length(piecenum[l]) - length(rear) + 1 
				      if (length(rear) == 0 && substr(piecenum[l],1,length(front)) == front) {
					  status = 0
					  break
				      }
				      else if (length(rear) != 0 && substr(piecenum[l],1,length(front)) == front && substr(piecenum[l],srear) == rear) {

				          status = 0
					  break
				      }
			          }
				  if (status)
				      ngood = ngood - 1
				  break
			      }
#------------------------------------------------------------------------------
			      else if (index(pe[j],":") != 0) {
# num:arch,...,arch  | num(.*):arch,...,arch | num:~arch,...,~arch | num(.*):~arch,...,~arch |
# only one .*  
				  ix = index(pe[j],":")
				  pej = substr(pe[j],1,ix-1)
				  list2 = substr(pe[j],ix+1)
				  nf2 = split(list2,pe2,",")
				  if (index(pe[j],"*") == 0) {
# no .*
				      if (index(list2,"~") == 0) {
# positive list
				          foundarch = 0
				          for (m = 1; m <= nf2; m = m + 1)
				              if (pe2[m] == arch[i]) {
					          foundarch = 1
					          break
				              }
				          if (foundarch == 0) continue   # loop over piece list on line
				      }
				      else {
# negative list
				          foundarch = 1
				          for (m = 1; m <= nf2; m = m + 1)
				              if (substr(pe2[m],2) == arch[i]) {
					          foundarch = 0
					          break
				              }
				          if (foundarch == 0) continue   # loop over piece list on line
				      }
				  }
				  else {
# with .* - only one *
				      if (index(list2,"~") == 0) {
# positive list
				          foundarch = 0
				          for (m = 1; m <= nf2; m = m + 1)
				              if (pe2[m] == arch[i]) {
					          foundarch = 1
					          break
				              }
				          if (foundarch == 0) continue
				      }
				      else {
# negative list
				          foundarch = 1
				          for (m = 1; m <= nf2; m = m + 1)
				              if (substr(pe2[m],2) == arch[i]) {
					          foundarch = 0
					          break
				              }
				          if (foundarch == 0) continue
				      }
# Break into three parts - front star rear (OLD awk cannot handle regular expressions as strings)!
				      lstart = 1
				      front = ""
				      star  = ""
				      rear  = "" 
				      for (l = lstart; l <= length(pe[j]); l = l + 1) {
					  c = substr(pe[j],l,1)
				          if (c == "*") {
					      lstart = l
					      break
				          }
					  else if (c == "a")
					      front = front archref[i]
					  else
					      front = front c
				      }
				      star = "*"
				      lstart = l + 1
				      for (l = lstart; l <= length(pe[j]); l = l + 1) {
					  c = substr(pe[j],l,1)
					  if (c == "a")
					      rear = rear archref[i]
					  else
					      rear = rear c
				      }
# Loop through all pieces to try to match the *
				      status = 1
				      minlen = length(front) + 1 + length(rear)
				      for (l = 1; l <= npiece; l = l + 1) {
					  if (length(piecenum[l]) < minlen) continue
					  srear = length(piecenum[l]) - length(rear) + 1 
					  if (length(rear) == 0 && substr(piecenum[l],1,length(front)) == front) {
					      status = 0
					      break
					  }
				          else if (length(rear) != 0 && substr(piecenum[l],1,length(front)) == front && substr(piecenum[l],srear) == rear) {

					      status = 0
					      break
					  }
				      }
				      if (status) {
					  ngood = ngood - 1
				          break
				      }
				      continue
				  }
			      }
#------------------------------------------------------------------------------
			      else if (index(pe[j],".h") != 0)
				  continue
			      else {
				  if (index(pe[j],".*") == 0 ) {
# no *
			              pej = pe[j]
				  }
				  else {
# has a *
				      lstart = 1
				      front = ""
				      star  = ""
				      rear  = "" 
				      for (l = lstart; l <= length(pe[j]); l = l + 1) {
					  c = substr(pe[j],l,1)
				          if (c == "*") {
					      lstart = l
					      break
				          }
					  else
					      front = front c
				      }
				      star = "*"
				      lstart = l + 1
				      for (l = lstart; l <= length(pe[j]); l = l + 1) {
					  c = substr(pe[j],l,1)
					  rear = rear c
				      }
# Loop through all pieces to try to match the *
				      status = 1
				      minlen = length(front) + 1 + length(rear)
				      for (l = 1; l <= npiece; l = l + 1) {
					  if (length(piecenum[l]) < minlen) continue
					  srear = length(piecenum[l]) - length(rear) + 1 
					  if (length(rear) == 0 && substr(piecenum[l],1,length(front)) == front) {
					      status = 0
					      break
					  }
				          else if (length(rear) != 0 && substr(piecenum[l],1,length(front)) == front && substr(piecenum[l],srear) == rear) {

					      status = 0
					      break
					  }
				      }
				      if (status) {
					  ngood = ngood - 1
				          break
				      }
				      continue
				  }
			      }
			      if (piece[pej] != 1) {
				  ngood = ngood - 1
			          break
			      }
			  } 	# end loop of piece list on line
		      } 	# end loop over arches
		      if (ngood == 0) product[k] = 0
		  }
		  else {
# only num or num(.*) - only one *
		      bad = 0
		      for (j = 1; j <= nf; j = j + 1)
			  if (index(pe[j],".h") == 0) {
			      if (index(pe[j],".*") == 0 ) {
# no *
			          pej = pe[j]
			          if (piece[pej] != 1) {
			              bad = 1
			              break
			          }
			      }
			      else {
# has a *
				  lstart = 1
				  front = ""
				  star  = ""
				  rear  = "" 
				  for (l = lstart; l <= length(pe[j]); l = l + 1) {
				      c = substr(pe[j],l,1)
				      if (c == "*") {
					  lstart = l
					  break
				      }
				      else
					  front = front c
				  }
				  star = "*"
				  lstart = l + 1
				  for (l = lstart; l <= length(pe[j]); l = l + 1) {
				      c = substr(pe[j],l,1)
				      rear = rear c
				  }
# Loop through all pieces to try to match the *
				  status = 1
				  minlen = length(front) + 1 + length(rear)
				  for (l = 1; l <= npiece; l = l + 1) {
				      if (length(piecenum[l]) < minlen) continue
				      srear = length(piecenum[l]) - length(rear) + 1 
				      if (length(rear) == 0 && substr(piecenum[l],1,length(front)) == front) {
					  status = 0
					  break
				      }
				      else if (length(rear) != 0 && substr(piecenum[l],1,length(front)) == front && substr(piecenum[l],srear) == rear) {

				          status = 0
					  break
				      }
				  }
				  if (status) {
				      bad = 1
				      break
				  }
				  continue
			      }
		          }
		      if (bad == 1) product[k] = 0
		  }
	      }
	      else if (helpset != 0) {
		  nf = split(piecelist[k],pe)
# Fix a HP bug. Be sure everything is a string
		  for (jj = 1; jj <= nf; jj = jj + 1) pe[jj] = pe[jj] ""
#
		  ngood = nh
		  for (i = 1; i <= nh; i = i + 1) {
		      if (product[na+1+i] == 0) {
			  ngood = ngood - 1
			  continue
		      }
		      found = 0
		      for (j = 1; j <= nf; j = j + 1)
			  if (index(pe[j],".h") != 0 && index(pe[j],".*") == 0) {
# no .* - only one h
			      ix = index(pe[j],".h")
			      if ((ix + 1) == length(pe[j]))
				  pej = substr(pe[j],1,ix) i
			      else
				  pej = substr(pe[j],1,ix) i substr(pe[j],ix+2)
			      if (piece[pej] == 1) {
				  found = 1
				  break
			      }
			  }
			  else if (index(pe[j],".h") != 0) {
# with .* - only one h and *
# Break into three parts - front star rear (OLD awk cannot handle regular expressions as strings)!
			      lstart = 1
			      front = ""
			      star  = ""
			      rear  = "" 
			      for (l = lstart; l <= length(pe[j]); l = l + 1) {
				  c = substr(pe[j],l,1)
				  if (c == "*") {
				      lstart = l
				      break
				  }
				  else if (c == "h")
				      front = front i
				  else
				      front = front c
			      }
			      star = "*"
			      lstart = l + 1
			      for (l = lstart; l <= length(pe[j]); l = l + 1) {
				  c = substr(pe[j],l,1)
				  if (c == "h")
				      rear = rear i
				  else
				      rear = rear c
			      }
# Loop through all pieces to try to match the *
			      found = 0
			      minlen = length(front) + 1 + length(rear)
			      for (l = 1; l <= npiece; l = l + 1) {
				  if (length(piecenum[l]) < minlen) continue
				  srear = length(piecenum[l]) - length(rear) + 1 
				  if (length(rear) == 0 && substr(piecenum[l],1,length(front)) == front) {
				      found = 1
				      break
				  }
				  else if (length(rear) != 0 && substr(piecenum[l],1,length(front)) == front && substr(piecenum[l],srear) == rear) {

				      found = 1
				      break
				  }
			      }
			  }
		      if (found == 0)
			  ngood = ngood - 1
		  }
		  if (ngood == 0) product[k] = 0
	      }
	      else
		   product[k] = 0
          }  	# while loop 
          products=""
	  for (i = 1; i <= np; i = i + 1) products = products " " product[i]
          print substr(products,2)
	}'`
#--------------------------------------------------------------------------
#
#//////////////////////////////////////////////////////////////////////////
#                   Determine number of products
#//////////////////////////////////////////////////////////////////////////
#
# Input file:
#
#   line:  $NARCHES
#   line:  $PRODUCTS
#
# Output: variable np
#
#   number of products
#
        echo $NARCHES > $temp_file2
	echo $PRODUCTS >> $temp_file2
#
	np=`cat $temp_file2 | awk '
#--------------------------------------------------------------------------
# Start on the first help (arch ... product help ...)
#                                            ^
#
    NR == 1 { nstart = $1 + 2; next }
    NR == 2 { np = 0
	      for (i = nstart; i <= NF; i = i + 1)
		 if ($i != 0) np = np + 1
	      print np }'`
#--------------------------------------------------------------------------
#
# Handle specially for no products
#
	if [ "$np" = "0" ]; then
#--------------------------------------------------------------------------
    echo ''
    echo '    No product files!'
    echo '' 
    echo '------------------------------------------------------------------------------'
    echo 'Return to the first screen:    press <return>'
    echo 'To quit:                       type q'
    echo '------------------------------------------------------------------------------'
#--------------------------------------------------------------------------
    	    mesg='> '
            . $dir/$echon_sh
    	    if [ "$batch" = "0" ]; then
	        read ans
	        if [ `expr "//$ans" : '//q'` -ne 0 ]; then
		    . $dir/$cleanup_sh
		    exit 1
		fi
	    else
		echo 'q'
		. $dir/$cleanup_sh
		exit 1
	    fi
	    continue
	fi
#--------------------------------------------------------------------------
    echo "'*' means product cannot be decrypted without an appropriate TMW_Archive"
    echo "    feature line in your 'license.dat' file."
    echo ''
#--------------------------------------------------------------------------
#
#//////////////////////////////////////////////////////////////////////////
#                   Determine which files to extract
#//////////////////////////////////////////////////////////////////////////
#
# Determine which files to extract. There always will be some to extract.
#
# Input file:
#
#   line:  $PRODUCTS 
#   line:  $ARCH_LIST_ALL
#   file:  screen.map
#   line:  EOF          (separator)
#   file:  piece.map
#
# Output file: $temp_file
#
#   list of piece numbers one per line
#   
#--------------------------------------------------------------------------
        echo $PRODUCTS > $temp_file2
        echo $ARCH_LIST_ALL >> $temp_file2
#
	cat $temp_file2 $MAPS_DIR/screen.map ${SCREEN}x | awk '
#--------------------------------------------------------------------------
    BEGIN { na = 0
	    np = 0
	    nh = 0
	    npe = 0
	    ipe = 0
	    state = 0 }
    NR == 1 { for (i = 1; i <= NF; i = i + 1) product[i] = $i; next}
    NR == 2 { for (i = 1; i <= NF; i = i + 1) archlist[i] = $i
              narchlist = NF
              next
            }
    NF == 0 { next }
    $1 ~ /^#.*/ { next }
    $1 == "EOF"     { state = 1; next }
		    { if (state == 1) {
			 npe = npe + 1
			 piecenum[npe] = $1
		         piece[$1] = 1
			 next
		      }
		    }
    $1 ~ /arch\..*/  { na = na + 1; np = np + 1
		       arch[na] = substr($1,6) 	# arch.xxxx
		       next }
    $1 ~ /^product/  { np = np + 1; next }
    $1 ~ /^help\..*/ { nh = nh + 1
		       np = np + 1
		       piecelist[np] = $2 ""
		       next }
                     { np = np + 1; prod[np] = $1
		       i = index($0,$2)
		       piecelist[np] = substr($0,i) ""
		       next }
    END {
#
# determine arch cross reference
#
          for (i = 1; i <= na; i = i + 1)
              for (j = 1; j <= narchlist; j = j + 1)
                  if (archlist[j] == arch[i]) {
                      archref[i] = j
                      break
                  }
#
# Put all the part numbers in the crossref[]
#
# check for any help selected and add any help desks
#
	  helpset = 0
	  for (i = 1; i <= nh; i = i + 1)
	      if (product[na+1+i] != 0) { 
		  helpset = 1
		  pej = piecelist[na+1+i]
		  if (piece[pej] == 1 && ipiece[pej] != 1) {
		      ipe = ipe + 1
		      ipiece[pej] = 1
		      crossref[ipe] = pej ""
		  }
	      }
#
	  k = na + 1 + nh
	  while (k < np) {
	      k = k + 1
	      if (product[k] == 0) 
		  continue
	      else if (product[na+1] != 0) {
#
# "product" must be selected
#
#----------------------------------------------------------------------------
# Syntax of screen line: (do not worry about *.h entries here)
#
#   name        num | num(.*) | num:arch,...,arch   | num(.*):arch,...,arch |
#                     num:~arch,...,~arch | num(.*):~arch,...,~arch |
#                     num(.a) | num(.a,.*) |  
#                     num(.h) | num(.h,.*) 
#----------------------------------------------------------------------------
#
		  nf = split(piecelist[k],pe)
# Fix a HP bug. Be sure everything is a string
		  for (jj = 1; jj <= nf; jj = jj + 1) pe[jj] = pe[jj] ""
#
		  for (j = 1; j <= nf; j = j + 1) {
		      if (index(pe[j],".a") != 0 && index(pe[j],".*") == 0) {
# num(.a) - only one a
		          for (i = 1; i <= na; i = i + 1) {
			      if (product[i] == 0) continue
			      ix = index(pe[j],".a")
			      if ((ix + 1) == length(pe[j]))
				  pej = substr(pe[j],1,ix) archref[i]
			      else
				  pej = substr(pe[j],1,ix) archref[i] substr(pe[j],ix+2)
			      if (piece[pej] == 1 && ipiece[pej] != 1) {
			          ipe = ipe + 1
			          ipiece[pej] = 1
			          crossref[ipe] = pej ""
			      }
			  }
                      }
		      else if (index(pe[j],".a") != 0) {
# num(.a,.*) - only one a and *			      
# Break into three parts - front star rear (OLD awk cannot handle regular expressions as strings)!
		          for (i = 1; i <= na; i = i + 1) {
			      if (product[i] == 0) continue
			      lstart = 1
			      front = ""
			      star  = ""
			      rear  = "" 
			      for (l = lstart; l <= length(pe[j]); l = l + 1) {
				  c = substr(pe[j],l,1)
				  if (c == "*") {
				      lstart = l
				      break
				  }
				  else if (c == "a")
				      front = front archref[i]
				  else
				      front = front c
			      }
			      star = "*"
			      lstart = l + 1
			      for (l = lstart; l <= length(pe[j]); l = l + 1) {
				  c = substr(pe[j],l,1)
				  if (c == "a")
				      rear = rear archref[i]
				  else
				      rear = rear c
			      }
			      minlen = length(front) + 1 + length(rear)
			      for (l = 1; l <= npe; l = l + 1) {
			          if (length(piecenum[l]) < minlen) continue
				  srear = length(piecenum[l]) - length(rear) + 1 
				  if (length(rear) == 0 && substr(piecenum[l],1,length(front)) == front) {
				      ipe = ipe + 1
				      ipiece[pej] = 1
				      crossref[ipe] = piecenum[l] ""
				  }
				  else if (length(rear) != 0 && substr(piecenum[l],1,length(front)) == front && substr(piecenum[l],srear) == rear) {
				      ipe = ipe + 1
				      ipiece[pej] = 1
				      crossref[ipe] = piecenum[l] ""
				  }
			      }
			  }
		      }
		      else if (index(pe[j],":") != 0) {
# num:arch,...,arch  | num(.*):arch,...,arch | num:~arch,...,~arch | num(.*):~arch,...,~arch |
# only one .*  
			  ix = index(pe[j],":")
			  pej = substr(pe[j],1,ix-1)
			  list2 = substr(pe[j],ix+1)
			  nf2 = split(list2,pe2,",")
			  if (index(pe[j],"*") == 0) {
# no .*
			      if (index(list2,"~") == 0) {
# positive list
		                  for (i = 1; i <= na; i = i + 1) {
			 	      if (product[i] == 0) continue
			              for (m = 1; m <= nf2; m = m + 1)
				      if (pe2[m] == arch[i] && piece[pej] == 1 && ipiece[pej] != 1) {
				  	  ipe = ipe + 1
				          ipiece[pej] = 1
				          crossref[ipe] = pej ""
					  break
				       }
			          }
			      }
			      else {
# negative list
		                  for (i = 1; i <= na; i = i + 1) {
			 	      if (product[i] == 0) continue
				      includearch = 1
			              for (m = 1; m <= nf2; m = m + 1)
				          if (substr(pe2[m],2) == arch[i]) {
					      includearch = 0
					      break
				          }
				      if (includearch == 1 && piece[pej] == 1 && ipiece[pej] != 1) {
				          ipe = ipe + 1
				          ipiece[pej] = 1
				          crossref[ipe] = pej ""
				      }
			          }
			      }
			  }
			  else {
# with .* - only one *
# Break into three parts - front star rear (OLD awk cannot handle regular expressions as strings)!
			      lstart = 1
			      front = ""
			      star  = ""
			      rear  = "" 
			      for (l = lstart; l <= length(pe[j]); l = l + 1) {
				  c = substr(pe[j],l,1)
				  if (c == "*") {
				      lstart = l
				      break
				  }
				  else if (c == "a")
				      front = front archref[i]
				  else
				      front = front c
			      }
			      star = "*"
			      lstart = l + 1
			      for (l = lstart; l <= length(pe[j]); l = l + 1) {
				  c = substr(pe[j],l,1)
				  if (c == "a")
				      rear = rear archref[i]
				  else
				      rear = rear c
			      }
# Loop through all pieces to try to match the *
			      status = 1
			      minlen = length(front) + 1 + length(rear)
			      for (l = 1; l <= npe; l = l + 1) {
				  if (length(piecenum[l]) < minlen) continue
				  srear = length(piecenum[l]) - length(rear) + 1 
				  if (length(rear) == 0 && substr(piecenum[l],1,length(front)) == front) {
			      	      if (index(list2,"~") == 0) {
# positive list
		                          for (i = 1; i <= na; i = i + 1) {
			 	      	      if (product[i] == 0) continue
			                      for (m = 1; m <= nf2; m = m + 1)
				                  if (pe2[m] == arch[i]) {
				  	              ipe = ipe + 1
				                      ipiece[pej] = 1
				                      crossref[ipe] = piecenum[l] ""
					              break
				              }
			                  }
			              }
			              else {
# negative list
		                          for (i = 1; i <= na; i = i + 1) {
			 	      	      if (product[i] == 0) continue
				              includearch = 1
			                      for (m = 1; m <= nf2; m = m + 1)
				                  if (substr(pe2[m],2) == arch[i]) {
					              includearch = 0
					              break
				                  }
				              if (includearch == 1) {
				                  ipe = ipe + 1
				                  ipiece[pej] = 1
				                  crossref[ipe] = piecenum[l] ""
				              }
			                  }
			              }
				  }
				  else if (length(rear) != 0 && substr(piecenum[l],1,length(front)) == front && substr(piecenum[l],srear) == rear) {
			      	      if (index(list2,"~") == 0) {
# positive list
		                          for (i = 1; i <= na; i = i + 1) {
			 	      	      if (product[i] == 0) continue
			                      for (m = 1; m <= nf2; m = m + 1)
				                  if (pe2[m] == arch[i]) {
				  	              ipe = ipe + 1
				                      ipiece[pej] = 1
				                      crossref[ipe] = piecenum[l] ""
					              break
				              }
			                  }
			              }
			              else {
# negative list
		                          for (i = 1; i <= na; i = i + 1) {
			 	      	      if (product[i] == 0) continue
				              includearch = 1
			                      for (m = 1; m <= nf2; m = m + 1)
				                  if (substr(pe2[m],2) == arch[i]) {
					              includearch = 0
					              break
				                  }
				              if (includearch == 1) {
				                  ipe = ipe + 1
				                  ipiece[pej] = 1
				                  crossref[ipe] = piecenum[l] ""
				              }
			                  }
			              }
				  }
			      }
			  }
		      }
		      else if (index(pe[j],".h") != 0 && index(pe[j],".*") == 0 && helpset != 0) {
# no .* - only one h
		          for (i = 1; i <= nh; i = i + 1) {
			      if (product[na+1+i] == 0) continue
			      ix = index(pe[j],".h")
			      if ((ix + 1) == length(pe[j]))
				  pej = substr(pe[j],1,ix) i
			      else
				  pej = substr(pe[j],1,ix) i substr(pe[j],ix+2)
			      if (piece[pej] == 1 && ipiece[pej] != 1) {
				  ipe = ipe + 1
				  ipiece[pej] = 1
				  crossref[ipe] = pej ""
			      }
			  }
		      }
		      else if (index(pe[j],".h") != 0 && helpset != 0) {
		          for (i = 1; i <= nh; i = i + 1) {
			      if (product[na+1+i] == 0) continue
# with .* - only one h and *
# Break into three parts - front star rear (OLD awk cannot handle regular expressions as strings)!
			      lstart = 1
			      front = ""
			      star  = ""
			      rear  = "" 
			      for (l = lstart; l <= length(pe[j]); l = l + 1) {
			          c = substr(pe[j],l,1)
			          if (c == "*") {
				      lstart = l
				      break
			          }
			          else if (c == "h")
				      front = front i
			          else
				      front = front c
			      }
			      star = "*"
			      lstart = l + 1
			      for (l = lstart; l <= length(pe[j]); l = l + 1) {
			          c = substr(pe[j],l,1)
			          if (c == "h")
				      rear = rear i
			          else
				      rear = rear c
			      }
# Loop through all pieces to try to match the *
			      minlen = length(front) + 1 + length(rear)
			      for (l = 1; l <= npe; l = l + 1) {
			          if (length(piecenum[l]) < minlen) continue
			          srear = length(piecenum[l]) - length(rear) + 1 
			          if (length(rear) == 0 && substr(piecenum[l],1,length(front)) == front) {
				      ipe = ipe + 1
				      ipiece[pej] = 1
				      crossref[ipe] = piecenum[l] ""
			          }
			          else if (length(rear) != 0 && substr(piecenum[l],1,length(front)) == front && substr(piecenum[l],srear) == rear) {
				      ipe = ipe + 1
				      ipiece[pej] = 1
				      crossref[ipe] = piecenum[l] ""
			          }

			      }
			  }
		      }
		      else if (index(pe[j],".h") != 0 && helpset == 0)
		          continue
		      else {
			  pej = pe[j]
			  if (piece[pej] == 1 && ipiece[pej] != 1) {
			      ipe = ipe + 1
			      ipiece[pej] = 1
			      crossref[ipe] = pej ""
			  }
		      }
		  }
	      }
	      else if (helpset != 0) {
#
# help selected without product
#
		  nf = split(piecelist[k],pe)
# Fix a HP bug. Be sure everything is a string
		  for (jj = 1; jj <= nf; jj = jj + 1) pe[jj] = pe[jj] ""
#
		  for (j = 1; j <= nf; j = j + 1) {
		      if (index(pe[j],".h") != 0 && index(pe[j],".*") == 0) {
# no .* - only one h
		          for (i = 1; i <= nh; i = i + 1) {
			      if (product[na+1+i] == 0) continue
			      ix = index(pe[j],".h")
			      if ((ix + 1) == length(pe[j]))
				  pej = substr(pe[j],1,ix) i
			      else
				  pej = substr(pe[j],1,ix) i substr(pe[j],ix+2)
			      if (piece[pej] == 1 && ipiece[pej] != 1) {
				  ipe = ipe + 1
				  ipiece[pej] = 1
				  crossref[ipe] = pej ""
			      }
			  }
		      }
		      else if (index(pe[j],".h") != 0 && index(pe[j],".*") != 0) {
		          for (i = 1; i <= nh; i = i + 1) {
			      if (product[na+1+i] == 0) continue
# with .* - only one h and *
# Break into three parts - front star rear (OLD awk cannot handle regular expressions as strings)!
			      lstart = 1
			      front = ""
			      star  = ""
			      rear  = "" 
			      for (l = lstart; l <= length(pe[j]); l = l + 1) {
			          c = substr(pe[j],l,1)
			          if (c == "*") {
				      lstart = l
				      break
			          }
			          else if (c == "h")
				      front = front i
			          else
				      front = front c
			      }
			      star = "*"
			      lstart = l + 1
			      for (l = lstart; l <= length(pe[j]); l = l + 1) {
			          c = substr(pe[j],l,1)
			          if (c == "h")
				      rear = rear i
			          else
				      rear = rear c
			      }
# Loop through all pieces to try to match the *
			      minlen = length(front) + 1 + length(rear)
			      for (l = 1; l <= npe; l = l + 1) {
			          if (length(piecenum[l]) < minlen) continue
			          srear = length(piecenum[l]) - length(rear) + 1 
			          if (length(rear) == 0 && substr(piecenum[l],1,length(front)) == front) {
				      ipe = ipe + 1
				      ipiece[pej] = 1
				      crossref[ipe] = piecenum[l] ""
			          }
			          else if (length(rear) != 0 && substr(piecenum[l],1,length(front)) == front && substr(piecenum[l],srear) == rear) {
				      ipe = ipe + 1
				      ipiece[pej] = 1
				      crossref[ipe] = piecenum[l] ""
			          }

			      }
			  }
		      }
		  }
	      }
          }
          for (i = 1; i <= ipe; i = i + 1) print crossref[i]
	}' | sort -n > $temp_file
#
	echo 'EOF' >> $temp_file
#--------------------------------------------------------------------------
#
#//////////////////////////////////////////////////////////////////////////
#           Output the amount of disk space used to standard output
#//////////////////////////////////////////////////////////////////////////
#
# diskspace:
#
#   FTP files:         14.039 megabytes  (required and recovered if deleted)
#   installed files:   28.774 megabytes* (less if installed over an old MATLAB 5)
#
# Input file:
#
#   file:  list of piece numbers one per line
#   line:  EOF          (separator)
#   file:  piece.map
#
        cat $temp_file $MAPS_DIR/piece.map | awk '
#--------------------------------------------------------------------------
    BEGIN { state = 0; npe = 0; ipe = 0 }
    $1 == "EOF" { state = 1; next }
    state == 0 { npe = npe + 1;
		 piece[npe] = $1
		 next 
	       }
    NF == 0 { next }
    $1 ~ /^#.*/ { next }
		{ ipe = ipe + 1
		  csize[$1] = $3
	          size[$1] = $5
		  if ($2 == "boot.ftp") bsize = $5
		}
    END { tcsize = 0; tsize = 0
	  for (i = 1; i <= npe; i = i + 1) {
	      ipiece=piece[i]
	      if (csize[ipiece] != "" ) tcsize = tcsize + csize[ipiece]
	      if (size[ipiece] != "" ) tsize = tsize + size[ipiece]
	  }
	  tcsize = tcsize / (1024 * 1024)
	  tsize = tsize / (1024 * 1024)
	  bsize = bsize / (1024 * 1024)
#--------------------------------------------------------------------------
printf "diskspace:\n\n"
printf "   boot files:        %7.3f megabytes  (fixed overhead)\n", bsize
printf "   FTP files:         %7.3f megabytes  (recovered if deleted)\n", tcsize
printf "   installed files:   %7.3f megabytes* (less if updating an older version)\n", tsize
#--------------------------------------------------------------------------
        }'
#--------------------------------------------------------------------------
echo ''
echo "'*' assumes all extracted toolboxes have been decrypted."
echo ''
#--------------------------------------------------------------------------
#
#//////////////////////////////////////////////////////////////////////////
#           		    Save the file list
#//////////////////////////////////////////////////////////////////////////
#
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
echo '----------------------------------------------------------------------------'
echo 'Path to save file list?'
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	while true
	do
	    mesg='([$MATLAB/files.ftp]) > '
            . $dir/$echon_sh
            read ans
    	    if [ "$batch" = "1" ]; then
		echo $ans
	    fi
	    failure=0
            if [ `expr "//$ans" : '//[ 	]*$'` -ne 0 ]; then
		file=$MATLAB/files.ftp
		if [ -f $file ]; then
                    oldname=files.ftp
                    dir_oldname=$MATLAB
                    . $dir/$oldname_sh
                    mv -f $file $dir_oldname/old/$oldname 2>/dev/null
		    if [ $? -ne 0 ]; then
		        failure=1
		    fi
		fi
	    else
		file=$ans
		cat /dev/null > $file 2>/dev/null
		if [ $? -ne 0 ]; then
		    failure=1
	        fi
	    fi
	    if [ "$failure" = "0" ]; then
                cat /dev/null > $file 2>/dev/null
		if [ $? -eq 0 ]; then
#--------------------------------------------------------------------------
# Input file:
#
#   file:  list of piece numbers one per line
#   line:  EOF          (separator)
#   file:  comment.map
#
#--------------------------------------------------------------------------
            	    cat $temp_file $MAPS_DIR/comment.map | awk '
#--------------------------------------------------------------------------
    BEGIN { state = 0; ipe = 0 }
    $1 == "EOF" { state = 1; next }
    state == 0 { ipe = ipe + 1;
		 p[$1] = 1
		 next 
	       }
	       { if ( p[$1] == 1 ) {
		     i = index($0,$2)
		     print substr($0,i)
	         }
	       }' | sort +0 -1 > $file
#--------------------------------------------------------------------------
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo ' '
    echo 'File list saved . . .'
    echo ' '
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
		    break
	        fi
	    fi
    	    if [ "$batch" = "0" ]; then
        	mesg='    Sorry! Cannot create file. Continue? ([y]/n) '
        	. $dir/$echon_sh
        	read ans
        	if [ `expr "//$ans" : '//[Nn].*'` -gt 0 ]; then
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo ' '
    echo 'File list not saved . . .'
    echo ' '
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
		    break
		fi
	    else
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo ''
    echo '    Sorry! Cannot create file. File list not saved . . .'
    echo ''
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
		break
	    fi
	done
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
echo '----------------------------------------------------------------------------'
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	break
    done
