#!/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.

# 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

case $# in
	0) echo "Usage: $prog dest_directory [file1.d [file2.d ...]]"; exit 1 ;;
	1) exit 0 ;;	# macro HFILES in Smakefile is empty
	*) DEST=$1; shift; HFILES=$* ;;
esac

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

if [ `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

START=`pwd`
cd $DEST
for file in $HFILES
do
	# read all the .FN's but those with /'s; to protect against filename 
	# expansion (.FN'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: | ^ & $ < > ( ) { } : ; " ` \.
	help_files=`awk '$1==".FN" {if($2!="/"&&$2!="%/%"){print $2}}' \
		$START/$file | sed 's/[][*?|^&$<>(){}:;"\`\\\]/\\\&/g'`
	rm -f $file
	cp $START/$file .
	for help_file in $help_files
	do
		# break flag list with 'dummy' in case some of $help_file
		# start with '-':
		eval "rm -f dummy $help_file
		      ln $file $help_file
		      echo $prog: installing $help_file"
	done
	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-/${HELP_DIR}\\$--\"`
"
