#!/bin/sh

# @(#) $RCSfile$: $Revision: #8 $, $Date: 2001/08/29 $
# @(#) Copyright (c), 1987, 2000 StatSci, Inc.  All rights reserved.

# Syntax: METAPREP destdir files ...

# This file takes a list of 'files' containing S expressions which create
# objects on search position 1, and the name of a directory to install the
# objects in.  It prepares a file of Splus commands and gives that to Splus;
# the files are read in to S from stdin and the objects assigned under
# position 1 ($swork1, a temporarily created directory). S is then
# restarted, with $swork1 now mounted at the end of the search list, and a
# new scratch directory $swork2 at the head of the search list. The objects
# are assigned from $swork1 to "destdir" with assign(). (An explanation for
# all this juggling is given in a comment later on.) The exit status of
# this script is nonzero if all went well. The error option in S is set to
# make S exit 1 on any kind of error [which triggers evaluation of the error 
# option]. There are also places where we tell S to q(1) explicitly. In
# either case, QINSTALL catches a nonzero exit from S, does cleanup, and exits 
# 1.

# 'files' typically contain '<-' type assignments, though they may contain
# those of type 'data.restore("dumpfile")' as well. Anything that creates 
# the objects on position 1 should work.

# If the environment variable SYSGEN_UPDATE is set, and if the file
# $SHOME/touches/sysgen exists, then only those QFILES ($2 onwards) which 
# are newer than $SHOME/touches/sysgen are installed. 
echo "entering program"
prog=$SHOME/cmd/METAPREP

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

	SEP=";"
PATH="$SHOME/cmd${SEP}$PATH"
export PATH
echo "path exported"
. $SHOME/cmd/DIRNAMES
INITFILE=.First
tmpdir=${S_TMP:-$SHOME/tmp}
swork1=$tmpdir/1_$$_Data
swork2=$tmpdir/2_$$_Data
S_CMDS_FILE=commands.q
echo "variables set"
case $# in
	0) echo "Usage: $prog dest_directory files ..." >&2
	   exit 1
	   ;;
	1) exit 0 	# macro QFILES in Smakefile is empty.
	   ;;
	*) DEST=$1
	   shift
	   QFILES=$*
	   ;;
esac
echo "checked argument list"
echo $DEST
export DEST
[ -d $DEST ] || { echo "$prog : no directory $DEST" >&2 ; exit 1 ; }
[ -w $DEST ] || { echo "$prog : no write permission for $DEST" >&2 ; exit 1 ; }

# Check first that we can read all files.

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

# If this is a sysgen-update, install only qfiles which changed since last time.
#if test x$SYSGEN_UPDATE != x && test -r $SHOME/touches/sysgen; then	
#	QFILES=`find $QFILES -newer $SHOME/touches/sysgen -print`
#	if test x"$QFILES" = x; then exit 0; fi
#fi

# If anything goes wrong, clean up, print a message, and exit 1. User can 
# also ^C the process to get the same result.

sigs="0 2"	# any exit, SIGINT (^C)

trap 'rm -rf $S_CMDS_FILE $swork1 $swork2 ;
	echo "$prog : aborted" >&2 ;
	exit 1' $sigs

rm -rf $S_CMDS_FILE $swork1 $swork2
#mkdir swork1 swork2
#S_WORK=swork1

S_DEFAULT_PATH="$SHOME/library/splus/.Data${SEP}$SHOME/library/models/.Data${SEP}$SHOME/library/main/.Data"

#export S_WORK  
SCMD="$SHOME/cmd/Splus EXEC Sqpe"
[ -r $SHOME/cmd/dSqpe ] && SCMD="$SHOME/cmd/Splus EXEC dSqpe"
$SHOME/cmd/Splus CHAPTER
# Create the objects on $swork1 in a separate invocation of S. Besides keeping 
# memory usage down, this ensures that there are no syntax errors in the 
# assignment expressions before we start writing to the system directory.

S_FIRST='options(conflicts.ok=T, error=function() { cat("S exiting because of error.\n") ; q(1) })'
export S_FIRST
echo $S_FIRST > $INITFILE
( echo 'attach( "DESTDIR" )' 
for i in $QFILES
do
    cat $i ; echo "" 
done ) | sed -e "s,DESTDIR,${DEST},g" | $SCMD 1>&2  || exit 1
# Replace the following line with each file terminated with a CR to prevent the problem 
# of missing CR at the end of the file.
#cat - $QFILES << EOF2 | sed -e "s,DESTDIR,${DEST},g" | $SCMD 1>&2  || exit 1
#attach( "DESTDIR" )
#EOF2
trap "" $sigs

