#
# usage:        defltmap.sh
#
# abstract:     This Bourne Shell script determines
#
#               DEFAULT_MAP = size is NPRODUCTS. If default selected
#			      than number in product list (file
#			      screen.map) else 0
#
# note(s):      1. This routine must be called using a . (period)
#
#		2. VENDOR_STRINGS is determined in cktmw_al.sh
#
#		   This is the list of vendor strings from TMW_Archive
#		   lines that were successfully checked out.
#		
#		3. DECRYPT_PRODUCTS is determined by decryptl.sh
#		   by merging together the vendor strings and then
#		   mapping the numbers to the list in the file
#
#			install/vstring.map
#
#		   For example, if VENDOR_STRINGS = 1 then
#
#			DECRYPT_PRODUCTS = tbx.matlab
#
#		   Note: vstring.map does have repeats of products
#			 like
#			     
#				15 tbx.symbolic
#				16 tbx.symbolic
#
#			 and these get mentioned only once in the
#			 final list if bit 15 and 16 are mentioned.
#
#		4. DEFAULT_MAP is determined as follows:
#
#		   Read the screen.map. The current layout is:
#
#		   archlist b product helplist b matlab b productlist b lmlist
#		   arch.*     product help.*     matlab        *         lm.*
#
#		   A match is done between each item in DEFAULT_MAP and
#		   the first token in the productlist lines of screen.map
#		   that are not comments.
#
#		   Rules: 1. If tbx.matlab is selected then select matlab
#			  2. Choose the first arch if ARCH is not in the
#			     list.
#
#		   For example, for MATLAB only on sol2 including product and
#		   English help we have:
#
#			DEFAULT_MAP  0 2 0 0 0 0 0 0   arch*
#				     1 2 3 4 5 6 7 8
#
#				     9 10 0 	product help.*
#				     1  2 3
#
#				     12 13 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 product*
#				     1   2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2
#
#				     0 0 0 0 0 0 0 0 0 0 0 0 0 0    product*
#				     3 4 5 6 7 8 9 0 1 2 3 4 5 6
#
#				     0 0 0 0 0 0 0 0 lm*
#				     1 2 3 4 5 6 7 8
#
#		   REMEMBER: we do not count products which are duplicates and
#			     therefore not listed twice in the screen map or
#			     start with a comment (#) in the screen map.
#
# Copyright (c) 1994-1997 by The MathWorks, Inc.
# $Revision: 1.7 $  $Date: 1997/11/30 21:28:33 $
#----------------------------------------------------------------------------
#
#                               Get DEFAULT_MAP
#                               ---------------
#
    DECRYPT_PRODUCTS=
    if [ "$VENDOR_STRINGS" != "" ]; then
	vendor_string="$VENDOR_STRINGS"
	. $dir/$decryptl_sh
    fi
#
# Notes:
# 1. If tbx.matlab is specified then set matlab.
# 2. Use the first arch if the arch.$ARCH is not on the tape.
# 3. Currently we add current arch, product, and help.english defaults
#    if there are products to decrypt. This can be changed to include
#    more arches, and help.
#
    plist="$DECRYPT_PRODUCTS"
    if [ "$plist" != "" ]; then
	plist="arch.$ARCH product help.english $plist"
    fi
    DEFAULT_MAP=`echo "$plist" | cat - $MAPS_DIR/screen.map | awk '
#--------------------------------------------------------------------------
    BEGIN { na = 0; np = 0; matlabn = 0; tbxmatlab = 0 }
    NR == 1 { product = 0; help = 0
              for ( i = 1; i <= NF; i = i + 1) {
		  if ($i == "product") product = 1
		  if ($i ~ /^help\..*/) help = 1
		  if ( $i == "tbx.matlab" ) tbxmatlab = 1
		  a[$i] = "1"
	      }
	      next
	    }
    NF == 0 || substr($1,1,1) == "#" { next }
	    { np = np + 1; b[np] = $1; next }
    END { s = ""; arch = 0
	  for (i = 1; i <= np; i = i + 1) {
	      name = b[i]
	      if (name == "matlab" && tbxmatlab == 1) 
		  snext = sprintf ("%d ", i)
	      else if (a[name] == "1")
		  snext = sprintf ("%d ", i)
	      else if (name == "product" && product == 0 && help == 0)
		  snext = sprintf ("%d ", i)
	      else if (name == "help.english" && product == 0 && help == 0)
		  snext = sprintf ("%d ", i)
	      else
		  snext = sprintf ("%d ", 0)
	      if (a[name] == "1" &&  name ~ /^arch\..*/) arch = 1
	      s = s snext
	  }
#
# If no arch.ARCH matches one in screen.map then automatically select
# the first arch.
#
	  if (arch == 0) s = "1" substr(s,2)
#
          print substr(s,1,length(s)-1) }'`
#--------------------------------------------------------------------------
#
# Set LOCALARCH_ON_TAPE to 1 if arch is on tape.
#
    LOCALARCH_ON_TAPE=`echo $ARCH |cat - $MAPS_DIR/screen.map | awk '
#--------------------------------------------------------------------------
    BEGIN { na = 0 }
    NR == 1 { arch = $1; archn = 0; next }
    NF == 0          { next }
    $1 ~ /^arch\..*/ { na = na + 1
		       name = "arch." arch
		       if ( $1 == name ) archn = na; next }
		     { next }
    END { if (archn != 0)
	      print 1
	  else
	      print 0
	}'`
#--------------------------------------------------------------------------
