#
# usage:        tpdscren.sh
#
# abstract:     This Bourne Shell script builds product screens and the
#		help screens for the 'collective tape' install.
#
# note(s):      1. This routine must be called using a . (period)
#
#		2. 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
#
#		3. Filename for each screen:
#
#		   screen paths = $MATLAB/update/tape/$$screen$i
#			    i=1,. . .,NSCREENS
#		   SCREEN = the scratch screen for the latest version
#			    of the current screen.
#
#		4. Currently, 24 lines of screen.map max per screen
#		   including blank lines
#
#		6. product screen layout
#
#--------------------------------------------------------------------------
#				Screen Layout
#
# 1   , 13  | 1    x - arch.sun4              13   x - tbx.chem (2.0a)
# 2   , 14  | 2    x - arch.hp700             14   x - tbx.signal (3.0b)
# ...
# 12  , 0   | 12   x - tbx.symbolic (1.0a)    [next screen]
#
# yyy , yyy | xxx  x - yyyyyyyyyyyyyyyyyyyyy  yyy  x - yyyyyyyyyyyyyyyyyyyyy
#
# 123456789012345
#             123456789012345678901234567890
#		123456789012345678901234567890123456789012345678901234567890
#--------------------------------------------------------------------------
#
#		   A script is built in SCREEN which when sourced creates
#		   the screens. The value returned by the awk script is
#		   the number of screens.
#
#		7. help screens
#
#		   Currently 3.
#
#		   See release/unix/tapehelp1.scn
#		   See release/unix/tapehelp2.scn
#		   See release/unix/tapehelp3.scn
#
#		   NHSCREENS = number of help screens
#
#		8. Be sure to remove any comments (lines that start with #)
#		   from
#
#			version.map
#			screen.map
#			
#
#		   NHSCREENS = number of help screens
#
# Copyright (c) 1994-1998 The Mathworks, Inc. All Rights Reserved.
# $Revision: 1.13 $  $Date: 1998/12/24 17:31:49 $
#----------------------------------------------------------------------------
#
#                             Build product screens
#                             ---------------------
#
    rm -f $SCREEN* > /dev/null 2>&1
#
# Output a file to source that sets:
#
#   variables: NARCHES, NPRODUCTS, NSCREENS, RANGE, ARCH_DEPEND_MAP
#
#   files: ${SCREEN}$i i=1, . . ., NSCREENS
#
    cat $MAPS_DIR/version.map > ${SCREEN}x
    echo 'EOF' >> ${SCREEN}x
    echo "$SCREEN" | cat - ${SCREEN}x $MAPS_DIR/screen.map | awk '
#----------------------------------------------------------------------------
#  size has to be an even number - set currently to 30, previous 24, 26
#
    BEGIN { narches = 0; nproducts = 0; nscreens = 0
            range = ""; depend = ""; nline = 0;  size = 30; start = 0
            squote = sprintf ("%c", 39)   # set single quote
            dquote = sprintf ("%c", 34)   # set double quote
	    state = 0
          }
#			ignore any commented lines
    substr($1,1,1) == "#" { next }
#
    NR == 1     { path = $1; next }
    $1 == "EOF" { state = 1; next }
                { if (state == 0) {
		      if (NF == 2) version[$1] = $2
		      next 
		  }
		}
# 				ignore any blank lines at the beginning
    NF == 0 { if (start == 0) next
              nline = nline + 1
	      if ( nline % size == 0 ) {
	          num[nline] = "0"
# 		             29 characters
		  line[nline] = "[next screen]                "
	          nline = nline + 1
	      }
	      num[nline] = "0"
# 		             30 blanks
#                            123456789012345678901234567890
	      line[nline] = "                              "
	      next }
    $1 ~ /arch\..*/ { narches = narches + 1; start = 1 }
    NF != 0 { start = 1
              nproducts = nproducts + 1
              found = 0
#
#----------------------------------------------------------------------------
# Syntax of screen line:
#
#   name        num | num:arch,...,arch | num:~arch,...,~arch | num.a
#----------------------------------------------------------------------------
#
              for (i = 2; i <= NF; i = i + 1)
		  if ( index($i,".a") != "0" || index($i,":") != "0" ) {  
		      depend = depend " " nproducts
		      found = 1
		      break
		  }
	      if (found == 0)
		  depend = depend " " "0"
	      nline = nline + 1
	      if ( nline % size == 0 ) {
		  line[nline] = "[next screen]                "
	          num[nline] = "0"
	          nline = nline + 1
	      }
	      num[nline] = nproducts
# 		                      30 characters
#				      xxx  x - 123456789012345678901
#                                     123456789012345678901234567890
	    
	      if (version[$1] != "")
	          line[nline] = sprintf("%-3d    - %-21s", nproducts, $1 " (" version[$1] ")")
	      else
	          line[nline] = sprintf("%-3d    - %-21s", nproducts, $1)
	    }
    END { 
#
# Back out any blank lines from the end
#
          for (i = nline; i >= 1; i = i - 1)
	      if (line[i] != "                             ") {
		  nline = i
		  break
	      }
	  rem = nline % size
	  nscreens = (nline - rem)/size
#
# If no extra be sure that the last is not [next screen]
#
          if ( rem == 0 ) {
	     if ( line[nline] == "[next screen]                 " ) 
	         line[nline] = "                              "
	  }
#
# If one extra, then move it to the last screen
#
          else if ( rem == 1 ) {
	      num[nline - 1] = num[nline]
	      line[nline - 1] = line[nline]
	      nline = nline - 1
	  }
	  else {
#
# Get the size of the last screen and be sure that there are enough
# blank lines to fill the screen.
#
               if ( rem > size/2 )
		   nadd = size - rem
	       else
		   nadd = rem
	       for (i = 1; i <= nadd; i = i + 1) {
		   nline = nline + 1
	           line[nline] = "                              "
	           num[nline] = "0"
	       }
	       nscreens = nscreens + 1
	  }
#
# Output the variables
#
    print "#!/bin/sh"
    print "#"
    print "NSCREENS=" nscreens
    print "NARCHES=" narches
    print "NPRODUCTS=" nproducts
#
# Determine the range
#
	  for (i = 1; i <= nscreens;  i = i + 1) {
	      start = (i - 1) * size + 1
	      if ( i == nscreens )
		 finish = nline
              else
		 finish = i * size
	      for (j = finish; j >= start ; j = j - 1)
		 if (num[j] != "0" ) {
		    range = range " " num[j]
		    break
		 }
	  }
    print "RANGE=" squote substr(range,2) squote
    print "ARCH_DEPEND_MAP=" squote substr(depend,2) squote
#
# Build the screens
#
	  for ( i = 1; i <= nscreens; i = i + 1 ) {
	      start = (i - 1) * size + 1
	      if ( i == nscreens )
		 finish = nline
              else
		 finish = i * size
	      screensize = (finish - start + 1) / 2
#
    print "cat > " path i " <<  EOF"
#
# yyy , yyy | xxx x - yyyyyyyyyyyyyyyyyyyyy  yyy x - yyyyyyyyyyyyyyyyyyyyy
#
	      for (j = 1; j <= screensize; j = j + 1) {
                  col1num = num[start + j - 1] ""
		  col2num = num[start + screensize + j - 1] ""
                  col1 = line[start + j - 1]
		  col2 = line[start + screensize + j - 1]
    out = sprintf("%-3s , %-3s | %-30s  %-30s", col1num, col2num, col1, col2 )
    print out
	      }
    print "EOF"
    print "#"
	  }
	}' > $SCREEN
#
    chmod +x $SCREEN
    . $SCREEN
#
# Build help screens
#
    cat > ${SCREEN}h1 << 'EOF'
                  Help for Select MATLAB Products [screen 1 of 3] 
--------------------------------------------------------------------------
 Change selection: (at the prompt type a space separated list of)

                 (m and n are item numbers on the current screen)
        +      - select all items listed on the screen
        m      - select just item m
        m-n    - select all items from item m through item n
       +m-n    - same as m-n	          
        -      - deselect all items listed on the screen
       -m      - deselect just item m
       -m-n    - deselect all items from item m through item n
        l      - select the license defaults that were determined
                 from the TMW_Archive FEATURE line(s) in the license.dat file

 Change screen: (at the prompt type one of the following) 

        b        - back a screen
     <return>    - next screen
        h        - first help screen
--------------------------------------------------------------------------
 <return> (next screen)  b (back a screen)  s (selection screen)
--------------------------------------------------------------------------
EOF
    cat > ${SCREEN}h2 << 'EOF'
		  Help for Select MATLAB Products [screen 2 of 3] 
-----------------------------------------------------------------------------
 Definitions:

    arch.'arch'    - tape contains files for all architecture dependent
                     products for architecture, 'arch', where 'arch' can be:

    sun4   - Sun Sparc (SunOS 4.1.4)        ibm_rs   - IBM RS/6000 (AIX 4.2)
    sol2   - Sun Sparc (Solaris 2.5)        sgi      - SGI (IRIX 6.2)
    hp700  - HP 9000   (HP-UX 10.01)        sgi64    - SGI (IRIX 6.2)
    alpha  - DEC Alpha (Digital UNIX 4.0A)  lnx86    - Linux (Linux 2.0.18)

    tbx.'toolbox'  - encrypted 'toolbox'
    'product'      - encrypted 'product' where product is other than matlab.
    lm.'arch'      - FLEXlm license manager software for architecture, 'arch'.
    decrypt.'arch' - decryption software for architecture, 'arch'.
-----------------------------------------------------------------------------
 <return> (next screen)  b (back a screen)  s (selection screen)
-----------------------------------------------------------------------------
EOF
    cat > ${SCREEN}h3 << 'EOF'
		  Help for Select MATLAB Products [screen 3 of 3] 
-----------------------------------------------------------------------------
 NOTES: 1. The product version in parenthesis follows the product name.
        2. Some products have architecture specific files. An arch.'arch'
           must be set BEFORE that product can be selected. The install
           program will enforce this rule.
        3. 'matlab' for a given architecture, 'arch', ALREADY contains items
           lm.'arch' and decrypt.'arch'. The install program will delete
           those items if selected in this case.
        4. Toolboxes alone require decrypt.'arch' for the local machine 'arch'.
           The install program will automatically add this item as required.
-----------------------------------------------------------------------------
 <return> (next screen)  b (back a screen)  s (selection screen)
-----------------------------------------------------------------------------
EOF
    NHSCREENS=3
