#!/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=RDINSTALL
LINK="FALSE"

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


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


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

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

EXT='.sgm'
START=`pwd`
cd $DEST
if [ ! -d __Hhelp ]
then
    mkdir __Hhelp && chmod a+w __Hhelp
fi
cd __Shelp

for file in $RDFILES
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
     # help_file should be value in first uncommented \alias{value} in file.
     # grep and sed are not sufficient to parse this, so we assume \alias essentially starts the line
     # and that we don't have \alias{\}} (which would allow help("}")).
     # Old version mapped '% \alias{foo} % comment' to '% foo % comment'.
     # old: help_file=`sed -e '/\\\alias{/q' $START/${file} | grep '\\alias{' | sed -e 's#\\\alias{##' -e 's/ *}//' -e  's/[][*?|^&$<>(){}:;"\`\\\]/\\\&/g' -e 's/
$//'`
     help_file=`sed -e '/^[ \t]*\\\alias{/q' $START/${file} |
         grep '^[ \t]*\\alias{' |
         sed -e 's#^[ \t]*\\\alias{##' -e 's/ *}.*//' -e  's/[][*?|^&$<>(){}:;"\`\\\]/\\\&/g' -e 's/
$//'`

     if [ -r ${file} ]; then
		rm -f ${file}
     fi
     cp $START/${file} ./${file} 
     sgmname=${PREFIX}${help_file}${EXT} 
     pkgutils=`find_pkgutils`
     if [ "$pkgutils" = "" ] ; then
        echo 1>&2 "$0: pkgutils library is missing - see help(install.pkgutils)"
        exit 1
     fi
     eval " if [ -r ${sgmname} ]; then
                  rm -f ${sgmname}
            fi
            # cp ${file} ${sgmname}
            echo $prog: processing ${DEST}/__Shelp/${sgmname}
            ${SHOME}/cmd/Sperl \"$pkgutils/share/perl/Rdconv\" --type=Ssgm ${file} > ${sgmname}
            ${SHOME}/cmd/makehtml ${sgmname} ../__Hhelp > /dev/null "
      rm -f $file
done
