#
# usage:        license.sh
#
# abstract:     This Bourne Shell script outputs the license agreement
#		and queries the user for acceptance or rejection.
#
# note(s):      1. This routine must be called using a . (period)
#
# Copyright (c) 1997-2000 by The MathWorks, Inc.
# $Revision: 1.9 $  $Date: 2000/08/08 17:38:57 $
#-----------------------------------------------------------------------
#23456789012345678901234567890123456789012345678901234567890123456789012
#
#=======================================================================
#
# Outside Global variables:
#
#   ARCH - instr_char, window_rows [install_matlab]
#   license_txt - accept_license, lpr_license [mapname.sh]
#
# Inside Global variables:
#
# Outside Functions:
#
#   center_line ()
#   clean_temp_areas ()
#   clear_screen ()
#   echon () 
#   get_version ()
#
# Main function:
#
#   accept_license ()		<- imbed copyright.ins
#
# Functions:
#
#   answerquery ()
#   expand_tabs ()
#   intr_char ()
#   license_screens ()
#   lpr_license ()
#   printlicense ()
#   printquery ()
#   window_rows ()
#   welcome ()  
#
#=======================================================================
    accept_license () { # Outputs the license agreement and queries the
			# user for acceptance or rejection
		 	#
		 	# This routine automatically accepts the license
		 	# agreement in batch mode. It is outputted first
		 	#
		 	# It divides up the agreement into pages and
			# allows the user to scroll through them forward
			# or backward
		 	#
		 	# The user can accept or reject the license at
			# any point
		 	#
		 	# The first page has the Copyright notice before
			# the beginning of the license agreement
		 	#
		 	# A rows output of stty -a is use to determine
			# the page size
		 	#
		 	# A page is of the form:
		 	#
		 	#     title
	         	#     <blankline>
	         	#     lines of text
                 	#     <blankline if last line of text is not
			#      blank>
		 	#          on the first screen
		 	#
#-----------------------------------------------------------------------------
#
# ----------------------------------------------------------------------------
#                 >>>>>>>> For this License Agreement <<<<<<<<
# Enter either:  <return>       a        r        p      ^c
# To:          [next screen] [accept] [reject] [print] [abort]
# ----------------------------------------------------------------------------
# >
			#	   on the inbetween screens
# ----------------------------------------------------------------------------
#                >>>>>>>> For this License Agreement <<<<<<<<
# Enter either:   <return>      a        r        p          b           ^c
# To:          [next screen] [accept] [reject] [print] [back a screen] [abort]
# ----------------------------------------------------------------------------
# >
			#	   on the last screen 
# ----------------------------------------------------------------------------
#                 >>>>>>>> For this License Agreement <<<<<<<<
# Enter either:   a        r        p          b           ^c
# To:          [accept] [reject] [print] [back a screen] [abort]
# ----------------------------------------------------------------------------
# >
#
#-----------------------------------------------------------------------------
		 	#
		 	# files:
		 	#
		 	#     $LSCREEN  - (/tmp/$$lscreen) scratch
			#		   directory created if
			#		   necessary. 
		 	#
		 	# $temp_file	- copyright banner  (imbedded)
		 	# $temp_file2	- license banner
		 	# $LICENSE_FILE - license agreement
		 	# 		  (CD-ROM:
		 	# 		   $CDROM/license.txt   - not HP
		 	#		   $CDROM/LICENSE.TXT;1 - HP)
		 	#		  (FTP:
		 	# 		   $MATLAB/license.txt)
		 	#
                 	# Always returns a 0 status.
                 	# 
                 	# usage: accept_license
                 	# 
#//////////////////////////////////////////////////////////////////////////
# Copyright banner
cat > $temp_file << EOF
#imbed copyright.ins
EOF
#
#//////////////////////////////////////////////////////////////////////////
# License banner
cat > $temp_file2 << 'EOF'
   ---------------------------------------------------------------------------
   | Please review the license agreement on the next set of screens. On any  |
   | screen the options to proceed are at the bottom. Type your response     |
   | at the prompt.                                                          |
   ---------------------------------------------------------------------------
EOF
#//////////////////////////////////////////////////////////////////////////
#
        LICENSE_FILE="$license_txt"
#
#                       Build temporary directory for screens
#                       -------------------------------------
#
        LSCREEN=/tmp/$$lscreen
        rm -rf $LSCREEN
        mkdir $LSCREEN
#
        if [ "$batch" = "1" ]; then
            if [ "$CD_ROOT" = "" ]; then
	        clear_screen
	        printlicense
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
echo '--------------------------------------------------------------------------'
echo ' a (accept)     r (reject)'
echo '--------------------------------------------------------------------------'
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	        echon "> "
	        answerquery 'accept'
	    else
#
# License already approved by CD-ROM front end
#
	        clear_screen
		welcome
	        echo ""
	        cat $temp_file | expand_tabs
#------------------------------------------------------------------------------
    echo ''
    echo '    License Agreement accepted by customer . . .'
    echo ''
#------------------------------------------------------------------------------
	    fi
        else
	    NLSCREENS=`license_screens`
#
# loop for screens
#
            nls=1
            while [ $nls -le $NLSCREENS ]
            do  
	        clear_screen
                cat $LSCREEN/$nls
	        printquery $nls $NLSCREENS
	        answerquery
	        if [ $? -ne 0 ]; then
#
# Reject license agreement
#
#------------------------------------------------------------------------------
    echo ''
    echo '    License Agreement rejected by customer:  '"$CMDNAME"' terminated . . .'
    echo ''
#------------------------------------------------------------------------------
		    clean_temp_areas
    		    exit 1
	        fi
	    done
        fi
#
	rm -rf $temp_file $temp_file2 $temp_file3 $temp_file4 $LSCREEN
#
	return 0
    }
#=======================================================================
    answerquery () { # If no arguments then query the user for a
		     # response and determines how to continue.
		     #
		     #    1. ''    - next page (nls) but never more than 
		     #               $NLSCREENS
		     #    2. 'a'   - nls = $NLSCREENS + 1
		     #    3. 'r'   - returns a nonzero status
		     #    4. 'b'   - previous page (nls - 1) but never
		     #               less than 1
		     #    5. 'p'   - print the license file
		     #    6. other - print error and ask to continue
		     #
		     # If one argument then use this response.
		     #
                     # Returns a 0 status unless an error.
		     #
		     # answerquery ['continue'|'accept'|'reject']
		     #
	if [ $# -eq 0 ]; then
            read ans
            if [ "$ans" = "" ]; then
		if [ $nls -lt $NLSCREENS ]; then
                    nls=`expr $nls + 1`
		else
		    echon ''
	        fi
            elif [ `expr "//$ans" : '//[    ]*a.*'` -ne 0 ]; then
                nls=`expr $NLSCREENS + 1`
            elif [ `expr "//$ans" : '//[    ]*r.*'` -ne 0 ]; then
		return 1
            elif [ `expr "//$ans" : '//[    ]*b.*'` -ne 0 ]; then
		if [ $nls -gt 1 ]; then
                    nls=`expr $nls - 1`
		else
		    echon ''
	        fi
            elif [ `expr "//$ans" : '//[    ]*p.*'` -ne 0 ]; then
		lpr_license
	    else
#-----------------------------------------------------------------------------
    echo "Error: invalid input ($ans) . . ."
    echo ''
#-----------------------------------------------------------------------------
                echon 'Please press <return> to continue: '
                read ans
	    fi
	else
            echo $1
	fi
#
	return 0
    }
#=======================================================================
    expand_tabs() { # Filter to expand tabs. It is platform independent.
		    # Tab stops are at multiples of 8.
                    #
                    # Always returns a 0 status.
                    # 
                    # usage: expand_tabs
                    # 

	awk '
#-----------------------------------------------------------------------
    BEGIN { tab = "	" ; tabstop = 8 }
          { n = 0; line = ""
            for (i = 1; i <= length($0); i = i + 1) {
                c = substr($0,i,1)
                if (c != tab) {
                    n = n + 1
                    line = line c
                }
                else {
                    rem = n % tabstop
                    for (j = 1; j <= 8 - rem; j = j + 1) {
                        n = n + 1
                        line = line " "
                    }
                }
            }    
            print line
          }'
#-----------------------------------------------------------------------
	return 0
    }
#=======================================================================
    intr_char () { # Outputs the name of the interrupt character
		   # from stty -a.  It is platform specific:
		   #
		   # Older style: (sun4)
		   # erase  kill   werase rprnt  flush  lnext  susp   intr   quit   stop   eof
		   # ^?     ^U     ^W     ^R     ^O     ^V     ^Z/^Y  ^C     ^\     ^S/^Q  ^D     
		   # BUG: /usr/bin/stty can't be piped
		   #      Use /usr/5bin/stty if possible
		   #
		   # Newer style (SystemV): (the rest)
		   # intr = ^c; quit = ^|; erase = ^?; kill = ^u;
                   # 
                   # Always returns a 0 status.
                   # 
                   # usage: intr_char
                   # 
	case "$ARCH" in
	    sun4)
		if [ -f /usr/5bin/stty ]; then
                    /usr/5bin/stty -a | awk '
#-----------------------------------------------------------------------
      BEGIN { found = 0; default = "^C" }
            { if (found == 1) {
                   print $nf 
                   exit }
            } 
            { for ( i = 1; i <= NF; i = i + 1)
                  if ($i == "intr" ) {
                      nf = i
                      nr = NR;
                      found = 1
                  }
              next 
            } 
        END { if (found == 0) print default }'
#-----------------------------------------------------------------------
		else
		    echo "  "
		fi
		;;
	    *)
		stty -a | awk '
#-----------------------------------------------------------------------
	BEGIN { found = 0; default = "^C" }
	{ for (i = 1; i <= NF; i = i + 1)
		if ( $i == "intr" && $(i+1) == "=") {
		    intr = substr($(i+2),1,length($(i+2))-1)
		    print intr
		    found = 1
		    exit
		}
	}
	END { if (found == 0) print default }'
#-----------------------------------------------------------------------
		;;
	esac
        return 0
    }
#=======================================================================
    license_screens () { # Builds the license screens
		         #
			 # 1. Determine the number of rows by calling
			 #    window_rows
			 #
			 #    Use rows = rows - 9
			 #
			 # 2. Uses first line from the license agreement
			 #    as the license title. The following is
			 #    appended to the license title:
			 #    ' [screen i of $NLSCREENS]'
			 # 
			 # 3. The total file to output is:
		         #
		         #	Version title (centered) - excluded in count
		         #	<blank line>             - excluded in count
		         #	copyright banner (5)  
			 #      license banner   (5)
		         #	<blank line>
		         #	license agreement first line
			 #	underline of first line
			 #	rest of license agreement file
			 #
			 #      length = total length - excluded count
			 #
			 # 4. The screens are of the form:
			 #
			 #	title (centered) - excluded in count
			 #      title underline  - excluded in count
			 #	<blankline>      - excluded in count
			 #	lines of text
			 #      <blankline>
			 #      6 query lines    - excluded in screen count
			 #	
			 #    NLSCREENS = ceil (length/nrows)
			 #
			 #    The number of lines on a screen <= nrows
			 #
			 # 5. The screens are named:
			 #
			 #    ${LSCREEN}$i, i = 1, ..., $NLSCREENS 
			 #
			 # 6. We assume that the copyright banner and
			 #    the first license agreement title are on the
			 #    first page
			 #
		         # Always returns a zero status.
		         #
			 # usage: license_screens
			 #
	nrows=`window_rows`
#
# Remove any leading whitespace from the license title
#
	lic_title=`cat "$LICENSE_FILE" | sed -n '1p' | expand_tabs | awk '
#-----------------------------------------------------------------------
		{ ix = index($0,$1); print substr($0,ix) }'`
#-----------------------------------------------------------------------
#
	length=`cat $temp_file $temp_file2 "$LICENSE_FILE" | wc -l | awk '
#-----------------------------------------------------------------------
		{ print ($1 + 2) }'`
#-----------------------------------------------------------------------
#
	NLSCREENS=`echo $length $nrows | awk '
#-----------------------------------------------------------------------
                { l = $1; r = $2; rem = l % r
                  if (rem > 0)
                        print (l - rem) / r + 1
                  else
                        print l/r }'`
#-----------------------------------------------------------------------
#
	echo $nrows $NLSCREENS $LSCREEN > $temp_file3
	welcome >> $temp_file3
	echo "$lic_title" >> $temp_file3
#
	cat $temp_file $temp_file2 | expand_tabs >> $temp_file3
	echo "" >> $temp_file3
#
	tmpline="$lic_title [screen 1 of $NLSCREENS]"
        underline=`echo "$tmpline" | awk '
#-----------------------------------------------------------------------
		{ ix = index($0,"[")
		  u = ""; for (i = 1; i <= ix - 2; i = i + 1) u = u "-" 
		  for (i = ix - 1; i <= length($0); i = i + 1) u = u " "
		  print u }'`
#-----------------------------------------------------------------------
	center_line "$tmpline" >> $temp_file3
	center_line "$underline" >> $temp_file3
	cat "$LICENSE_FILE" | sed -n '2,$p' | expand_tabs >> $temp_file3
#
# Build a script to build the screens.
#
# Screen #1:
#	     first title
#	     <blankline>
#	     nrows of text
#
# Screen #i: i = 2, ..., NLSCREEN
#	     second title with [screen 2 of $NLSCREEN] appended
#	     <blankline>
#	     nrows of text
#
# Pseudocode:
# 	      print "#!/bin/sh"
#  	      print "cat > $LSCREEN/$i << EOF"
#  	      print text
#  	      ...
#             print "EOF"
#  
	cat $temp_file3 | awk '
#-----------------------------------------------------------------------
# blankhead is 40 characters
#
    BEGIN { blankhead = "                                        "
	    iscreen = 1; count = 0
	    squote = sprintf ("%c", 39)   # set single quote
	  }
    NR == 1 { nrows = $1; nlscreen = $2; lscreen = $3; next }
    NR == 2 { title1 = $0; next }
    NR == 3 { titlex = $0; len = length(titlex)
	      u = ""; for (i = 1; i <= len; i = i + 1) u = u "-" 
	      next
	    }
    count == 0 { if (iscreen == 1) {
		      print "#!/bin/sh"
		      print "cat > " lscreen "/" iscreen " << " squote "EOF" squote
		      print title1
		      print ""
		 }
		 else {
		      print "cat > " lscreen "/" iscreen " << " squote "EOF" squote
		      title = titlex " [screen " iscreen " of " nlscreen "]"
	              len = 40 - (length(title) / 2 - length(title) % 2)
	              print substr(blankhead,1,len) title
		      print substr(blankhead,1,len) u
		      print ""
		 }
	       }
	       { if (count < nrows) {
		     count = count + 1
		     print $0
		 }
		 if (count == nrows) {
		     print "EOF"
		     count = 0
		     iscreen = iscreen + 1
		 }
	       }
    END { if (count != 0) print "EOF" }' > $temp_file4
#-----------------------------------------------------------------------
	chmod +x $temp_file4
	. $temp_file4
#
	echo $NLSCREENS
	return 0
    }
#=======================================================================
    lpr_license () { # Print the license file using lpr
                     # 
                     # Always returns a 0 status.
                     # 
                     # usage: lpr_license
                     # 
	while true
	do
#-----------------------------------------------------------------------------
    echo ' '
    echo 'Please specify printer name. Press <return> for default in [] . . .'
    echon "[$PRINTER] > "
#-----------------------------------------------------------------------------
	    read ans
            if [ "$ans" != "" ]; then
	        PRINTER=$ans; export PRINTER
	    fi
#-----------------------------------------------------------------------------
    echo ' '
#-----------------------------------------------------------------------------
	    lpr "$LICENSE_FILE"
	    if [ $? -eq 0 ]; then
#-----------------------------------------------------------------------------
    echo "lpr command succeeded. Press <return> to continue  . . ."
#-----------------------------------------------------------------------------
		read ans
		break
	    fi
#-----------------------------------------------------------------------------
    echo '--------------------------------------------------------------------------'
    echo "lpr command failed!  We are trying to print . . ."
    echo ' '
    echo "    $license_txt"
    echo ' '
    echon 'Do you want to try to print again? ([y]/n) '
#-----------------------------------------------------------------------------
            read ans
            if [ `expr "//$ans" : '//[Nn].*'` -gt 0 ]; then
		break
	    fi
	done
        return 0
    }
#=======================================================================
    printlicense () { # Outputs to standard output: (for batch)
		      #
		      #		Version title (centered)
		      #		<blank line>
		      #	        copyright banner
		      #         license banner
		      #		<blank line>
		      #	        license agreement
		      #		<blank line>
                      #
		      # Always returns a zero status.
                      #
                      # usage: printlicense
                      #
#
	welcome
	echo ""
	cat $temp_file $temp_file2 | expand_tabs
	echo ""
#
# Underline the first line only
#
	cat "$LICENSE_FILE" | sed -n '1p' | expand_tabs | awk '
#-----------------------------------------------------------------------
		{ print $0
		  ix = index($0,$1)
		  u = ""; for (i = 1; i <= ix - 1; i = i + 1) u = u " " 
		  for (i = ix; i <= length($0); i = i + 1) u = u "-"
		  print u }'
#-----------------------------------------------------------------------
	cat "$LICENSE_FILE" | sed -n '2,$p' | expand_tabs
	echo ""
#
	return 0
    }
#=======================================================================
    printquery () { # Prints the query lines. If the two arguments are
		    # different then print the more case else print
		    # the end case
		    #
		    # Uses the intr parameter from stty -a to get the
		    # interrupt character.
		    #
		    # Always returns a zero status.
		    #
		    # usage: printquery n m
		    #
	n=$1
	m=$2
	intr=`intr_char`
	if [ "$n" != "$m" ]; then
	    if [ "$n" = "1" ]; then
# first screen
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
echo '----------------------------------------------------------------------------'
echo '                >>>>>>>> For this License Agreement <<<<<<<<'
echo "Enter either:  <return>       a        r        p      $intr"
echo 'To:          [next screen] [accept] [reject] [print] [abort]'
echo '----------------------------------------------------------------------------'
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	    else
# inbetween screens
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
echo '----------------------------------------------------------------------------'
echo '                >>>>>>>> For this License Agreement <<<<<<<<'
echo "Enter either:   <return>      a        r        p          b           $intr"
echo 'To:          [next screen] [accept] [reject] [print] [back a screen] [abort]'
echo '----------------------------------------------------------------------------'
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	    fi
	else
# last screen
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
echo '----------------------------------------------------------------------------'
echo '                >>>>>>>> For this License Agreement <<<<<<<<'
echo "Enter either:   a        r        p          b           $intr"
echo 'To:          [accept] [reject] [print] [back a screen] [abort]'
echo '----------------------------------------------------------------------------'
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	fi
	echon "> "
	return 0
    }
#=======================================================================
    window_rows () { # Outputs the number of rows in the display window
		     # from stty -a.  It is platform specific:
		     #
		     # System V style: (sol2,hpux,hp700)
		     #
		     # speed 9600 baud; 
		     # rows = 66; columns = 118; ypixels = 858; ...
		     # ...
		     #
		     # other style: (alpha,ibm_rs,sgi,glnx86)
		     #
		     # ... speed 9600 baud; 66 rows; 118 columns
		     # ...
		     #
                     # Always returns a 0 status.
                     # 
                     # usage: window_rows
                     # 
	case "$ARCH" in
	    alpha|ibm_rs|sgi|glnx86)
		stty -a | awk '
#-----------------------------------------------------------------------
	BEGIN { found = 0; extra = 9; min = 24; default = 24 }
	{ for (i = 1; i <= NF; i = i + 1)
		if ($i == "rows;")  {
		    nrows = $(i-1)
		    if (nrows < min) nrows = default
		    print (nrows - extra)
		    found = 1
		    exit
		}
	}
	END { if (found == 0) print (default - extra) }'
#-----------------------------------------------------------------------
		;;
	    *)
		stty -a | awk '
#-----------------------------------------------------------------------
	BEGIN { found = 0; extra = 9; min = extra + 8; default = 24 }
	{ for (i = 1; i <= NF; i = i + 1)
		if ($i == "rows" && $(i+1) == "=") {
		    nrows = substr($(i+2),1,length($(i+2))-1) + 0
		    if (nrows < min) nrows = default
		    print (nrows - extra)
		    found = 1
		    exit
		}
	}
	END { if (found == 0) print (default - extra) }'
#-----------------------------------------------------------------------
		;;
	esac
        return 0
    }
#=======================================================================
    welcome () { # Output the welcome line.
                 #
                 # Always returns a 0 status.
                 #
                 # usage: welcome
                 #
        center_line "Welcome to MATLAB  `get_version`  Install"
        return 0
    }
#=======================================================================
