#
# usage:        fpdscren.sh
#
# abstract:     This Bourne Shell script builds product screens and the
#		help screens for ftp product selection.
#
# 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
#		   HELP_DEPEND_MAP = size is NPRODUCTS. If product
#				     depends on doc then number in
#				     product list (file screen.map) else 0
#
#		3. Filenames:
#		   
#		   Working directory:  $MATLAB/update/ftp
#				       (removed by cleanup.sh and
#					cleanup2.sh)
#				
#		   SCREEN = $MATLAB/update/ftp/$$screen
#			        (set in ftp.sh)
#		
#		   screen paths = $MATLAB/update/ftp/$$screen$i
#			    i=1,. . .,NSCREENS
#
#		   ${SCREEN}x = scratch area
#		   $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 - yyyyyyyyyyyyyyyyyyyyyyyyyy yyy  x - yyyyyyyyyyyyyyyyyyyyyyyyyy
# yyy , yyy | xxx  x - yyyyyyyyyyyyyyyyyyyyyyyyyy yyy  x - yyyyyyyyyyyyyyyyyyyyyyyyyy
#
# 123456789012345
#             12345678901234567890123456789012345 12345678901234567890123456789012345
#            0        1         2         3         4         5         6         7
#--------------------------------------------------------------------------
#
#		   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/ftphelp1.scn
#		   See release/unix/ftphelp2.scn
#		   See release/unix/ftphelp3.scn
#
#		   NHSCREENS = number of help screens
#
#		8. Be sure to remove any comments (lines that start with #)
#		   from
#
#			version.map
#			screen.map
#			
#
# Copyright (c) 1994-1998 The Mathworks, Inc. All Rights Reserved.
# $Revision: 1.10 $  $Date: 1998/12/30 22:56:04 $
#----------------------------------------------------------------------------
#=======================================================================
# Outside Global variables:
#
#   batch  - lines_for_screen
#
# Inside Globals variables:
#
# Outside Functions:
#
# Functions:
#
#   lines_for_screen ()
#
#=======================================================================
    lines_for_screen() { # Returns the maximum number of lines used in
                         # each screen. In batch, there is only one
                         # screen. It MUST be even.
                         #
                         # not batch:   30   (has been 24 and 26)
                         # batch:       next_lower_even_number_of
                         #              (non-comment lines in $MAPS_DIR/
                         #               screen.map + 1)
                         #
                         # Always returns a 0 status.
                         #
                         # usage: lines_for_screen
                         #
        if [ "$batch" = "0" ]; then
            echo "30"
        else
            cat $MAPS_DIR/screen.map | awk '
#----------------------------------------------------------------------------
        BEGIN { n = 0 }
        substr($1,1,1) != "#" { n = n + 1}
        END { print (n + 1) - (n + 1) % 2 }'
#----------------------------------------------------------------------------
        fi
        return 0
    }
#=======================================================================
#
#                             Build product screens
#                             ---------------------
#
    rm -f $SCREEN*
#
# Input file:
#
#   line:  $SCREEN      (path)
#   file:  version.map 
#   line:  EOF		(separator)
#   file:  screen.map
#
# Output file: Bourne Shell source that creates -
#
#   variables: NARCHES, NHELP, NPRODUCTS, NSCREENS, RANGE, ARCH_DEPEND_MAP,
#	       HELP_DEPEND_MAP
#
#   files: ${SCREEN}$i i=1, . . ., NSCREENS
#
    cat $MAPS_DIR/version.map > ${SCREEN}x
    echo 'EOF' >> ${SCREEN}x
    echo "$SCREEN" `lines_for_screen` | cat - ${SCREEN}x $MAPS_DIR/screen.map | awk '
#----------------------------------------------------------------------------
    BEGIN { narches = 0; nhelp = 0; nproducts = 0; nscreens = 0
            range = ""; depend_arch = ""; depend_help = ""
            nline = 0; 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; size = $2; 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 }
    $1 ~ /^help\..*/ { nhelp = nhelp + 1 }
    NF != 0 { start = 1
              nproducts = nproducts + 1
              found = 0
#
#----------------------------------------------------------------------------
# Syntax of screen line:
#
#   name        num | num(.*) | num:arch,...,arch   | num(.*):arch,...,arch |
#                     num:~arch,...,~arch | num(.*):~arch,...,~arch |
#                     num(.a) | num(.a,.*) |
#                     num(.h) | num(.h,.*)
#----------------------------------------------------------------------------
#
              for (i = 2; i <= NF; i = i + 1)
		  if ( index($i,".a") != "0" || index($i,":") != "0" ) {
		      depend_arch = depend_arch " " nproducts
		      found = 1
		      break
		  }
	      if (found == 0)
		  depend_arch = depend_arch " " "0"
              found = 0
              for (i = 2; i <= NF; i = i + 1)
		  if ( index($i,".h") != "0" ) {
		      depend_help = depend_help " " nproducts
		      found = 1
		      break
		  }
	      if (found == 0)
		  depend_help = depend_help " " "0"
	      nline = nline + 1
	      if ( nline % size == 0 ) {
		  line[nline] = "[next screen]                "
	          num[nline] = "0"
	          nline = nline + 1
	      }
	      num[nline] = nproducts
# 		                      35 characters (minimum)
#				      xxx  x - 12345678901234567890123456
#                                     12345678901234567890123456789012345
	    
	      if (version[$1] != "")
	          line[nline] = sprintf("%-3d    - %-26s", nproducts, $1 " (" version[$1] ")")
	      else
	          line[nline] = sprintf("%-3d    - %-26s", 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 "NHELP=" nhelp
    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_arch,2) squote
    print "HELP_DEPEND_MAP=" squote substr(depend_help,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 - yyyyyyyyyyyyyyyyyyyyyyyyyy yyy  x - yyyyyyyyyyyyyyyyyyyyyyyyyy
#
	      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 | %-35s %-35s", 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'    - include architecture dependent files for 'arch', where
                     'arch' can be:

                                            
    sol2   - Sun Sparc   (Solaris 2.5.1)      sgi      - SGI (IRIX 6.3)
    hp700  - HP 9000     (HP-UX 10.20)        sgi64    - SGI (IRIX 6.4)
    alpha  - DEC Alpha   (Digital UNIX 4.0D)  lnx86    - Linux (Linux 2.0.34)
    ibm_rs - IBM RS/6000 (AIX 4.2)

    product        - include files for each 'product' other than help.
    help.english   - include any English help files for each 'product'.
    help.japanese  - include any Japanese help files for each 'product'.

    'product'      - encrypted 'product' like matlab, etc.

    lm.'arch'      - FLEXlm license manager 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.
        3. 'matlab' for a given architecture, 'arch', ALREADY contains
           lm.'arch'.
-----------------------------------------------------------------------------
 <return> (next screen)  b (back a screen)  s (selection screen)
-----------------------------------------------------------------------------
EOF
    NHSCREENS=3
