#!/bin/sh
#
# Copyright (C) 1990 Department of Computer Science, University of Queensland
#
# qc:	Qu-Prolog compiler (a la cc and nc)
#

options='-X -S -c -o outfile -aN -pN -hN -lN -tN -HN -LN -TN -xN -iN -sN'
usage="usage: $0 [ $options ] file ..."

# -Z	use SICStus version of the compiler
# -X	use the X version of Qu-Prolog
# -D	only run DCG translator
# -S	stop after compilation
# -c	do not link objects
# -o F	generate an executable or object file called F (default a.out)
# -aN	set atom table size to at least N
# -pN	set predicate table size to at least N
# -hN	set heap size to N
# -lN	set local stack size to N
# -tN	set trail size to N
# -dN	set delayed problem stack size to N
# -HN	set persistent stack size to N
# -LN	set persistent variables stack size to N
# -TN	set persistent trail size to N
# -xN	set number of X registers to N
# -iN	set size of code (instruction) area to N
# -sN	set size of string table to N
#
# BUGS: If -o is specified with -c, then if there is more than one
#	.ql file, then the object code for all .ql files will be
#	written to the same output file.

#
# location of Qu-Prolog project
#
project=/homes/qp/qp3.2
qolib=$project/qplib/obj

#
# location of NU-Prolog stuff for DCG translation
#
nubin="/usr/local/lib/nuprolog/lib/bin"
translate="$nubin/nep -R $nubin/nuc -a -D"

#
# location of Qu-Prolog binaries
#
qlib=$project/bin
compile=$qlib/qc1
compversion=sics
assemble=$qlib/qa
link=$qlib/ql
execute=$qlib/qem

libqofiles="$qolib/*.qo"

qgfiles=""
qlfiles=""
qsfiles=""
qofiles=""

newqlfiles=""
newqsfiles=""
newqofiles=""

dflag=false		# flag to stop after making .ql files
sflag=false		# flag to stop after making .qs files
cflag=false		# flag to stop after making .qo files

Doptions=""		# DCG translator options
Coptions=""		# compiler options
Aoptions=""		# assembler options
Loptions=""		# linker options
Eoptions=""		# emulator options

execfile=a.out		# default executable file name

#
# scan args, looking for options and setting file lists
#
while [ $# -ne 0 ]
do
	arg="$1"
	case "$arg" in
	-Z)	compversion=sics ;;
	-X)	execute=$qlib/xqem ;;
	-D)	dflag=true ;;
	-S)	sflag=true ;;
	-c)	cflag=true ;;
	-debug)
		Coptions="$Coptions debug" ;;
	-[ap]*)
		Loptions="$Loptions $arg" ;;
	-[si]*)
		Loptions="$Loptions $arg"
		Eoptions="$Eoptions $arg" ;;
	-[dhltxHLT]*)
		Eoptions="$Eoptions $arg" ;;
	-o)	if [ $# -eq 1 ]
		then	echo "$0: missing argument for -o option" >&2
			echo $usage >&2
			exit 1
		fi
		shift
		execfile="$1"
		case "$execfile" in
		"")	echo "$0: null output file name" >&2
			exit 1 ;;
		*.q[gls])
			echo "$0: dangerous output file name: $execfile" >&2
			exit 1 ;;
		esac ;;
	-*)	echo "$0: Unrecognized option: $arg" >&2
		echo $usage >&2
		exit 1 ;;
	*.qg)	qgfiles="$qgfiles $arg" ;;
	*.ql)	qlfiles="$qlfiles $arg" ;;
	*.qs)	qsfiles="$qsfiles $arg" ;;
	*.qo)	qofiles="$qofiles $arg" ;;
	*)	echo "$0: Unrecognized argument: $arg" >&2
		echo $usage >&2
		exit 1 ;;
	esac
	shift
done

if [ "$qgfiles$qlfiles$qsfiles$qofiles" = "" ]
then
	echo $usage >&2
	exit 1
fi

openfilename=
#
# trap hangup
#
trap "if [ -n \\\"\$openfilename\\\" -a -f \$openfilename ] ; \
      then mv -f  \$openfilename \$openfilename.incomplete; \
     fi; \
     exit " \
     1
#
# trap interrupt
#
trap "if [ -n \\\"\$openfilename\\\" -a -f \$openfilename ] ; \
      then [ -z \\\"\$openfilename\\\" ] || rm -f \$openfilename; \
      fi; \
      exit " \
      2
#
# first pass: translate DCGs (.qg -> .ql)
#
for arg in $qgfiles
do
	stem=`expr $arg : '\(.*\).qg'`
    openfilename=$stem.ql
	$translate $Doptions <$stem.qg >$stem.ql || exit 1
    openfilename=
	newqlfiles="$newqlfiles $stem.ql"
done

$dflag && exit 0

#
# second pass: compile (.ql -> .qs)
#
for arg in $qlfiles $newqlfiles
do
	stem=`expr $arg : '\(.*\).ql'`
    openfilename=$stem.qs
	$compile.$compversion $Coptions $stem.ql || exit 1
    openfilename=
	newqsfiles="$newqsfiles $stem.qs"
done

[ -z "$newqlfiles" ] || rm -f $newqlfiles

$sflag && exit 0

#
# third pass: assemble (.qs -> .qo)
#
for arg in $qsfiles $newqsfiles
do
	stem=`expr $arg : '\(.*\).qs'`
    openfilename=$stem.qo
	if [ "$execfile" != a.out ] && $cflag
	then
		$assemble $Aoptions -i $stem.qs -o $execfile || exit 1
	else
		$assemble $Aoptions -i $stem.qs -o $stem.qo || exit 1
	fi
    openfilename=
	newqofiles="$newqofiles $stem.qo"
done

[ -z "$newqsfiles" ] || rm -f $newqsfiles

$cflag && exit 0

#
# fourth pass: make save file and executable shell command file
#

case $execfile in
/*)
	;;
*)
	case $PWD in
	/u*)
		abspath=`pwd | sed "s,/u./$HOSTNAME/,/homes/,"`
		;;
	/tmp_mnt/*)
		abspath=`pwd | sed 's,/tmp_mnt/homes/[^/]*/,/homes/,'`
		;;
	/homes/*)
		abspath=$PWD
		;;
	esac
	execfile=$abspath/$execfile
	;;
esac

savefile=$execfile.qx

# generate save file

openfilename=$savefile
$link -o $savefile $Loptions $libqofiles $qofiles $newqofiles || exit 1
openfilename=

[ -z "$newqofiles" ] || rm -f $newqofiles

# make executable file

echo "$execute $Eoptions -Q $savefile \$*" >$execfile
chmod a+x $execfile
