#
# usage:        tpdselct.sh
#
# abstract:     This Bourne Shell script selects which products
#		are to be selected for the 'collective tape' install.
#
# note(s):      1. This routine must be called using a . (period)
#
#		2. External screen format
#
#-------------------------------------------------------------------------
#	           T2: Select MATLAB Products [screen 1 of 3]
#----------------------------------------------------------------------------
#    1      - arch.sun4             11     - tbx.simulink (1.0)
#    2      - arch.hp700            12     - tbx.control (1.0)        
#    3      - arch.sgi              13     - tbx.ident (1.0)          
#    4      - arch.ibm_rs           14     - tbx.robust (1.0)         
#    5      - arch.dec_risc         15     - tbx.optim (1.0)          
#    6      - arch.hp300            16     - tbx.splines (1.0)
#    7      - arch.sol2             17     - tbx.signal (1.0)
#    8      - arch.alpha            18     - tbx.mmle3 (1.0)          
#                                   19     - tbx.mutools (1.0)
#    9      - matlab (1.0)          20     - tbx.chem (1.0)          
#                                   21     - tbx.nnet (1.0)           
#    10     - tbx.matlab (1.0)      [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   , 11  | 1      - arch.sun4             11     - tbx.simulink (1.0)
# 2   , 12  | 2      - arch.hp700            12     - tbx.control (1.0)        
# 3   , 13  | 3      - arch.sgi              13     - tbx.ident (1.0)         
# 4   , 14  | 4      - arch.ibm_rs           14     - tbx.robust (1.0)         
# 5   , 15  | 5      - arch.dec_risc         15     - tbx.optim (1.0)          
# 6   , 16  | 6      - arch.hp300            16     - tbx.splines (1.0)        
# 7   , 17  | 7      - arch.sol2             17     - tbx.signal (1.0)
# 8   , 18  | 8      - arch.alpha            18     - tbx.mmle3 (1.0)
# 0   , 19  |                                19     - tbx.mutools (1.0)        
# 9   , 20  | 9      - matlab (1.0)          20     - tbx.chem (1.0)           
# 0   , 21  |                                21     - tbx.nnet (1.0)           
# 10  , 0   | 10     - tbx.matlab (1.0)     [next screen]               
#-------------------------------------------------------------------------
#		   |<- (18)                       !<- (49)
# 1234567890123456789012345678901234567890123456789
# 12345678901
#	     123456789012345678901234567890123456789012345678901234567890
#
#
#		The
#
#		4. Variables
#
#		   Supplied:
#
#                  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
#                  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
#
#		   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='1 0 0 0 0 0 0 0 9 10 0 0 0 0 0 0 17 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
# NPRODUCTS=45
# RANGE='21 42 45'
# ARCH_DEPEND_MAP='0 0 0 0 0 0 0 0 9 10 11 0 0 0 0 0 17 0 0 0 0 22 23 24 0 0 0 0 0 0 0 0 0 0 0 0 0 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
#
#-------------------------------------------------------------------------
#	           T2: Select MATLAB Products [Summary]
# ----------------------------------------------------------------------------
# arches:   
# products: tbx.mmle3 decrypt.alpha tbx.symbolic
#	    -> tbx.symbolic(alpha) - not on media
# ----------------------------------------------------------------------------
# '*' means product cannot be decrypted without an appropriate TMW_Archive
#     feature line in your 'license.dat' file.
#
# diskspace:    28.774 megabytes* (less if installed over an old MATLAB)
#
#    boot files:        3.250 megabytes  (required and recovered)
#    extracted files:  14.039 megabytes  (required and recovered) 
#    installed files:  28.774 megabytes* (less if installed over an old MATLAB)
#
# '*' assumes all extracted toolboxes have been decrypted.
#
# Ready to extract product files from tape? ([y]/n)
#
#    or if no products
#
# No product files! Please press <return> to continue: 
#
#-------------------------------------------------------------------------
#
# 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-1997 by The MathWorks, Inc.
# $Revision: 1.17 $  $Date: 1997/05/08 22:06:10 $
#----------------------------------------------------------------------------
#
#                               Product selection
#                               -----------------
#
    PRODUCTS=$DEFAULT_MAP
    while true
    do
#
# Loop for each screen
#
	ns=1
	while [ $ns -le $NSCREENS ]
	do
#
	    while true
	    do
#
# Output the screen
#
	    . $dir/$clearsc_sh
#--------------------------------------------------------------------------
echo "                 T2: Select MATLAB Products [screen $ns of $NSCREENS]"
echo '------------------------------------------------------------------------------'
#--------------------------------------------------------------------------
		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,48) "*" substr(line,50)
	      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 '------------------------------------------------------------------------------'
#--------------------------------------------------------------------------
		mesg="> "
    		. $dir/$echon_sh
		read ans
#		
                if [ "$ans" = "" ]; then
		    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
			mesg="> "
    		        . $dir/$echon_sh
		        read ans
#
                        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 ''
#-----------------------------------------------------------------------------
            		    mesg='Please press <return> to continue: '
            		    . $dir/$echon_sh
            		    read ans
		            continue
			fi
		    done
		    continue
		fi
#
# Attempt to apply changes and check for errors. Write data to file $SCREEN,
# and create file ${SCREEN}x which is then sourced. 
#
#--------------------------------------------------------------------------
    echo $ns > $SCREEN			# line 1
    echo $NSCREENS >> $SCREEN		# line 2
    echo $RANGE >> $SCREEN		# line 3
    echo $NARCHES >> $SCREEN		# line 4
    echo $NPRODUCTS >> $SCREEN		# line 5
    echo $DEFAULT_MAP >> $SCREEN	# line 6	
    echo $ARCH_DEPEND_MAP >> $SCREEN	# line 7
    echo $PRODUCTS >> $SCREEN		# line 8
    echo $ans >> $SCREEN		# line 9
#--------------------------------------------------------------------------
		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 { nproducts = $1; next }
    NR == 6 { for (i = 1; i <= NF; i = i + 1) default[i] = $i; next }
    NR == 7 { for (i = 1; i <= NF; i = i + 1) depend[i] = $i; next }
    NR == 8 { 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 == 9 { for (i = 1; i <= NF; i = i + 1) ans[i] = $i
              nans = NF; answer = $0;  next }
    END {
#
    print "#!/bin/sh"
    print "#"
#
	  founderr = 0
 	  for (ni = 1; ni <= nans; ni = ni + 1) { 
#------------------------------------------------------------------------------
#
# Determine if arch is set
#
              archset = 0
	      for (i = 1; i <= narches; i = i + 1)
	          if (product[i] != 0) { 
		      archset = 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 (depend[num] != 0 && archset == 0) {
			  mesg="an arch.* must be set for this selection"
		   	  founderr = 1
			  break
		      }
		      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) {
#------------------------------------------------------------------------------ #
# Make change and see if arch dependency is ok
#
		      for (i = 1; i <= nproducts; i = i + 1)
		          save[i] = product[i]
		      save[num] = 0
                      archset = 0
		      for (i = 1; i <= narches; i = i + 1)
			  if (save[i] != 0) { 
			      archset = 1
			      break
			  }
		      archdepend = 0
		      for (i = narches + 1; i <= nproducts; i = i + 1)
			  if (save[i] != 0 && depend[i] != 0) {
			      archdepend = 1
			      break
			  }
#------------------------------------------------------------------------------
		      if (archset == 0 && archdepend != 0) {
			  mesg="an arch.* still must be set for this deselection"
		          founderr = 1
		          break
		      }
		      else
		          for (i = 1; i <= nproducts; i = i + 1)
		              product[i] = save[i]
		  }
		  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) {
#------------------------------------------------------------------------------ #
# Make change and see if arch dependency is ok
#
		      for (i = 1; i <= nproducts; i = i + 1)
		          save[i] = product[i]
		      for (i = l; i <= u; i = i + 1)
			  save[i] = i
                      archset = 0
		      for (i = 1; i <= narches; i = i + 1)
			  if (save[i] != 0) { 
			      archset = 1
			      break
			  }
		      archdepend = 0
		      for (i = narches + 1; i <= nproducts; i = i + 1)
			  if (save[i] != 0 && depend[i] != 0) {
			      archdepend = 1
			      break
			  }
#------------------------------------------------------------------------------
		      if (archset == 0 && archdepend != 0) {
			  mesg="an arch.* must still be set for this selection range"
		          founderr = 1
		          break
		      }
		      else
		          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) {
#------------------------------------------------------------------------------ #
# Make change and see if arch dependency is ok
#
		      for (i = 1; i <= nproducts; i = i + 1)
		          save[i] = product[i]
		      for (i = l; i <= u; i = i + 1)
			  save[i] = 0
                      archset = 0
		      for (i = 1; i <= narches; i = i + 1)
			  if (save[i] != 0) { 
			      archset = 1
			      break
			  }
		      archdepend = 0
		      for (i = narches + 1; i <= nproducts; i = i + 1) 
			  if (save[i] != 0 && depend[i] != 0) {
			      archdepend = 1
			      break
			  }
#------------------------------------------------------------------------------
		      if (archset == 0 && archdepend != 0) {
			  mesg="an arch.* must still be set for this deselection range"
		          founderr = 1
		          break
		      }
		      else
		          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 "    mesg=" squote "Please press <return> to continue: " squote
    print "    . $dir/$echon_sh"
    print "    read ans"
	  }
	}' > ${SCREEN}x
#--------------------------------------------------------------------------
	        chmod +x ${SCREEN}x
	        . ${SCREEN}x
	    done
	    ns=`expr $ns + 1`
	done
#
# Output what was selected
# 
	. $dir/$clearsc_sh
#
#--------------------------------------------------------------------------
echo "                 T2: Select MATLAB Products [Summary]"
echo '----------------------------------------------------------------------------'
#--------------------------------------------------------------------------
	echo 'EOF' > ${SCREEN}x
	cat $MAPS_DIR/piece.map >> ${SCREEN}x
	echo $NPRODUCTS $PRODUCTS $ARCH $DECRYPT_PRODUCTS | cat - $MAPS_DIR/screen.map ${SCREEN}x | awk '
#--------------------------------------------------------------------------
    BEGIN { npx = 0; na = 0; np = 0; npe = 0; state = 0; npmatlab = 0
            nplmarch = 0; npdecryptarch = 0; ilocalarch = 0; nxtbx = 0 }
    NR == 1 { npx = $1; for (i = 2; i <= npx + 1; i = i + 1) product[i-1] = $i
              localarch = $(npx + 2); decryptarch = "decrypt." localarch;
              lmarch = "lm." localarch;
	      for (i = npx + 3; i <= NF; i = i + 1) decryptprod[$i] = 1; next}
    NF == 0 { next }
    $1 ~ /^#.*/ { next }
    $1 == "EOF"     { state = 1; next }
		    { if (state == 1) {
			 npe = npe + 1
		         piece[$1] = 1
			 piecename[$2] = 1
			 next
		      }
		    }
    $1 ~ /^arch\..*/ { na = na + 1; np = np + 1
		      arch[na] = substr($1,6) 	# arch.xxxx
		      archnum[na] = $2
		      next }
    $1 ~ /^matlab/ { np = np + 1; prod[np] = $1
		      npmatlab = np
		      i = index($0,$2)
		      piecelist[np] = substr($0,i)
		      next }
    $1 ~ /^tbx\..*/ { np = np + 1; prod[np] = $1
		     i = index($0,$2)
		     piecelist[np] = substr($0,i)
		     if (product[np] != 0) nxtbx = nxtbx + 1
		     next }
    $1 ~ /^lm\..*/ { np = np + 1; prod[np] = $1
		    i = index($0,$2)
		    piecelist[np] = substr($0,i)
		    if ($1 == lmarch) nplmarch = np
		    next }
    $1 ~ /^decrypt\..*/ { np = np + 1; prod[np] = $1
		        i = index($0,$2)
		        piecelist[np] = substr($0,i)
		        if ($1 == decryptarch) npdecryptarch = np
		        next }
                     { np = np + 1; prod[np] = $1
		       i = index($0,$2)
		       piecelist[np] = substr($0,i)
		       next }
    END { nax = 0
	  for (i = 1; i <= na; i = i + 1)
	      if ( product[i] != 0 ) nax = nax + 1
#
# 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"
	  }
#--------------------------------------------------------------------------
#
# 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 on media	   (one line per arch)
#	    tbx.fdident . . .
#
# lm.$arch     is dropped if matlab is selected and arch.$arch is selected
# decrypt.$arch is dropped if matlab is selected and arch.$arch is selected
# decrypt.$arch is added if matlab not selected, at least one valid tbx is
#	       selected and decrypt.$arch not selected
#
#           -> lm.$arch - already in matlab, dropped
#           -> decrypt.$arch - already in matlab, dropped
#           -> decrypt.$arch - added to do decryption on this host
#
# Products: Maximum of 5 per line
#
          pperline = 5
          pfront1="products: "
          pfront="          "
#
#--------------------------------------------------------------------------
	  k = na
          newline = 1
	  printf "%-s", pfront1
	  while (k < np) {
	      k = k + 1
#
# If toolbox is not in decrypted list then add a star to the end
#
	      prodk = prod[k]
	      if (index(prod[k],"tbx.") == 1 && decryptprod[prodk] != 1)
		  prodk = prodk "*"
#
# Handle decrypt.$arch - it is always assumed to be after matlab and all
#		         toolboxes.
#
	      if (npdecryptarch != 0 && k == npdecryptarch) {
	          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 if (ilocalarch != 0) {
		      if (product[npmatlab] == 0 &&  nxtbx != 0) {
		          if (newline > pperline) {
		              newline = 1
		              printf "\n%-s", pfront
		          }
		          printf "%-s ", prodk
			  printf "\n"
                  	  printf "%-s", pfront
			  printf "    -> %-s - added to do decryption on this host", prod[k]
			  newline = pperline
		      }
		  }
		  else if (nxtbx != 0) {
		          if (newline > pperline) {
		              newline = 1
		              printf "\n%-s", pfront
		          }
		          printf "%-s ", prodk
			  printf "\n"
                  	  printf "%-s", pfront
			  printf "    -> %-s - added to do decryption on this host", prod[k]
			  newline = pperline
		  }
		  else
		     continue
	      }
	      else 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:
#
#   name	num | num:arch,...,arch | num:~arch,...,~arch | num.a
#----------------------------------------------------------------------------
#
		  nf = split(piecelist[k],pe)
		  if (index(piecelist[k],".a") != 0 || index(piecelist[k],":") != 0) {
#
# Handle any arch dependencies
#
		      nbad = 0
		      for (i = 1; i <= na; i = i + 1) {
			  if (product[i] == 0) continue
			  bad = 0
			  for (j = 1; j <= nf; j = j + 1) {
			      if (substr(pe[j], length(pe[j])-1) == ".a")
				  pej = substr(pe[j],1,length(pe[j])-1) archnum[i]
#------------------------------------------------------------------------------
			      else if (index(pe[j],":") != 0) {
				  ix = index(pe[j],":")
				  pej = substr(pe[j],1,ix-1)
				  list2 = substr(pe[j],ix+1)
				  nf2 = split(list2,pe2,",")
				  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
				  }
			      }
#------------------------------------------------------------------------------
			      else
				  pej = pe[j]
			      if (piece[pej] != 1) {
				  bad = 1
				  nbad = nbad + 1
			          break
			      }
			  }
			  if (bad == 1) {
			      printf "\n"
                  	      printf "%-s", pfront
			      printf "    -> %-s(%s) - not on media", prod[k], arch[i]
			      newline = pperline
			  }
		      }
		      if (nbad == nax && index(prod[k],"tbx.") == 1)
			  nxtbx = nxtbx - 1
		  }
		  else {
		      bad = 0
		      for (j = 1; j <= nf; j = j + 1) {
			  pej = pe[j]
			  if (piece[pej] != 1) {
			      bad = 1
			      break
			  }
		      }
		      if (bad == 1) {
			  printf "\n"
                  	  printf "%-s", pfront
			  printf "    -> %-s - not on media", prod[k]
			  newline = pperline
			  if (index(prod[k],"tbx.") == 1)
			     nxtbx = nxtbx - 1
		      }
		  }
		  newline = newline + 1
	      }
          }
	  printf "\n"
#--------------------------------------------------------------------------
	}'
#--------------------------------------------------------------------------
echo '----------------------------------------------------------------------------'
#--------------------------------------------------------------------------
#
# Remove any products that don't have any all the pieces from PRODUCTS list
# Add decrypt.$arch if one is required. 
#
	PRODUCTS=`echo $PRODUCTS $ARCH | cat - $MAPS_DIR/screen.map ${SCREEN}x | awk '
#--------------------------------------------------------------------------
    BEGIN { na = 0; np = 0; npe = 0; state = 0; npmatlab = 0
            nplmarch = 0; npdecryptarch = 0; ilocalarch = 0; nxtbx = 0 }
    NR == 1 { for (i = 1; i <= NF-1; i = i + 1) product[i] = $i
              localarch = $NF; decryptarch = "decrypt." localarch;
              lmarch = "lm." localarch;  next}
    NF == 0 { next }
    $1 ~ /^#.*/ { next }
    $1 == "EOF"     { state = 1; next }
		    { if (state == 1) {
			 npe = npe + 1
		         piece[$1] = 1
			 piecename[$2] = 1
			 next
		      }
		    }
    $1 ~ /^arch\..*/ { na = na + 1; np = np + 1
		      arch[na] = substr($1,6) 	# arch.xxxx
		      archnum[na] = $2
		      next }
    $1 ~ /^matlab/   { np = np + 1; prod[np] = $1
		      npmatlab = np
		      i = index($0,$2)
		      piecelist[np] = substr($0,i)
		      next }
    $1 ~ /^tbx\..*/ { np = np + 1; prod[np] = $1
		     i = index($0,$2)
		     piecelist[np] = substr($0,i)
		     if (product[np] != 0) nxtbx = nxtbx + 1
		     next }
    $1 ~ /^lm\..*/ { np = np + 1; prod[np] = $1
		    i = index($0,$2)
		    piecelist[np] = substr($0,i)
		    if ($1 == lmarch) nplmarch = np
		    next }
    $1 ~ /^decrypt\..*/ { np = np + 1; prod[np] = $1
		        i = index($0,$2)
		        piecelist[np] = substr($0,i)
		        if ($1 == decryptarch) npdecryptarch = np
		        next }
                     { np = np + 1; prod[np] = $1
		       i = index($0,$2)
		       piecelist[np] = substr($0,i)
		       next }
    END { nax = 0
	  for (i = 1; i <= na; i = i + 1)
	      if ( product[i] != 0 ) nax = nax + 1
#
# local arch selected
#
          
	  for (i = 1; i <= na; i = i + 1)
	      if (arch[i] == localarch && product[i] != 0)
		 ilocalarch = i
#--------------------------------------------------------------------------
#
# Check that each product has all the pieces. If it is arch dependent
# then delete the product if there are missing pieces for ALL arches
# that were selected. If it is not arch dependent then delete the product
# if any piece is missing.
#
# drop:
#     lm.$arch     if matlab is selected and arch.$arch is selected
#     decrypt.$arch if matlab is selected and arch.$arch is selected
#
# add:
#     decrypt.$arch if matlab not selected, at least one valid tbx is
#	           selected and decrypt.$arch not selected
#
#--------------------------------------------------------------------------
	  k = na
	  while (k < np) {
	      k = k + 1
#
# Handle decrypt.$arch - it is always assumed to be after matlab and all
#		        toolboxes.
#
	      if (npdecryptarch != 0 && k == npdecryptarch) {
	          if (product[k] != 0) {
		      if (ilocalarch != 0 && product[npmatlab] != 0)
			  product[k] = 0
		  }
		  else if (ilocalarch != 0) {
		      if (product[npmatlab] == 0 &&  nxtbx != 0)
			  product[k] = k
		  }
		  else if (nxtbx != 0)
		      product[k] = k
		  else
		     continue
	      }
	      else 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 { 
#
#----------------------------------------------------------------------------
# Syntax of screen line:
#
#   name	num | num:arch,...,arch | num:~arch,...,~arch | num.a
#----------------------------------------------------------------------------
#
		  nf = split(piecelist[k],pe)
		  if (index(piecelist[k],".a") != 0 || index(piecelist[k],":") != 0) {
#
# Handle any arch dependencies
#
		      nbad = 0
		      for (i = 1; i <= na; i = i + 1) {
			  if (product[i] == 0) continue
			  bad = 0
			  for (j = 1; j <= nf; j = j + 1) {
			      if (substr(pe[j], length(pe[j])-1) == ".a")
				  pej = substr(pe[j],1,length(pe[j])-1) archnum[i]
#------------------------------------------------------------------------------
			      else if (index(pe[j],":") != 0) {
				  ix = index(pe[j],":")
				  pej = substr(pe[j],1,ix-1)
				  list2 = substr(pe[j],ix+1)
				  nf2 = split(list2,pe2,",")
				  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
				  }
			      }
#------------------------------------------------------------------------------
			      else
				  pej = pe[j]
			      if (piece[pej] != 1) {
				  bad = 1
				  nbad = nbad + 1
			          break
			      }
			  }
		      }
		      if (nbad == nax && index(prod[k],"tbx.") == 1) {
			  nxtbx = nxtbx - 1
			  product[k] = 0
		      }
		  }
		  else {
		      bad = 0
		      for (j = 1; j <= nf; j = j + 1) {
			  pej = pe[j]
			  if (piece[pej] != 1) {
			      bad = 1
			      break
			  }
		      }
		      if (bad == 1 && index(prod[k],"tbx.") == 1) {
			  nxtbx = nxtbx - 1
			  product[k] = 0
		      }
		  }
	      }
          }
          products=""
	  for (i = 1; i <= np; i = i + 1) products = products " " product[i]
          print substr(products,2)
	}'`
#--------------------------------------------------------------------------
#
# Handle specially for no products
#
	np=`echo $NARCHES $PRODUCTS | awk '
#--------------------------------------------------------------------------
    NR == 1 { np = 0
	      for (i = $1 + 2; i <= NF; i = i + 1)
		 if ($i != 0) np = np + 1
	      print np }'`
#--------------------------------------------------------------------------
	if [ "$np" = "0" ]; then
#--------------------------------------------------------------------------
    echo ''
#--------------------------------------------------------------------------
	    mesg='No product files! Please press <return> to continue: '
            . $dir/$echon_sh
	    read ans
	    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. There always will be some to extract.
#
#--------------------------------------------------------------------------
	PIECES=`echo $PRODUCTS | cat - $MAPS_DIR/screen.map ${SCREEN}x | awk '
#--------------------------------------------------------------------------
    BEGIN { na = 0; np = 0; npe = 0; ipe = 0; state = 0 }
    NR == 1 { for (i = 1; i <= NF; i = i + 1) product[i] = $i; next}
    NF == 0 { next }
    $1 ~ /^#.*/ { next }
    $1 == "EOF"     { state = 1; next }
		    { if (state == 1) {
			 npe = npe + 1
		         piece[$1] = 1
			 next
		      }
		    }
    $1 ~ /arch\..*/ { na = na + 1; np = np + 1
		      arch[na] = substr($1,6) 	# arch.xxxx
		      archnum[na] = $2
		      next }
                    { np = np + 1; prod[np] = $1
		      i = index($0,$2)
		      piecelist[np] = substr($0,i) ""
		      next }
    END { k = na
	  while (k < np) {
	      k = k + 1
	      if (product[k] == 0) 
		  continue
	      else { 
#
#----------------------------------------------------------------------------
# Syntax of screen line:
#
#   name	num | num:arch,...,arch | num:~arch,...,~arch | num.a
#----------------------------------------------------------------------------
#
		  nf = split(piecelist[k],pe)
		  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
			  for (j = 1; j <= nf; j = j + 1) {
			      if (substr(pe[j], length(pe[j])-1) == ".a")
				 pej = substr(pe[j],1,length(pe[j])-1) archnum[i]
#------------------------------------------------------------------------------
			      else if (index(pe[j],":") != 0) {
				  ix = index(pe[j],":")
				  pej = substr(pe[j],1,ix-1)
				  list2 = substr(pe[j],ix+1)
				  nf2 = split(list2,pe2,",")
				  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
				  }
			      }
#------------------------------------------------------------------------------
			      else
				  pej = pe[j] ""
			      if (piece[pej] == 1 && ipiece[pej] != 1) {
				  ipe = ipe + 1
				  ipiece[pej] = 1
				  crossref[ipe] = pej ""
			      }
			  }
		      }
		  }
		  else
		      for (j = 1; j <= nf; j = j + 1) {
			  pej = pe[j] ""
			  if (piece[pej] == 1 && ipiece[pej] != 1) {
			      ipe = ipe + 1
			      ipiece[pej] = 1
			      crossref[ipe] = pej ""
			  }
		      }
	      }
          }
          for (i = 1; i <= ipe; i = i + 1) print crossref[i]
	}' | sort -n`
#--------------------------------------------------------------------------
# diskspace:    28.774 megabytes (may be less if installed over an old MATLAB)
#
#   boot files:         3.500 megabytes (required and recovered)
#   extracted files:   14.039 megabytes (required and recovered) 
#   installed files:   28.774 megabytes (less if installed over an old MATLAB)
#
	boot_size='3.25'
        echo $boot_size $PIECES | cat - $MAPS_DIR/piece.map | awk '
#--------------------------------------------------------------------------
    BEGIN { ipe = 0 }
    NR == 1 { bsize = $1 + 0; for (i = 2; i <= NF; i = i + 1) piece[i-1] = $i
              npe = NF - 1; next}
    NF == 0 { next }
    $1 ~ /^#.*/ { next }
		{ ipe = ipe + 1
		  csize[$1] = $3
	          size[$1] = $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)
	  dsize = tsize 
	  if (bsize + tcsize > dsize) dsize = bsize + tcsize
#--------------------------------------------------------------------------
printf "diskspace:      %7.3f megabytes* (less if installed over an old MATLAB)\n\n", dsize
printf "   boot files:         %7.3f megabytes  (required and recovered)\n", bsize
printf "   extracted files:    %7.3f megabytes  (required and recovered)\n", tcsize
printf "   installed files:    %7.3f megabytes* (less if installed over an old MATLAB)\n", tsize
#--------------------------------------------------------------------------
        }'
#--------------------------------------------------------------------------
echo ''
echo "'*' assumes all extracted toolboxes have been decrypted."
echo ''
#--------------------------------------------------------------------------
	mesg='Ready to extract product files from tape? ([y]/n) '
        . $dir/$echon_sh
        read ans
        if [ `expr "//$ans" : '//[Nn].*'` -gt 0 ]; then
	    continue
	else
	    break
	fi
    done
