#!/bin/sh
# @(#) $RCSfile: BUILD_JHELP,v $: $Revision: #18 $, $Date: 2008/09/30 $ 
# @(#) Copyright (c), 1987, 2006 Insightful Corp.  All rights reserved.
##  This script creates a .Data/__Jhelp directory which contains JavaHelp
##  helpset files.
##  This script must be executed from an S-PLUS Chapter which
##  contains a .Data directory and a .Data/__Hhelp directory.
##  The HTML help files must be in .Data/__Hhelp directory.

usage (){
echo "Usage: $0 [-nogifs] [-nosearch] [-keywords keyword-file] [libname]"
echo "   -nogifs    Do not make a gifs directory. By default a gifs directory"
echo "              is created in __Jhelp and required gif files are copied"
echo "              into it.  This option should only be used for builtin"
echo "              S-PLUS libraries, i.e. those in the SHOME/library directory."
echo "   -nosearch  Do not create the files required for full-text search."
echo "   -keywords  keyword-file  Use the topic keywords from this file; "
echo "                            otherwise use the S-PLUS keywords file"
echo "                            which is SHOME/splus/lib/X11/keywords."
echo "   libname    The name to use when creating the helpset files. By"
echo "              default the basename of the current directory is used,"
echo "              except when the current directory is named man and ../DESCRIPTION"
echo "              exists (then the name on the Package: line of ../DESCRIPTION"
echo "              will be used)."
exit 1
}

DESTPATH=.
# this is path to html files from __Jhelp directory
TO_HTML=../__Hhelp
# default values
GIFS=gifs
LIBNAME=`basename \`pwd\``
if [ "$LIBNAME" = "man"  -a -r ../DESCRIPTION ] ; then
	tmp=`grep '^PackageSplus:' ../DESCRIPTION | sed -e 's/^PackageSplus://' -e 's/[ 	]*//g'`
	[ "$tmp" = "" ] && tmp=`grep '^Package:' ../DESCRIPTION | sed -e 's/^Package://' -e 's/[ 	]*//g'`
        if [ "$tmp" != "" ] ; then
		LIBNAME=$tmp
	fi
fi
KEYWORDS=${SHOME}/splus/lib/X11/keywords

while [ $# -gt 0 ]
do
	case $1 in
		-nogifs)  
			GIFS=../../../../splus/lib/javahelp/gifs
			shift;;
		-keywords|-key)  KEYWORDS=$2; shift; shift;;
		-nosearch)  NOSEARCH=$1; shift;;
		-h|-help)  usage;;
		-*)  echo "Unknown option $1"; usage;;
		*)  LIBNAME=$1; shift; break;;
	esac
done
if [ $# != 0 ]; then echo "Invalid args: $*"; usage; fi

if [ ${SHOME}X = X ] ;then
    echo "SHOME is not set"
    exit 1
fi
if  test ! -d .Data; then
    echo "You must run from an S-PLUS chapter which has a .Data dir."
    exit 1
fi
if  test ! -d .Data/__Hhelp; then
    echo "You must run from an S-PLUS chapter which has a .Data/__Hhelp dir."
    exit 1
fi

cd .Data
if test  -d __Jhelp; then rm -rf __Jhelp; fi
mkdir __Jhelp
cd __Jhelp

if [ ${GIFS} != gifs ] ; then
	if test ! -r ${GIFS}/jhgif_toplevel.gif
	then GIFS=gifs
	fi
fi
if [ ${GIFS} = gifs -a -r $SHOME/splus/lib/javahelp/gifs/jhgif_toplevel.gif ] ; then
	echo "$0 $LIBNAME: Creating gifs directory."
	mkdir gifs
	cp $SHOME/splus/lib/javahelp/gifs/jhgif_toplevel.gif gifs &&
            chmod u+w gifs/jhgif_toplevel.gif
fi

${SHOME}/cmd/html2jhelp ${LIBNAME} ${TO_HTML} \
    -jh ${DESTPATH}/ ${LIBNAME} -gif ${GIFS} -kw ${KEYWORDS} ${NOSEARCH}

#build JavaHelp search files
if [ X${NOSEARCH} = X -a -x ${SHOME}/java/jh/javahelp/bin/jhindexer ] ; then
    # set path to JavaHelp home and path to jhindexer
    PATHEXE=${SHOME}/java/jh/javahelp/bin
    PATH="$SHOME"/java/jre/bin:$PATH; export PATH
    echo "$0: Creating Search info -- running jhindexer"
    $PATHEXE/jhindexer ${TO_HTML} -db ${DESTPATH}/${LIBNAME}HelpSearch
fi
