#
# usage:        bld_help.sh
#
# abstract:     This Bourne Shell script installs help related files
#		and links.
#
# note(s):      1. This routine must be called using a . (period)
#
#		2. Always change
#
#		   MATLAB:
#
#		   a. Link $MATLAB/bin/matlabdoc -> $MATLAB/matlabdoc
#		   b. Configure invisibly the startdoc/matlabdoc.html file
#		   c. Configure invisibly the Online Help
#		   d. Configure invisibly the Japanese Online Help
#
# Copyright (c) 1998 by The MathWorks, Inc.
# $Revision: 1.1 $  $Date: 1998/06/10 14:55:42 $
#----------------------------------------------------------------------------
#
#//////////////////////////////////////////////////////////////////////////
#
# Link $MATLAB/bin/matlabdoc -> $MATLAB/matlabdoc if bin directory exists
#
    if [ -f $ML_ROOT_DIR/matlabdoc -a -d $ML_ROOT_DIR/bin ]; then
	(cd $ML_ROOT_DIR/bin
	 rm -f matlabdoc
	 ln -s ../matlabdoc matlabdoc)
    fi
#//////////////////////////////////////////////////////////////////////////
#
# Configure matlabdoc.html. Start with startdoc/template/matlabdoc.html
#                           and create startdoc/matlabdoc.html
# Search for: (starting in column 1)
# nohelp <A ...  - No 	help desk 
# help   <A ...  - English   help desk
# jhelp  <A ...  - Japanese  help desk
#
    if [ -d $ML_ROOT_DIR/startdoc ]; then
        helpdesks=''
        if [ -d $ML_ROOT_DIR/help ]; then
	    helpdesks="$helpdesks help"
        fi
        if [ -d $ML_ROOT_DIR/jhelp ]; then
	    helpdesks="$helpdesks jhelp"
        fi
        if [ "$helpdesks" = "" ]; then
	    helpdesks="$helpdesks nohelp"
        fi
#
        (cd $ML_ROOT_DIR/startdoc
         if [ -f matlabdoc.html ]; then
	     rm -f matlabdoc.html
         fi
         echo $helpdesks | cat - template/matlabdoc.html | awk '
#-----------------------------------------------------------------------
    NR == 1 { n = split($0,a); next }
            { if ($2 == "<A") {
	          for (i = 1; i <= n; i = i + 1)
		    if (a[i] == $1) {
			ix = index($0,$2)
			print substr($0,ix)
			break
		    }
	      }
	      else
	          print}' > matlabdoc.html
#-----------------------------------------------------------------------
         chmod 644 matlabdoc.html)
#
# Link $MATLAB/matlabdoc.html -> $MATLAB/startdoc/matlabdoc.html
#
	(cd $ML_ROOT_DIR
         rm -f matlabdoc.html
         ln -s startdoc/matlabdoc.html matlabdoc.html)
    fi

#//////////////////////////////////////////////////////////////////////////
#
# Configure Online Help files (English)
#
#     sentinel line:  '// end docinstl -->'
#
    if [ -d $ML_ROOT_DIR/help ]; then
	if [ -f $ML_ROOT_DIR/help/template/helpdesk.html ]; then
	   (cd $ML_ROOT_DIR/help
	    if [ -f helpdesk.html ]; then
	        rm -f helpdesk.html
	    fi
	    cat template/helpdesk.html | awk '
#-----------------------------------------------------------------------
    substr($1,1,2) == "//" { s = ""; for (i = 1; i <= NF; i = i + 1) s = s $i
			     if (s == "//enddocinstl-->") exit }
                { print }' > $temp_file
#-----------------------------------------------------------------------
	    cat template/helpdesk.html | awk '
#-----------------------------------------------------------------------
    BEGIN { state = 0 }
    substr($1,1,2) == "//" { s = ""; for (i = 1; i <= NF; i = i + 1) s = s $i
			     if (s == "//enddocinstl-->") state = 1 }
                { if (state == 0)
                      next
                  else
                      print }' > $temp_file2
#-----------------------------------------------------------------------
	    cat $temp_file install/html/* $temp_file2 > helpdesk.html
	    chmod 644 helpdesk.html)
	fi
#
        if [ -f $ML_ROOT_DIR/help/template/fulldocset.html ]; then
	   (cd $ML_ROOT_DIR/help
	    if [ -f fulldocset.html ]; then
	        rm -f fulldocset.html
	    fi
	    cat template/fulldocset.html | awk '
#-----------------------------------------------------------------------
    substr($1,1,2) == "//" { s = ""; for (i = 1; i <= NF; i = i + 1) s = s $i
			     if (s == "//enddocinstl-->") exit }
                { print }' > $temp_file
#-----------------------------------------------------------------------
	    cat template/fulldocset.html | awk '
#-----------------------------------------------------------------------
    BEGIN { state = 0 }
    substr($1,1,2) == "//" { s = ""; for (i = 1; i <= NF; i = i + 1) s = s $i
			     if (s == "//enddocinstl-->") state = 1 }
                { if (state == 0)
                      next
                  else
                      print }' > $temp_file2
#-----------------------------------------------------------------------
	    cat $temp_file install/pdf/* $temp_file2 > fulldocset.html
	    chmod 644 fulldocset.html)
	fi
#
# For standalone 
#
        if [ -f $ML_ROOT_DIR/help/template/mathlib.html ]; then
	   (cd $ML_ROOT_DIR/help
	    if [ -f mathlib.html ]; then
	        rm -f mathlib.html
	    fi
	    cat template/mathlib.html | awk '
#-----------------------------------------------------------------------
    substr($1,1,2) == "//" { s = ""; for (i = 1; i <= NF; i = i + 1) s = s $i
			     if (s == "//enddocinstl-->") exit }
                { print }' > $temp_file
#-----------------------------------------------------------------------
	    cat template/mathlib.html | awk '
#-----------------------------------------------------------------------
    BEGIN { state = 0 }
    substr($1,1,2) == "//" { s = ""; for (i = 1; i <= NF; i = i + 1) s = s $i
			     if (s == "//enddocinstl-->") state = 1 }
                { if (state == 0)
                      next
                  else
                      print }' > $temp_file3
#-----------------------------------------------------------------------
	    cat install/html/* install/pdf/* 2>/dev/null | sort | \
		uniq | egrep '(mathlib|cppmath)' > $temp_file2
#-----------------------------------------------------------------------
	    cat $temp_file $temp_file2 $temp_file3 > mathlib.html
	    chmod 644 mathlib.html)
	fi
    fi
#
#//////////////////////////////////////////////////////////////////////////
#
# Configure Online Help files (Japanese)
#
#     sentinel line:  '// end docinstl -->'
#
    if [ -d $ML_ROOT_DIR/jhelp ]; then
	if [ -f $ML_ROOT_DIR/jhelp/template/helpdesk.html ]; then
	   (cd $ML_ROOT_DIR/jhelp
	    if [ -f helpdesk.html ]; then
	        rm -f helpdesk.html
	    fi
	    cat template/helpdesk.html | awk '
#-----------------------------------------------------------------------
    substr($1,1,2) == "//" { s = ""; for (i = 1; i <= NF; i = i + 1) s = s $i
			     if (s == "//enddocinstl-->") exit }
                { print }' > $temp_file
#-----------------------------------------------------------------------
	    cat template/helpdesk.html | awk '
#-----------------------------------------------------------------------
    BEGIN { state = 0 }
    substr($1,1,2) == "//" { s = ""; for (i = 1; i <= NF; i = i + 1) s = s $i
			     if (s == "//enddocinstl-->") state = 1 }
                { if (state == 0)
                      next
                  else
                      print }' > $temp_file2
#-----------------------------------------------------------------------
	    cat $temp_file install/html/* $temp_file2 > helpdesk.html
	    chmod 644 helpdesk.html)
	fi
#
# Currently no support for pdf files in Japanese on UNIX
#
# Currently no support for standalone in Japanese
#
    fi
#//////////////////////////////////////////////////////////////////////////
#
    rm -f $temp_file $temp_file2 $temp_file3
