#! /bin/sh
#
#  Name:
#
#     matlabdoc   script to access key parts of the MATLAB Online
#		  Documentation. This can require using a WEB
#                 browser, the Adobe Acrobat Reader, and the
#		  'more' command.
#     
#  Usage:
#
#     matlabdoc|doc  [-h|-help] | [-i] [browser] |
#			          -a [[reader] pdf_file] |
#			          -man [name] | -ascii [name]
#
#  Description:
#
#     This Bourne Shell script sits in the MATLAB root directory or
#     at the top of CDROM.  It can be used to start up a WEB browser
#     on the base documentation page or the Adobe Acrobat Reader on
#     a specific file or a manual page using 'more' or an ASCII
#     README file using 'more'.
#
#     Currently the MATLAB Help Desk is not viewable from the CDROM.
#
#  Options:
#
#     -h | -help 	- help. Print usage.
#
#     -i		- (Netscape Browser only) always bring up a
#			  new Netscape browser. Ignore the lock to an
#			  existing Netscape browser.
#
#     browser	        - path to browser if the program can not find
#			  one on your path.
#
#     -a		- (alone) print a list of the pdf documents
#			  available in the MATLAB Online Documentation.
#
#     reader		- path to Adobe acrobat Reader if the program
#			  cannot find it on your path.
#
#     pdf_file          - name of pdf file (without the path or
#			  extension). It should be on the list gotten
#			  by -a.
#
#     -man		- list and view manual pages using 'more'.
#
#     -ascii	  	- list and view the ASCII README files using
#			  'more'.
#
#     name		- start with this particular manual page or
#			  ASCII README file.
#
#  Copyright (c) 1996-1998 by The MathWorks, Inc.
#  $Revision: 1.11 $  $Date: 1998/08/25 12:20:14 $
#-----------------------------------------------------------------------
#23456789012345678901234567890123456789012345678901234567890123456789012
#
    arg0_=$0
#
    temp_file=/tmp/$$a
#
    trap "rm -rf $temp_file > /dev/null 2>&1; exit 1" 1 2 3 15
#
#=======================================================================
# Functions:
#   scriptpath ()
#   actualpath ()
#   bld_man_dir ()
#   bld_readme_dir ()
#   clearsc ()
#   echon ()
#   listman ()
#   listreadme ()
#   listpdf ()
#   output_howto_help ()
#   process_exist ()
#   searchpath ()
#   searchpdf ()
#   to9660 ()
#   setbrowser_env ()
#=======================================================================
    scriptpath () { # returns path of this script as a directory,
                    # ROOTDIR, and command name, CMDNAME.
		    #
		    # Returns a 0 status unless an error.
		    #
                    # usage: scriptpath
                    #
#
	filename=$arg0_
#
# Now it is either a file or a link to a file.
#
        cpath=`pwd`
#
# Follow up to 8 links before giving up. Same as BSD 4.3
#
        n=1
        maxlinks=8
        while [ $n -le $maxlinks ]
        do
#
# Get directory correctly!
#
	    newdir=`echo "$filename" | awk '
                { tail = $0
                  np = index (tail, "/")
                  while ( np != 0 ) {
                      tail = substr (tail, np + 1, length (tail) - np)
                      if (tail == "" ) break
                      np = index (tail, "/")
                  }
                  head = substr ($0, 1, length ($0) - length (tail))
                  if ( tail == "." || tail == "..")
                      print $0
                  else
                      print head
                }'`
	    if [ ! "$newdir" ]; then
	        newdir="."
	    fi
	    (cd $newdir) > /dev/null 2>&1
	    if [ $? -ne 0 ]; then
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo ''
    echo 'Internal error 1: We could not determine the path of the'
    echo '                  matlabdoc command.'
    echo ''
    echo "                  original command path = $arg0_"
    echo "                  current  command path = $filename"
    echo ''
    echo '                  Please contact:'
    echo '' 
    echo '                      MathWorks Technical Support'
    echo ''
    echo '                  for further assistance.'
    echo ''
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	        return 1
	    fi
	    cd $newdir
#
# Need the function pwd - not the built in one
#
	    newdir=`/bin/pwd`
#
	    newbase=`expr //$filename : '.*/\(.*\)' \| $filename`
            lscmd=`ls -l $newbase 2>/dev/null`
	    if [ ! "$lscmd" ]; then
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo ''
    echo 'Internal error 2: Could not determine the path of the'
    echo '                  matlabdoc command.'
    echo ''
    echo "                  original command path = $filename"
    echo "                  current  command path = $filename"
    echo ''
    echo '                  Please contact:'
    echo '' 
    echo '                      MathWorks Technical Support'
    echo ''
    echo '                  for further assistance.'
    echo ''
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	        return 1
	    fi
#
# Check for link portably
#
	    if [ `expr "$lscmd" : '.*->.*'` -ne 0 ]; then
	        filename=`echo "$lscmd" | awk '{ print $NF }'`
	    else
#
# It's a file
#
	        dir="$newdir"
	        CMDNAME="$newbase"
	        ROOTDIR=`/bin/pwd`
	        break
	    fi
	    n=`expr $n + 1`
        done
        if [ $n -gt $maxlinks ]; then
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo ''
    echo 'Internal error 3: More than $maxlinks links in path to'
    echo "                  this script. That's too many!"
    echo ''
    echo "                  original command path = $filename"
    echo "                  current  command path = $filename"
    echo ''
    echo '                  Please contact:'
    echo '' 
    echo '                      MathWorks Technical Support'
    echo ''
    echo '                  for further assistance.'
    echo ''
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	    return 1
        fi
        cd $cpath
        return 0
    }
#=======================================================================
    actualpath () { # Determine the actual path of a file following
		    # all links. Returns the real path or null if it
		    # is not a file.
                    #
		    # Always returns a 0 status.
		    #
                    # usage: actualpath path
                    #
        File=$1
        Filepath=
#
        lsCmd=`ls -ld $File 2>/dev/null`
        if [ "$lsCmd" ]; then
#
# Check for link portably
#
	    if [ `expr "$lsCmd" : '.*->.*'` -eq 0 ]; then
    	        if [ ! -d $File ]; then
	            Filepath=$File
                fi
            else
#
# A directory link?
#
                (cd $File) > /dev/null 2>&1
                if [ $? -eq 0 ]; then
	            :
                else
#
# Now it is either a file, link to a file, or still a bad path.
#
	            cpath=`/bin/pwd`
                    localFile=$File
#
# Follow up to 8 links before giving up. Same as BSD 4.3
#
	            n=1
    	            while [ $n -le 8 ]
    	            do
#
# Get directory correctly!
#
   	                newDir=`echo "$localFile" | awk '
	{ tail = $0
          np = index (tail, "/")
          while ( np != 0 ) {
              tail = substr (tail, np + 1, length (tail) - np)
              if (tail == "" ) break
              np = index (tail, "/")
          }
          head = substr ($0, 1, length ($0) - length (tail))
          if ( tail == "." || tail == "..")
              print $0
          else
              print head
        }'`
	                if [ ! "$newDir" ]; then
	                    newDir="."
	                fi
	                (cd $newDir) > /dev/null 2>&1
	                if [ $? -ne 0 ]; then
		            break
	                fi
	                cd $newDir
	                newDir=`/bin/pwd`
	 newBase=`expr //$localFile : '.*/\(.*\)' \| $localFile`
                        lsCmd=`ls -l $newBase 2>/dev/null`
	                if [ ! "$lsCmd" ]; then
		            break
	                fi
#
# Check for link portably
#
	                if [ `expr "$lsCmd" : '.*->.*'` -ne 0 ]; then
	                    localFile=`echo "$lsCmd" | \
				awk '{ print $NF }'`
	                else
#
# It's a file
#
	                    Filepath=$newDir/$newBase
	                fi
		        n=`expr $n + 1`
    	            done
    	            cd $cpath
	        fi
	    fi
        fi
	echo $Filepath
	return 0
    }
#=======================================================================
    bld_man_dir () { # Builds a directory of links to the
		     # real man pages. Files considered and
		     # the mappings are:
		     #
		     # MATLAB:
		     #  
		     #    $ROOTDIR/man/formatted/name.1   -> $DIR/name
		     # 
		     # CDROM (ufs):
		     #	
		     #    $ROOTDIR/man/name.1    -> $DIR/name
		     #    (Map any 8.3 names to full names using
		     #     $ROOTDIR/man/83tofull.sh)
		     #
		     # CDROM (9660):
		     # 
		     #    $ROOTDIR/MAN/NAME.1;1   -> $DIR/name
		     #    (Map any 8.3 names to full names using
		     #     $ROOTDIR/MAN/83TOFULL.SH;1)
		     # 
		     # DIR is the value passed as an argument.
		     # It is always ufs format. It is initially
		     # created.
		     #
		     # Returns a 0 status if no error otherwise 1.
		     #
		     # usage: bld_man_dir DIR
		     #
	DIR=$1
	rm -rf $DIR > /dev/null 2>&1
	mkdir $DIR
	if [ "$oncdrom" = "0" ]; then
	    list=`(cd "$ROOTDIR/man/formatted"; ls *.1 2>/dev/null)`
        elif [ "$is9660" = "0" ]; then
	    list=`(cd "$ROOTDIR/man"; ls *.1 2>/dev/null)`
        else
	    list=`(cd "$ROOTDIR/MAN"; ls *.1';'* 2>/dev/null)`
	fi
	if [ "$list" = "" ]; then
#-----------------------------------------------------------------------
echo " "
echo "    Error: Cannot find any manual files . . ."
echo "           Please contact MathWorks Technical Support."
echo " "
#-----------------------------------------------------------------------
	    exit 1
	fi
	for file in $list
	do
	    if [ "$oncdrom" = "0" ]; then
                base=`expr "$file" : '\([^\.]*\)\.[^\.]*$'`
	        ln -s "$ROOTDIR/man/formatted/$file" $DIR/$base
            elif [ "$is9660" = "0" ]; then
                base=`expr "$file" : '\([^\.]*\)\.[^\.]*$'`
	        ln -s "$ROOTDIR/man/$file" $DIR/$base
            else
                base=`expr "$file" : '\([^\.]*\)\.[^\.]*$' | tr 'A-Z' 'a-z'`
	        ln -s "$ROOTDIR/MAN/$file" $DIR/$base
	    fi
	done
#
# Fix the mapping using 83tofull.sh
#
	if [ "$oncdrom" = "0" ]; then
	    :
        elif [ "$is9660" = "0" ]; then
	    (cd $DIR; $ROOTDIR/man/83tofull.sh)
        else
	    (cd $DIR; $ROOTDIR/MAN/"83TOFULL.SH;1")
	fi
	return 0
    }
#=======================================================================
    bld_readme_dir () { # Builds a directory of links to the
			# real README files. Files considered and
			# the mappings are:
			#
			# MATLAB:
		        #  
			#    $ROOTDIR/README.name   -> $DIR/name
			#    $ROOTDIR/*/README.name -> $DIR/name
			# 
		        # CDROM (ufs):
			#	
			#    $ROOTDIR/name.asc     -> $DIR/name
			#    $ROOTDIR/install.asc -> $DIR/install
			#    $ROOTDIR/*/name.asc   -> $DIR/name
			#    $ROOTDIR/*/install.asc -> $DIR/install
		        #
			# CDROM (9660):
			# 
			#    $ROOTDIR/NAME.ASC;1     -> $DIR/name
			#    $ROOTDIR/INSTALL.ASC;1 -> $DIR/install
			#    $ROOTDIR/*/NAME.ASC;1   -> $DIR/name
			#    $ROOTDIR/*/INSTALL.ASC;1 -> $DIR/install
			# 
			# DIR is the value passed as an argument.
			# It is always ufs format. It is initially
			# created.
			#
			# We will assumed that name is unique
		        # across all the directories.
			#
			# Returns a 0 status if no error otherwise 1.
			#
			# usage: bld_readme_dir DIR
			#
	DIR=$1
	rm -rf $DIR > /dev/null 2>&1
	mkdir $DIR
	(cd "$ROOTDIR"
	 if [ "$oncdrom" = "0" ]; then
	     list=`ls README.* */README.* 2>/dev/null`
         elif [ "$is9660" = "0" ]; then
	     list=`ls *.asc */*.asc 2>/dev/null`
         else
	     list=`ls *.ASC';'* */*.ASC';'* 2>/dev/null`
	 fi
	 if [ "$list" = "" ]; then
#-----------------------------------------------------------------------
echo " "
echo "    Error: Cannot find any README files . . ."
echo "           Please contact MathWorks Technical Support."
echo " "
#-----------------------------------------------------------------------
	     exit 1
	 fi
	 for file in $list
	 do
	     if [ "$oncdrom" = "0" ]; then
	         base=`expr "$file" : '.*\.\([^\.]*\)$'`
             elif [ "$is9660" = "0" ]; then
		 base=`echo "$file" | tr '/' ' ' | \
		       awk '{ix = index($NF,"."); print substr($NF,1,ix-1)}'`
             else
		 base=`echo "$file" | tr 'A-Z/' 'a-z ' | \
		       awk '{ix = index($NF,"."); print substr($NF,1,ix-1)}'`
	     fi
#
# Only link up new files
#
	     if [ ! -f $DIR/$base ]; then
	         ln -s "$ROOTDIR/$file" $DIR/$base
	     fi
	 done)
	return $?
    }
#=======================================================================
    clearsc () { # Clears the screen.
	         #
	         # Always returns a 0 status. 
	         #
	         # usage: clearsc
	         #
	clear
	return 0
    }
#=======================================================================
    echon () { # Echo's out a message without the newline so that the 
	       # user can be prompted.
	       #
	       # Always returns a 0 status. 
	       #
	       # usage: echon mesg
	       #
        if [ "`echo -n`" != "" ]; then
            echo "$1\c"
        else
            echo -n "$1"
        fi
	return 0
    }
#=======================================================================
    listman () { # Search dir for manual pages. If file is not null
		 # then attempt to more this page first before listing
		 # all the files. Remove any trailing numbers when
		 # listing. An error occurs if the formatted man pages
		 # do not exist.
                 #
		 # Returns 0 status if no error otherwise 1.
		 #
                 # usage: listman dir file
                 #
        dir=$1
	file=""
	if [ $# -eq 2 ]; then
	    file=$2
	fi
	(cd $dir) > /dev/null 2>&1
	if [ $? -ne 0 ]; then
#-----------------------------------------------------------------------
echo " "
echo "    Error: Cannot find any formatted man pages . . ."
echo "           Please contact MathWorks Technical Support."
echo " "
#-----------------------------------------------------------------------
	    return 1
	fi
	if [ "$file" != "" ]; then
	    flist=`ls -d $dir/$file* 2>/dev/null`
	    if [ "$flist" != "" ]; then
		clearsc
		more $flist*
	    else
#-----------------------------------------------------------------------
echo "-----------------------------------------------------------------------" 
echo " "
echo "    Error: Man page $file not found . . ."
echo " "
#-----------------------------------------------------------------------
	    fi
	fi
	(cd $dir
#-----------------------------------------------------------------------
echo " "
echo "    Man pages :"
echo " "
#-----------------------------------------------------------------------
	  ls -C * | expand | tr 'A-Z' 'a-z' | \
			     sed -e 's/\.[0-9]*[ ]//g' \
	     			 -e 's/\.[0-9]*$//' \
	     			 -e 's/^/        /'
	 )
#-----------------------------------------------------------------------
echo " "
#-----------------------------------------------------------------------
	return 0
    }
#=======================================================================
    listreadme () { # List contents of dir. If file is not null
		    # then attempt to more this file first before
		    # listing all the files. There will always be at
		    # least one file.
		    #
		    # Always returns 0 status.
		    #
                    # usage: listreadme dir file
                    #
        dir=$1
	file=""
	if [ $# -eq 2 ]; then
	    file=$2
	fi
	if [ "$file" != "" ]; then
	    flist=`ls -d $dir/$file* 2>/dev/null`
	    if [ "$flist" != "" ]; then
		clearsc
		more $flist*
	    else
#-----------------------------------------------------------------------
echo "-----------------------------------------------------------------------" 
echo " "
echo "    Error: README file $file not found . . ."
echo " "
#-----------------------------------------------------------------------
	    fi
	fi
	(cd $dir
#-----------------------------------------------------------------------
echo " "
echo "    README files :"
echo " "
#-----------------------------------------------------------------------
	  ls -C | expand | sed -e 's/^/        /'
	 )
#-----------------------------------------------------------------------
echo " "
#-----------------------------------------------------------------------
	return 0
    }
#=======================================================================
    listpdf () { # Search all the help directories and list the
		 # pdf files. An error occurs if the Help Desk does
		 # not exist.
                 #
		 # Returns 0 status if no error otherwise 1.
		 #
                 # usage: listpdf
                 #
	(cd "$ROOTDIR/`to9660 help/`") > /dev/null 2>&1
	if [ $? -ne 0 ]; then
#-----------------------------------------------------------------------
echo " "
echo "    Error: Cannot find any pdf files. No MATLAB Help Desk ..."
echo "           Please install the MATLAB Help Desk from media or ftp."
echo " "
#-----------------------------------------------------------------------
	    return 1
	fi
	(cd $ROOTDIR/help
	    dirlist=`find . -type f -name '*.pdf' -print | sort | awk '
#-----------------------------------------------------------------------
    BEGIN { newdir = 0
	    lastdir = "" }
	  { n = split($1,d,"/")
	    file = d[n]
	    l = length($1)
	    dir = substr($1,1, l - length(file) - 1)
	    if (lastdir != dir) print substr(dir,3) 
	    lastdir = dir
	  }'`
#-----------------------------------------------------------------------
#
         for dir in $dirlist
	 do
#-----------------------------------------------------------------------
echo " "
echo "    In help/$dir :"
echo " "
#-----------------------------------------------------------------------
	    (cd $dir; ls -C *.pdf | expand | sed 's/\.pdf//g'| \
	     sed -e 's/^/        /')
	 done)
#-----------------------------------------------------------------------
echo " "
echo "    To view a file from one of the list(s) above try:"
echo " "
echo "        matlabdoc -a file"
echo " "
#-----------------------------------------------------------------------
	output_howto_help
	return 0
    }
#=======================================================================
    output_howto_help () { # Output how to get complete list of options
	             	   #
	             	   # Always returns a 0 status. 
	                   #
	                   # usage: output_howto_help
	             	   #
#-----------------------------------------------------------------------
echo "    -------------------------------------------------------------------" 
#-----------------------------------------------------------------------
	if [ "$oncdrom" = "0" ]; then
#-----------------------------------------------------------------------
    echo "    To review the options try 'matlabdoc -help'"
#-----------------------------------------------------------------------
        elif [ "$is9660" = "0" ]; then
#-----------------------------------------------------------------------
    echo "    To review the options try 'doc -help'"
#-----------------------------------------------------------------------
	else
#-----------------------------------------------------------------------
    echo "    To review the options try '"'DOC* -help'"'"
#-----------------------------------------------------------------------
	fi
#-----------------------------------------------------------------------
echo "    -------------------------------------------------------------------" 
#-----------------------------------------------------------------------
	return 0
    }
#=======================================================================
    process_exist () { # Determines whether the process exists or not.
                       #
		       # Returns 0 status if exists otherwise 1.
		       #
                       # usage: process_exist pid
                       #
        ps -p $1 > /dev/null 2>&1
        if [ $? -ne 0 ]; then
            ps $1 2>&1 | grep $1 > /dev/null 2>&1
            if [ $? -ne 0 ]; then
                return 1
            else
                return 0
            fi
        else
            return 0
        fi
    }
#=======================================================================
    searchpath () { # Search all the directories in your PATH for a
		    # command.
		    #
		    # Return 0 status if success or 1 if failure.
                    #
                    # usage: searchpath command
                    #
	vpath=`echo $PATH | tr ":" " "`
#
	for dir in $vpath
	do
	    (cd $dir/$1) > /dev/null 2>&1
	    if [ $? -ne 0 ]; then
	        lscmd=`ls -l $dir/$1 2>/dev/null`
	        if [ "$lscmd" ]; then
		    file=`actualpath $dir/$1`
		    if [ "$file" != "" ]; then
			if [ -x $file ]; then
		    	    return 0
			fi
		    fi
	        fi
	    fi
	done
	return 1
    }
#=======================================================================
    searchpdf () { # Search all the help directories and the return
		   # the path of the pdf file in $temp_file
                   #
		   # Returns 0 status if files(s) found otherwise 1.
		   #
                   # usage: searchpdf pdf_file
                   #
	pdf_file=$1
	rm -f $temp_file > /dev/null 2>&1
	(cd "$ROOTDIR/`to9660 help/`") > /dev/null 2>&1
	if [ $? -ne 0 ]; then
#-----------------------------------------------------------------------
echo " "
echo "    Error: Cannot find any pdf files. No MATLAB Help Desk ..."
echo "           Please install the MATLAB Help Desk from media or ftp."
echo " "
#-----------------------------------------------------------------------
	    return 1
	fi
	(cd $ROOTDIR/help
	    dirlist=`find . -type f -name '*.pdf' -print  | sort | awk '
#-----------------------------------------------------------------------
    BEGIN { newdir = 0
	    lastdir = "" }
	  { n = split($1,d,"/")
	    file = d[n]
	    l = length($1)
	    dir = substr($1,1, l - length(file) - 1)
	    if (lastdir != dir) print substr(dir,3) 
	    lastdir = dir
	  }'`
#-----------------------------------------------------------------------
         for dir in $dirlist
	 do
	    if [ -f $dir/$pdf_file.pdf ]; then
		echo help/$dir/$pdf_file.pdf > $temp_file
		return 0
	    fi
	 done)
	 if [ ! -s $temp_file ]; then
#-----------------------------------------------------------------------
echo " "
echo "    Error: pdf filename ($1) cannot be found"
echo " "
#-----------------------------------------------------------------------
	     return 1
	 fi
	 return 0
    }
#=======================================================================
    to9660 () { # Convert ufs path to ISO 9660 form if on CDROM
		# otherwise the path remains unchanged. Output the
		# result to standard output. The path must end in a /
	        # if the last item is a directory.
		#
                # Directories:   x -> upper(X)
                # Files:         x -> upper(X).;1
		#		 x.y -> Upper(X).Upper(Y);1
		#
		# We assume that all directories and files are 8.3
		# format.
	        #
		# Always returns 0 status.
		#
                # usage: to9660 path
                #
	if [ "$oncdrom" != "1" ]; then
	    echo $1
        else
	    echo "$1" | tr 'a-z' 'A-Z' | awk '
#-----------------------------------------------------------------------
	{ if (substr($1,length($1),1) == "/") 
		print $1
	  else {
	      n = split($1,a,"/")
	      if (index(a[n],".") != 0)
		  print $1 ";1"
	      else
		  print $1 ".;1"
	  }
	}'
#-----------------------------------------------------------------------
	fi
	return 0
    }
#=======================================================================
    setbrowser_env () { # Configure the browser environment if required.
	                # 
			# The following are currently known.
			#
			# 1. AIX: LANG=C; export LANG
			#         We are always assuming netscape.
	       		#
	       		# Always returns a 0 status. 
	       		#
	       		# usage: setbrowser_env
	       		#

	if [ -f /bin/uname ]; then
   	    case "`/bin/uname`" in
	        AIX*)					# ibm_rs
#
# With AIX 4.1, uname can return more than just 'AIX'
#
	            LANG=C; export LANG
	            ;;
	        *)
		    :
	            ;;
            esac
	fi
	return 0
    }
#=======================================================================
#
# Determine the MATLAB (CDROM) root directory (ROOTDIR)
#
    scriptpath
    if [ $? -ne 0 ]; then
	exit 1
    fi
#
    case "$CMDNAME" in
	'DOC.;1')
		is9660=1
	        oncdrom=1
    	        BASE_PAGE="CDOC9660.HTM;1"
		;;
	'doc')
	        is9660=0
		oncdrom=1
    	        BASE_PAGE="cdoc.htm"
		;;
	'matlabdoc')
		is9660=0
		oncdrom=0
    	        BASE_PAGE="matlabdoc.html"
		;;
    esac
#
    BROWSER_LIST='netscape Netscape Mosaic mosaic'
    ACROBAT_LIST=acroread
#
# Verify input
#
    stat="OK"
    msg=""
    ignorelock=""
    browser=""
    reader=""
    trypdf=0
    manpage=""
    name=""
    readme=""
    if [ $# -gt 3 ]; then
	stat=""
    fi
#
    while [ "$stat" = "OK" -a $# -gt 0 ]; do
	case "$1" in
	    -h|-help)		# -help: Help option.
		stat=""
		;;
	    -i)
		ignorelock=1
		;;
	    -a)
		if [ $# -eq 1 ]; then
#
# list all the pdf file in the Help Desk
#
#-----------------------------------------------------------------------
echo " "
echo "    Searching for pdf files. Please wait ..."
#-----------------------------------------------------------------------
		    listpdf
		    exit $?
		else
    		    trypdf=1
		fi
		;;
	    -man) 
		if [ $# -gt 2 ]; then
		    msg='too many arguments'
		    stat=""
		elif [ $# -eq 2 ]; then
		    shift
		    name=$1
		    manpage=1
		else
		    manpage=1
		fi
		;;
	    -ascii)
		if [ $# -gt 2 ]; then
		    msg='too many arguments'
		    stat=""
		elif [ $# -eq 2 ]; then
		    shift
		    name=$1
		    readme=1
		else
		    readme=1
		fi
		;;
	    -*)
		msg='usage'
		stat=""
		;;
	    *)
		if [ "$trypdf" = "0" ]; then
		    if [ $# -ne 1 ]; then
			msg='too many arguments'
			stat=""
		        break
		    fi
		    browser=`echo $1 | awk '
#-----------------------------------------------------------------------
	{ if (index($1,"/") || index($1,"..")) print ; else print "" }'`
#-----------------------------------------------------------------------
		    if [ "$browser" = "" ]; then
		        searchpath $1
		        if [ $? -eq 0 ]; then
			    browser=$1
		        else
#-----------------------------------------------------------------------
echo " "
echo "    Warning: browser ($1) not on Unix search path ..."
echo "             Trying one of the defaults ..."
#-----------------------------------------------------------------------
		        fi
		    elif [ ! -f $browser ]; then
			msg="browser ($1) does not exist"
			stat=""
		        break
		    fi
		else
		    if [ $# -gt 2 ]; then
			msg='too many arguments'
			stat=""
		        break
		    else
			if [ $# -eq 2 ]; then
		    	    reader=`echo $1 | awk '
#-----------------------------------------------------------------------
	{ if (index($1,"/") || index($1,"..")) print ; else print "" }'`
#-----------------------------------------------------------------------
		    	    if [ "$reader" = "" ]; then
		        	searchpath $1
		        	if [ $? -eq 0 ]; then
			    	    reader=$1
		        	else
#-----------------------------------------------------------------------
echo " "
echo "    Warning: Acrobat Reader ($1) not on Unix search path ..."
echo "             Trying one of the defaults ..."
#-----------------------------------------------------------------------
		        	fi
			    elif [ ! -f $reader ]; then
			        msg="reader ($1) does not exist"
			        stat=""
		                break
		    	    fi
			    shift
			fi
		    	file=`echo $1 | awk '
#-----------------------------------------------------------------------
	{ if (index($1,"/") || index($1,"..")) print ; else print "" }'`
#-----------------------------------------------------------------------
		        if [ "$file" != "" ]; then
			    msg="filename ($1) must be simple. No paths."
			    stat=""
		            break
			else
			    file=`expr "$1" : '\(.*\)\.pdf'` 
			    if [ "$file" = "" ]; then
			        ext=`expr "$file" : '.*\(\.[^\.]*\)$'`
				if [ "$ext" != "" ]; then 
	msg="invalid filename ($1) - use .pdf extension or none at all"
			    	    stat=""
		                    break
				else
				    file=$1
			        fi
			    fi
#-----------------------------------------------------------------------
echo " "
echo "    Searching for pdf file. Please wait ..."
#-----------------------------------------------------------------------
			    searchpdf $file
			    if [ $? -ne 0 ]; then 
				output_howto_help
				exit 1
			    else
				PDF_FILE=`cat $temp_file`
				rm -f $temp_file
			    fi
			fi
		    fi
		fi
	        ;;
	esac
	shift
    done
#
    if [ "$stat" != "" -a "$trypdf" = "0" -a "$browser" = "" -a \
         "$manpage" = "" -a "$readme" = "" ]; then
#
# Try locating one of the default browsers
#
        found=0
        for name in $BROWSER_LIST
	do
            searchpath $name
	    if [ $? -eq 0 ]; then
	        browser=$name
#-----------------------------------------------------------------------
echo " "
echo "    Found default browser ($browser) ..."
#-----------------------------------------------------------------------
		found=1
		break
	    fi
	done
	if [ "$found" = "0" ]; then
	    msg="no default browser on Unix search path"
	    stat=""
	fi
    fi
#
    if [ "$stat" != "" -a "$trypdf" != "0" -a "$reader" = "" -a \
	 "$manpage" = "" -a "$readme" = "" ]; then
#
# Try locating one of the default readers
#
        found=0
        for name in $ACROBAT_LIST
	do
            searchpath $name
	    if [ $? -eq 0 ]; then
	        reader=$name
#-----------------------------------------------------------------------
echo " "
echo "    Found default Acrobat Reader ($reader) ..."
#-----------------------------------------------------------------------
		found=1
		break
	    fi
	done
	if [ "$found" = "0" ]; then
	    msg="no default Acrobat Reader on Unix search path"
	    stat=""
	fi
    fi
#
    if [ "$stat" = "" ]; then
	if [ "$msg" != "" ]; then
#-----------------------------------------------------------------------
    echo " "
    echo "    Error: $msg"
    echo " "
#-----------------------------------------------------------------------
	fi
#-----------------------------------------------------------------------
(
	if [ "$oncdrom" != "1" ]; then
echo "-----------------------------------------------------------------"
echo " "
echo "    Usage: matlabdoc  [-h|-help] | [-i] [browser] |"
echo "                                   -a [[reader] pdf_file] |"
echo "                                   -man [name] |"
echo "                                   -ascii [name]"
echo " "
echo "    -h|-help          - Help."
echo " "
echo "    -i                - (Netscape browser only) Always bring up"
echo "                        a new copy. Ignore the lock to an"
echo "                        existing Netscape browser."
echo " "
echo "    browser           - Path of WEB browser. It will search your"
echo "                        path if a simple name is given."
echo "                        DEFAULT search list: netscape, Netscape,"
echo "                                             Mosaic, mosaic"                   
echo " "
echo "    -a                - (alone) Print a list of the pdf documents"
echo "                        available in the MATLAB Online"
echo "                        Documentation."
echo " "
echo "    reader            - Path to Adobe Acrobat Reader. It will"
echo "                        search your path if a simple name is"
echo "                        given."
echo "                        DEFAULT search list: acroread" 
echo " "
echo "    pdf_file          - Name of pdf file (without the path or"
echo "                        extension). It should be on the list"
echo "                        gotten by executing the command with the"
echo "                        -a option alone."
echo " "
echo "    -man              - List and view manual pages using 'more'."
echo " "
echo "    -ascii            - List and view ASCII README files using" 
echo "                        'more'."
echo " "
echo "    name              - Start with this particular manual page"
echo "                        or ASCII README file."
echo " "
echo "    Either startup a WEB browser at the base page for accessing"
echo "    MATLAB Online Documentation or open a file in the Adobe"
echo "    Acrobat Reader or view man pages using 'more' or view"
echo "    README files using 'more'. The WEB browser base page is:"
echo " "
echo '                     $MATLAB/startdoc/matlabdoc.html'
echo " "
echo "    where"
echo " "
echo "        MATLAB = $ROOTDIR"
echo " "
echo "-----------------------------------------------------------------" 
	else
echo "-----------------------------------------------------------------"
echo " "
echo "    Usage: doc  [-h|-help] | [-i] [browser] |"
echo "                             -man [name] |"
echo "                             -ascii [name]"
echo " "
echo "    -h|-help          - Help."
echo " "
echo "    -i                - (Netscape browser only) Always bring up"
echo "                        a new copy. Ignore the lock to an"
echo "                        existing Netscape browser."
echo " "
echo "    browser           - Path of WEB browser. It will search your"
echo "                        path if a simple name is given."
echo "                        DEFAULT search list: netscape, Netscape,"
echo "                                             Mosaic, mosaic"                   
echo " "
echo "    -man              - List and view manual pages using 'more'."
echo " "
echo "    -ascii            - List and view ASCII README files using" 
echo "                        'more'."
echo " "
echo "    name              - Start with this particular manual page"
echo "                        or ASCII README file."
echo " "
echo "    Either startup a WEB browser at the base page for accessing"
echo "    MATLAB Online Documentation or view man pages using 'more'"
echo "    or view README files using 'more'."
echo " "
echo "-----------------------------------------------------------------" 
	fi
) | more
#-----------------------------------------------------------------------
	exit 1
    fi
#
    cd "$ROOTDIR"
#
# Browser case
#
    if [ "$trypdf" = "0" -a "$manpage" = "" -a "$readme" = "" ]; then
#
# Check for base page
#
        if [ ! -f "$BASE_PAGE" ]; then
#-----------------------------------------------------------------------
    echo " "
    echo "    Error: base page ($BASE_PAGE) cannot be found relative to:"
    echo " "
    echo "           $ROOTDIR"
    echo " "
#-----------------------------------------------------------------------
	    output_howto_help
	    exit 1
        fi
#
	browser_base=`echo "$browser" | tr '/' ' ' | awk '{print $NF}'`
	if [ "$browser_base" = "netscape" -o "$browser_base" = "Netscape" ]; then
            lmcmd=`ls -ld $HOME/.netscape/lock 2>/dev/null` 
	    if [ "$lmcmd" != "" -a "$ignorelock" = "" ]; then
#-----------------------------------------------------------------------
echo " "
echo "    Connecting to existing browser. Please wait ..."
#-----------------------------------------------------------------------
		setbrowser_env
        	$browser "-remote" "openURL(file:$ROOTDIR/$BASE_PAGE)" 2>/dev/null &
#-----------------------------------------------------------------------
echo " "
echo "    Note: if base page does not appear then try 'matlabdoc -i' ..."
echo " "
#-----------------------------------------------------------------------
	    else
#-----------------------------------------------------------------------
echo " "
echo "    Starting up browser. Please wait for new window to appear ..."
#-----------------------------------------------------------------------
		setbrowser_env
	        $browser "$BASE_PAGE" 2>/dev/null &
		process=$!
		sleep 5
		process_exist $process
		if [ $? -ne 0 ]; then
#-----------------------------------------------------------------------
echo " "
echo "    Error: browser $browser failed to start ..."
echo " "
#-----------------------------------------------------------------------
		    $browser "$BASE_PAGE"
#-----------------------------------------------------------------------
echo " "
#-----------------------------------------------------------------------
		    output_howto_help
	            exit 1
		fi
	   fi
	else
#-----------------------------------------------------------------------
echo " "
echo "    Starting up browser. Please wait for new window to appear ..."
#-----------------------------------------------------------------------
	    setbrowser_env
	    $browser "$BASE_PAGE" 2>/dev/null &
	    process=$!
	    sleep 5
	    process_exist $process
	    if [ $? -ne 0 ]; then
#-----------------------------------------------------------------------
echo " "
echo "    Error: browser $browser failed to start ..."
echo " "
#-----------------------------------------------------------------------
		$browser "$BASE_PAGE"
#-----------------------------------------------------------------------
echo " "
#-----------------------------------------------------------------------
		output_howto_help
	        exit 1
	    fi
	fi
    elif [ "$manpage" = ""  -a "$readme" = "" ]; then
#
# Acrobat Reader case
#
#-----------------------------------------------------------------------
echo " "
echo "    Starting up Acrobat Reader. Please wait for new window to appear ..."
#-----------------------------------------------------------------------
	$reader $PDF_FILE 2>/dev/null &
	process=$!
	sleep 5
	process_exist $process
	if [ $? -ne 0 ]; then
#-----------------------------------------------------------------------
echo " "
echo "    Error: Acrobat Reader $reader failed to start ..."
echo " "
#-----------------------------------------------------------------------
	    $reader $PDF_FILE
	    output_howto_help
	    exit 1
	fi
    elif [ "$manpage" != "" ]; then
        MANDIR=$temp_file
	bld_man_dir $MANDIR
	if [ $? -ne 0 ]; then
	    rm -rf $MANDIR > /dev/null 2>&1
	    output_howto_help
	    exit 1
	fi
#
	n=0
	while true
	do
	    n=`expr $n + 1`
	    if [ $n -gt 1 ]; then
		name=""
	    fi
	    listman $MANDIR $name
#-----------------------------------------------------------------------
echon '    Man page name to view [Hit return alone to quit] > '
#-----------------------------------------------------------------------
            read ans
	    if [ "$ans" = "" ]; then
#-----------------------------------------------------------------------
echo " "
#-----------------------------------------------------------------------
        	rm -rf $MANDIR > /dev/null 2>&1
		output_howto_help
	        exit 0
	    fi
	    files=`(cd $MANDIR; ls $ans* 2>/dev/null)`
	    if [ "$files" != "" ]; then
		clearsc
	        (cd $MANDIR; more $files)
	    else
#-----------------------------------------------------------------------
echo "-----------------------------------------------------------------------" 
echo " "
echo "    Error: Manual page(s) $ans"'*'" not found . . ."
#-----------------------------------------------------------------------
	    fi
#-----------------------------------------------------------------------
echo " "
#-----------------------------------------------------------------------
	done
#=========================
    else
#
# Query loop for ASCII README files.
#
        READMEDIR=$temp_file
	bld_readme_dir $READMEDIR
	if [ $? -ne 0 ]; then
	    rm -rf $READMEDIR > /dev/null 2>&1
	    output_howto_help
	    exit 1
	fi
#
	n=0
	while true
	do
	    n=`expr $n + 1`
	    if [ $n -gt 1 ]; then
		name=""
	    fi
	    listreadme $READMEDIR $name
#-----------------------------------------------------------------------
echon '    README file to view [Hit return alone to quit] > '
#-----------------------------------------------------------------------
            read ans
	    if [ "$ans" = "" ]; then
#-----------------------------------------------------------------------
echo " "
#-----------------------------------------------------------------------
        	rm -rf $READMEDIR > /dev/null 2>&1
		output_howto_help
	        exit 0
	    fi
	    files=`(cd $READMEDIR; ls $ans* 2>/dev/null)`
	    if [ "$files" != "" ]; then
		clearsc
	        (cd $READMEDIR; more $files)
	    else
#-----------------------------------------------------------------------
echo "-----------------------------------------------------------------------" 
echo " "
echo "    Error: README file(s) $ans"'*'" not found . . ."
#-----------------------------------------------------------------------
	    fi
#-----------------------------------------------------------------------
echo " "
#-----------------------------------------------------------------------
	done
    fi
#-----------------------------------------------------------------------
echo " "
#-----------------------------------------------------------------------
    output_howto_help
    exit
