#!/bin/sh
#
# ${RHOME}/etc/install-help
# Usage:  install-help [pkg] [lib]
# Install all help files, INDEX and TITLE for package pkg in library lib
# (defaults are ${RHOME}/src/library/base' and `${RHOME}', respectively).

# FIXME when the new directory structure is here!

RHOME=`dirname $0`/..		# relative
RHOME=`cd ${RHOME}; pwd`	# absolute

if [ "$1" ]
then
    pkg=$1
    test -d ${pkg} || ( echo "Package \`${pkg}' does not exist"; exit 1 )    
else
    pkg="${RHOME}/src/library/base"
fi
pkgname=`basename ${pkg}`

if [ "$2" ]
then
    mkdir -p $2 || ( echo "Cannot write to \`$2'"; exit 1 )
    lib=`cd $2; pwd`
else
    lib=${RHOME}
fi    

cd ${pkg}
pkg=`basename ${pkg}`

DEST=${lib}/help/${pkg}
mkdir -p ${DEST} || ( echo "Cannot write to \`${DEST}'"; exit 1 )
cp /dev/null ${DEST}/AnIndex

FILES="`ls -A man | sed '/CVS/d;/~$/d;/\.bak$/d;/\.orig$/d'`"

echo -n " >>> Building help index for package \`${pkg}' ..."
(
for file in ${FILES}
do
    NAMES=`grep '^ALIAS(' man/${file} | sed 's/ALIAS(\(.*\))$/\1/; s/[\*]/\\\&/g'`
    for name in ${NAMES}
    do
	echo "$name	$file"
	#	   ^^^^^ = TAB
    done
    # (echo "${file}:	${NAMES}" | tr '\012' ','; echo '') | sed 's/,$//' >&2
done
if [ ${pkg} != "base" ]
## The following is NOT done anymore in "base":
## 1) Each help file should have  ALIAS(..) for ALL its objects
## 2) The help file name may differ from _ANY_ object (eg `Special')
then
    for file in ${FILES}
    do
	echo "${file}	${file}"
    done
fi
) | sort -d -f | uniq >> ${DEST}/AnIndex
echo " done."

echo " >>> Building help pages for package \`${pkg}'"
for file in ${FILES}
do
    ${RHOME}/etc/doc2ms man/${file} \
	| nroff -ms 2> /dev/null \
	| ${RHOME}/etc/help.pretty \
	| sed 's/^/	/' > ${DEST}/${file}
    echo "  ${file}"
done
if [ -f INDEX ]
then
    cp INDEX ${DEST}/INDEX
fi
if [ -f TITLE ]
then
    cp TITLE ${DEST}/TITLE
fi

(cd ${lib}/help; cat */TITLE > LibIndex 2> /dev/null)
