#
# usage:        options.sh
#
# abstract:     This Bourne Shell script parsers the options on the
#		command line.
#
# note(s):      1. This routine must be called using a . (period)
#		2. Options:
#
#     	   -h | -help   Help. Show command usage.
#     		   -b   Batch mode. Ignore all queries to continue.
#			Cannot be used with -m option.
#		   -d   (FTP only) Delete each ftp file immediately after
#		        it is untarred.
#		   -D   Documentation only install.
#		   -e   (Tape Only) Extract files from 'collective tape'
#			and delete 'collective tape' boot directory only.
#		   -E   (Tape Only) Extract files from 'collective tape' only.
#		   -f   Analyze TMW_Archive INCREMENT Lines in license.dat
#			file for validity.
#		   -l   Output hostname and lmhostid for this host only.
#		   -m   Minimal install. Do not allow changes to anything
#			outside the MATLAB root directory. Cannot be
#			specified with -b option.
#		   -o   Place a template license file in the path given by
#		        license_file. It will never replace an existing file.
#			If license_file is not specified 'etc/license.dat' is
#			assumed.
#		   -r   (Tape Only) Remove the 'collective tape' boot directory.
#		   -s   Skip the welcome messages. Used by 'collective tape'
#			install.
#     		   -t 	Terse mode. Only print screens that are critical
#			to the installation.
#		   -u   User mode. Ignore checking for superuser and network
#			superuser.
#		   -w   (FTP Only) Generate the size of and optionally the
#			list of FTP files to download.
#		   -x   Extract files only, i.e. do updating only.
#		   -X   Extract files and update scripts only.
#		-arch   Assume this architecture for installation.
#     		   type Type of installation:
#               	1 - normal
#               	2 - client
#               	3 - client with license manager
#               	4 - license manager only
#
# Copyright (c) 1992-1998 by The MathWorks, Inc.
# $Revision: 1.27 $  $Date: 1998/11/30 18:20:37 $
#----------------------------------------------------------------------------
#
    lmhostid_only=0
    licdat_only=0
    extract_only=0
    extract_fix_only=0
    terse=0
    skip_welcome=0
    remove_tape_dir=0
    textract_rm_only=0
    textract_only=0
    batch=0
    usermode=0
    check_tmw_al=0
    minimal_install=0
    ftp_select=0
#
    bad=0
    type=0
    nargs=$#
    msg=
    alone=0
    notalone=0
    doc_only_install=0
    while [ $# -gt 0 ]
    do
	case $1 in
	    -h|-help)				# help
		bad=1
		;;
	    -l)				# (alone) lmhostid
		alone=`expr $alone + 1`
		lmhostid_only=1
		;;
	    -o)				# (alone) skeleton license.dat file
		alone=`expr $alone + 1`
		if [ $# -eq 1 ]; then
		    licdat_only=1
		    licdat_path=etc/license.dat
		else
		    shift
		    licdat_only=1
		    licdat_path=$1
		fi
		;;
	    -x)				# extract files only
		notalone=`expr $notalone + 1`
		extract_only=1
		type=1
		;;
	    -X)				# extract files and update scripts
		notalone=`expr $notalone + 1`
		extract_fix_only=1
		type=1
		;;
	    -t)				# terse  
		notalone=`expr $notalone + 1`
		terse=1
		;;
	    -s)				# skip the welcome messages
		notalone=`expr $notalone + 1`
		skip_welcome=1
		;;
	    -r)				# (alone) (tape only) remove the
					# tape boot dir
		alone=`expr $alone + 1`
		remove_tape_dir=1
		if [ "$MODE" != "tape" ]; then
		    msg="no 'collective tape' boot directory loaded"
		    bad=1
		fi
		;;
	    -e)				# (alone) (tape only) extract files
				        # and remove tape boot dir only
		alone=`expr $alone + 1`
		textract_rm_only=1
		if [ "$MODE" != "tape" ]; then
		    msg="no 'collective tape' boot directory loaded"
		    bad=1
		fi
		;;
	    -E)		    		# (alone) (tape only) extract files only
		alone=`expr $alone + 1`
		textract_only=1
		if [ "$MODE" != "tape" ]; then
		    msg="no 'collective tape' boot directory loaded"
		    bad=1
		fi
		;;
	    -b)				# batch
		notalone=`expr $notalone + 1`
		batch=1
		;;
	    -u)				# install as non-superuser
		notalone=`expr $notalone + 1`
		usermode=1
		;;
	    -f)				# (only) analyze TMW_Archive feature
					# line(s)
		alone=`expr $alone + 1`
		check_tmw_al=1
		;;
	    -m)				# only make changes inside of $MATLAB
		notalone=`expr $notalone + 1`
		minimal_install=1
		;;
	    -w)				# (alone) (FTP only) generate list of
					# files to download
		alone=`expr $alone + 1`
    		ftp_select=1
		if [ ! -d $MATLAB/update/maps/ftp ]; then
		    msg='boot.ftp not loaded.'
		    bad=1
		fi
		;;
	     -d)			# delete ftp files as they are untarred
		notalone=`expr $notalone + 1`
		;;
	     -D)
                notalone=`expr $notalone + 1`
                doc_only_install=1
                type=1
		;;
	     -*)
                found=0
                for arch in $ARCH_LIST
                do
                    value=`expr "$1" : '-\(.*\)'`
                    if [ "$arch" = "$value" ]; then
                        found=1
                        break
                    fi
                done
                if [ "$found" = "0" ]; then
                    msg='usage'
		    bad=1
                fi
		;;
	     *)				# type of install and everything else
		notalone=`expr $notalone + 1`
		if [ "$1" != "1" -a "$1" != "2" -a "$1" != "3" -a "$1" != "4" ]; then
        	    msg='usage'
		    bad=1
		else
		    type=`expr $1 + 0`
		fi
		;;
	esac
	shift
	if [ "$bad" = "1" ]; then
	    break
	fi
    done
    alone=`expr $alone + 0`
    notalone=`expr $notalone + 0`
    if [ $alone -gt 1 ]; then
	bad=1
        msg='usage'
    elif [ $alone -gt 0 -a $notalone -gt 0 ]; then
#
# Next two cases for auto-updating from ftp
#
        if [ $alone -eq 1 -a "$ftp_select" = "1" -a $notalone -eq 1 -a "$batch" = "1" ]; then
	    :
	elif [ $alone -eq 1 -a "$ftp_select" = "1" -a $notalone -eq 2 -a "$batch" = "1" -a "$type" != "0" ]; then
	    :
	else
	    bad=1
            msg='usage'
	fi
    elif [ "$extract_only" = "1" -a "$type" != "1" ]; then
	bad=1
        msg='usage'
    elif [ "$extract_fix_only" = "1" -a "$type" != "1" ]; then
	bad=1
        msg='usage'
    elif [ "$doc_only_install" = "1" -a "$type" != "1" ]; then
	bad=1
        msg='usage'
    elif [ "$extract_only" = "1" -a "$extract_fix_only" = "1" ]; then
	bad=1
        msg='usage'
    elif [ "$extract_only" = "1" -a "$doc_only_install" = "1" ]; then
	bad=1
        msg='usage'
    elif [ "$extract_fix_only" = "1" -a "$doc_only_install" = "1" ]; then
	bad=1
        msg='usage'
    elif [ "$batch" = "1" -a "$minimal_install" = "1" ]; then
	bad=1
        msg='usage'
    fi
    if [ "$bad" = "1" ]; then
	if [ "$msg" != "" ]; then
	    cat /dev/null > $temp_file 2>&1
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	cat > $temp_file << EOF
    echo '--------------------------------------------------------------------'
    echo "    Error: $msg"
EOF
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	fi
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	cat >> $temp_file << EOF
    echo '--------------------------------------------------------------------'
    echo ' '
    echo '    Usage: install_matlab [-h|-help] | -l | -o [license_file] |'
    echo '                                  -f | -w | -e | -E | -r |' 
    echo '                      [-d] [-t] [-s] [-b|-m] [1] [-u] -x |'
    echo '                      [-d] [-t] [-s] [-b|-m] [1] [-u] -X |'
    echo '                      [-d] [-t] [-s] [-b|-m] [1] [-u] -D |'
    echo '                      [-d] [-t] [-s] [-b|-m] [-u] [-arch] [type]'
    echo ' '
    echo '      -h    Help. Show command usage.'
    echo '      -help'
    echo ' '
    echo '      -b    Batch mode. Ignore all queries to continue. Cannot'
    echo '            be specified with -m option.'
    echo ' '
    echo '      -d    (FTP only) Delete each ftp file immediately after it is'
    echo '            untarred.'
    echo ' '
    echo '      -D    Documentation only install. Like -X except only'
    echo '            update help related scripts and links.'
    echo ' '
    echo "      -e    (Tape Only) Extract files from 'collective tape' and"
    echo "            delete 'collective tape' boot directory only."
    echo ' '
    echo "      -E    (Tape Only) Extract files from 'collective tape' only."  
    echo ' '
    echo '      -f    Analyze TMW_Archive INCREMENT Lines in license.dat file'
    echo '            for validity.'
    echo ' '
    echo '      -l    Output hostname and lmhostid for this host only.'
    echo ' '
    echo '      -m    Minimal install. Do not allow changes to anything'
    echo '            outside the MATLAB root directory. Cannot be specified'
    echo '            with -b option.'
    echo ' '
    echo '      -o    Place a template license file in the path given by'
    echo '            license_file. It will never replace an existing file.'
    echo "            If license_file is not specified 'etc/license.dat' is"
    echo '            assumed.'
    echo ' '
    echo '      -r    (Tape Only) Remove the 'collective tape' boot directory.'
    echo ' '
    echo '      -s    Skip welcome messages.'
    echo ' '
    echo '      -t    Terse mode. Print critical screens only.'
    echo ' '
    echo '      -u    User mode. Ignore checking for superuser and network'
    echo '            superuser.'
    echo ' '
    echo '      -w    (FTP Only) Generate the size of and optionally the list'
    echo '            of FTP files to download.'
    echo ' '
    echo '      -x    Extract files only, i.e. do updating only.'
    echo ' '  
    echo '      -X    Extract files and update scripts only.'
    echo ' '
    echo '      -arch Assume this architecture for installation.'
    echo ' '
    echo '      type Type of installation:'
    echo '           1 - normal      3 - client with license manager'
    echo '           2 - client      4 - license manager only'
    echo ' '
    echo '--------------------------------------------------------------------'
EOF
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	chmod +x $temp_file
	. $temp_file | more
        . $dir/$cleanup_sh
	exit 0
    fi
