#!/bin/sh

# @(#)HINSTALL version 3.15 created 12/20/94
# @(#)Copyright (c), 1987, 1994 StatSci, Inc.  All rights reserved.

# HINSTALL: install helpfiles.
# This file will copy all files referred to from $2 onwards into directory $1.

# Pre-process (sgml) help files and store them in the directory __Hhelp.
# __Hhelp must exist in the same directory as the __Shelp directory.
# If you only supply the object_directory, all help files in its __Shelp
# directory will be processed with sgml2html.
# You can also provide a list of help files
# after the object_directory and only those help files will be processed with
# sgml2html.

# If the environment variable SYSGEN_UPDATE is set, and if the file
# $SHOME/touches/sysgen exists, then only those HFILES ($2 onwards) which
# are newer than $SHOME/touches/sysgen are installed.

prog=HINSTALL
LINK="FALSE"

case $# in
	0) echo "Usage: $prog <-link> dest_directory [file1.sgml [file2.sgml ...]]"; exit 1 ;;
	1) exit 0 ;;	# macro HFILES in Smakefile is empty
	*) if [ "x$1" = "x-link" ]; then
                LINK="TRUE"
                shift
           fi
           DEST=$1; shift; HFILES=$* ;;
esac


: ${SHOME?"must be set for $prog"}


if [ `$SHOME/cmd/PLATFORM` = HP700 ]; then
 set -f  # kludge because hp expands asterisks if directory not empty 
fi

if [ ! -d $DEST ]; then echo $prog: no directory $DEST >&2; exit 1; fi

for i in $HFILES
do
        [ -r $i ] || { echo "$prog : can't read $i" >&2 ; exit 1 ; }
done

# If this is a sysgen-update, install only hfiles which changed since last time.
if test x$SYSGEN_UPDATE != x && test -r $SHOME/touches/sysgen; then
	HFILES=`find $HFILES -newer $SHOME/touches/sysgen -print`
	if test x"$HFILES" = x; then exit 0; fi
fi
EXT='.sgm'
START=`pwd`
echo $START
cd $DEST
if [ ! -d __Shelp ]
then 
    echo "Making __Shelp directory."
    mkdir __Shelp
fi
if [ ! -d __Hhelp ]
then
    echo "Making __Hhelp directory."
    mkdir __Hhelp && chmod a+w __Hhelp
fi
cd __Shelp
echo `pwd`


for file in $HFILES
do
     # read all the topics's but those with /'s; to protect against filename
     # expansion (topics's may contain * etc.), backquote [ ] * ?
     # and use eval later on to get rid of the \. Use of eval then means
     # we have to \ characters with other special meaning, like ^. So
     # backquote these too: | ^ & $ < > ( ) { } : ; " ` \.
     # Finally, remove DOS end-of-line characters (control-M) so they
     # don't show up in file names (depending what editor you use to view
     # this file, they may show up as new-lines).
     isclassfile=`grep -c \<s-class-doc\>\<s-topics\> $START/${file}`
     if test "$isclassfile" != "0"
     then
         PREFIX="class."
     else
         PREFIX=""
     fi
	 if [ "x${LINK}" = "xFALSE" ]; then
             help_file=`sed -e '/<s-topic>/q' $START/$file  | grep \<s-topic\> | sed -e 's/<s-topic>//' -e 's/<\/s-topic>//' -e 's/^[ ]*//' -e 's/[ ]*$//' -e  's/[][*?|^&$<>(){}:;"\`\\\]/\\\&/g' -e 's/
$//'`
	 else
             help_files=`grep \<s-topic\> $START/$file | sed -e 's/<s-topic>//' -e 's/<\/s-topic>//' -e  's/[][*?|^&$<>(){}:;"\`\\\]/\\\&/g' -e 's/
$//'`
         fi

	 if [ -r ${file} ]; then
		rm -f ${file}
	 fi
	 cp $START/${file} ./${file} 
         if [ "x${LINK}" = "xFALSE" ]; then
#               eval "if [ -r ${help_file}${EXT} ]; then
#                      rm -f dummy ${help_file}${EXT}
#                      fi"
                sgmname=${PREFIX}${help_file}${EXT} 
                eval " if [ -r ${sgmname} ]; then
                          rm -f ${sgmname}
                       fi
                       cp ${file} ${sgmname}
                       echo $prog: processing ${DEST}/__Shelp/${sgmname}
                       $SHOME/cmd/makehtml ${sgmname} ../__Hhelp > /dev/null "
        else
	  for help_file in $help_files
	  do
		# break flag list with 'dummy' in case some of $help_file
		# start with '-':
		# use ./$help_file because some help directories use meta
		# characters.  For instance the help file for the ~ operator.
#		eval "rm -f dummy $help_file$EXT"
		eval " if [ -r ./$PREFIX$help_file$EXT ]; then
			rm -f ./$PREFIX$help_file$EXT
		fi
		      echo $prog: installing $DEST/$PREFIX$help_file$EXT
		      ln  $file ./$PREFIX$help_file$EXT
		      $SHOME/cmd/makehtml ./$PREFIX$help_file$EXT $DEST/__Hhelp > /dev/null "
	  done
        fi
        rm -f $file

done

#. $SHOME/adm/cmd/DIRNAMES
#echo "
#The .Help.find.sum file in $DEST may now be out of date.
#You can remake it with:
#
#	Splus help.findsum `echo $DEST | sed \"s-/${SGML_HELP_DIR}\\$--\"`
#"

