#!/bin/sh
# cb 010227
# $Id: parse-context,v 1.6 2003/04/09 12:04:33 mah Exp $

# trap ^C
trap killpvs 2

PARSEFILE=/tmp/parsefile-$$
PARSER=/tmp/parser-$$
PVS=${PVS:-/usr/local/pvs/pvs}
PVSOPTS=${PVSOPTS:-"-redhat 5 -emacs xemacs -no-site-file -batch -q -v 1"}

killpvs() {
  killall pvs-allegro5.0
  rm $PARSEFILE $PARSER
  exit 1
}

parsecontext() {


# either run gmake or make
if [ -e Makefile ]; then
	if command -v gmake 1>/dev/null 2>/dev/null; then
		gmake;
	elif command -v make 1>/dev/null 2>/dev/null; then
		make;
	fi
fi

cat > $PARSER << EOF
#!/usr/bin/perl -w

print ';; emacs lisp file for parsing this context
;; generated by parse-files-el - do not edit !

(setq files \'(
 "';

print join '"
 "', map { /([\w\d]+)\.pvs$/ } @ARGV;

print '"
))

(setq current-prefix-arg t)
(mapcar (lambda (f) (parse f)) files)
(save-context)
';
EOF

perl $PARSER *.pvs > $PARSEFILE
$PVS $PVSOPTS -l $PARSEFILE

rm $PARSEFILE $PARSER
}

if [ "$*" ] ; then
	for context in $* ; do
		( echo $context ; cd $context && parsecontext )
	done
else
	parsecontext
fi
