#!/bin/csh
#
# $Id: configure,v 1.5 1993/10/27 08:16:37 bd Exp $
#
# Currently only:
# make Makefile and version.pam
#

if ( "$1" != "" ) then
	set P1='###'
	set P2=
	set P3=$1
else
	set P1=
	set P2='#'
	set P3='??????????????'
	echo '[No path argument given -- you must edit the generated Makefile]'
endif

sed -e "s <<1>> $P1 ;s <<2>> $P2 ;s <<3>> $P3 " >! Makefile << 'END_OF_TEXT'
#
# Copyright (C) 1990, 1991, 1992, 1993 Swedish Institute of Computer Science
#
# make all		to build everything
# make clean		to delete object files etc. not used during runtime
# make realclean	to delete everything that wasn't in the distribution
# make tape		to make a tape copy of everything needed

# Flags:
#	TRACE		include debugger
#	DEBUG		include low level PAM debugger
#	METERING	include performance measurement
#	BIGNUM		include arbritrary precision arithmetic

# Note that BIGNUM needs the GNU MP package included in the distribution.
# Locally installed GNU MP seem to need the -static flag to CC on Sun
# You can exclude GNU MP by removing the flag BIGNUM and remove the
# reference to gmp in LDFLAGS

# Comment out...
<<1>>wakeup:
<<1>>	@echo "Please set SOURCEPATH first."
<<2>>SOURCEPATH= <<3>>

SHELL= /bin/sh
TMP = /tmp
MAN1 = /usr/local/man/man1

MAKE= gmake
CC= gcc

CFLAGS= -O2 -g -DMETERING -DTRACE -DBIGNUM -DNDEBUG -DCOMPAT08 -I$(SOURCEPATH)/gmp
oCFLAGS= -O2 -DMETERING -DBIGNUM -DNDEBUG -DCOMPAT08 -I$(SOURCEPATH)/gmp
eCFLAGS= -O2 -DMETERING -DSTRUCT_ENV -DBIGNUM -DNDEBUG -DCOMPAT08 -I$(SOURCEPATH)/gmp

LDFLAGS= -L$(SOURCEPATH)/gmp -lgmp -lm 
# LDFLAGS= -n -L$(SOURCEPATH)/gmp -lgmp -lm
# LDFLAGS= -static -L$(SOURCEPATH)/gmp -lgmp

VERSION= AKL/PS 0.9

# SOURCEPATH is the path to the directory of this Makefile 
# (and all source files)
# Change it if you want to be able to make the system from
# another directory.

all:
	$(MAKE) $(MFLAGS) emul
	$(MAKE) $(MFLAGS) version libr demo
#	$(MAKE) $(MFLAGS) akltools

emul: gmplib
	(cd emulator; $(MAKE) $(MFLAGS) akl \
		CC=$(CC) CFLAGS='$(CFLAGS)' LDFLAGS='$(LDFLAGS)')
	/bin/rm -f akl
	echo '#! /bin/csh' > akl
	echo 'set AKLPATH = '$(SOURCEPATH) >> akl
	echo 'set CC = '$(CC) >> akl
	echo 'set CFLAGS = '"'$(CFLAGS)'" >> akl
	echo 'set LDFLAGS = '"'$(LDFLAGS)'" >> akl
	echo 'set TMP = '$(TMP) >> akl
	cat aklscript >> akl
	chmod +x akl

alt_emul: gmplib
	(cd emulator; $(MAKE) $(MFLAGS) realclean; \
	 $(MAKE) $(MFLAGS) akl CC=$(CC) CFLAGS='$(oCFLAGS)' LDFLAGS='$(LDFLAGS)'; \
	 mv akl oakl; \
	 $(MAKE) $(MFLAGS) realclean; \
	 $(MAKE) $(MFLAGS) akl CC=$(CC) CFLAGS='$(eCFLAGS)' LDFLAGS='$(LDFLAGS)'; \
	 mv akl eakl; \
	 $(MAKE) $(MFLAGS) realclean )

version:
	/bin/rm -f version.akl
	echo "akl_version('$(VERSION)')." > version.akl
	echo "akl_date('`date`')." >> version.akl
	echo "akl_copyright('(C) 1990-1993, Swedish Institute of Computer Science')." >> version.akl
	echo "akl_rights('All rights reserved.')." >> version.akl
	echo "library_directory(D) :- | D = '$(SOURCEPATH)/library/'." >> version.akl
	echo "compilef(version)."  | ./akl

libr:
	(cd library; $(MAKE) $(MFLAGS) all)

akltools:
	-(cd tools; $(MAKE) $(MFLAGS) all)

demo:
	(cd demos; $(MAKE) $(MFLAGS) all)

gmplib:
	(cd gmp; $(MAKE) $(MFLAGS) CC=$(CC) libgmp.a)

install:
	/bin/cp akl.1 $(MAN1)
	(cd tools; $(MAKE) $(MFLAGS) install)

clean:
	/bin/rm -f environment/version.akl
	-(cd emulator; $(MAKE) $(MFLAGS) clean)
	-(cd oldcompiler; $(MAKE) $(MFLAGS) clean)
	-(cd environment; $(MAKE) $(MFLAGS) clean)
	-(cd demos; $(MAKE) $(MFLAGS) clean)
	-(cd library; $(MAKE) $(MFLAGS) clean)
	-(cd gmp; $(MAKE) $(MFLAGS) clean)
	-(cd documentation/manual;  $(MAKE) $(MFLAGS) clean)

# never do realclean in gmp directory because user may not have "configure"
# program to recreate Makefile
realclean:
	-(cd emulator; $(MAKE) $(MFLAGS) realclean)
	-(cd oldcompiler; $(MAKE) $(MFLAGS) realclean)
	-(cd environment; $(MAKE) $(MFLAGS) realclean)
	-(cd demos; $(MAKE) $(MFLAGS) realclean)
	-(cd library;  $(MAKE) $(MFLAGS) realclean)
	/bin/rm -f akl
	-(cd gmp;  $(MAKE) $(MFLAGS) clean)
	-(cd documentation/manual;  $(MAKE) $(MFLAGS) clean)
'END_OF_TEXT'
echo "Makefile created."

cat >! version.pam << 'END_OF_TEXT'
predicate('akl_version'/1
	switch_on_term(2,2,1,1,1,1,1)
	label(1)
	fail
	label(2)
	try_single(0)
	label(0)
	allocate(0)
	get_constant_x0('AKL/PS VERSION')
	guard_wait
	proceed
)


predicate('akl_date'/1
	switch_on_term(2,2,1,1,1,1,1)
	label(1)
	fail
	label(2)
	try_single(0)
	label(0)
	allocate(0)
	get_constant_x0('DATE')
	guard_wait
	proceed
)


predicate('akl_copyright'/1
	switch_on_term(2,2,1,1,1,1,1)
	label(1)
	fail
	label(2)
	try_single(0)
	label(0)
	allocate(0)
	get_constant_x0('(C) 1990-1993, Swedish Institute of Computer Science')
	guard_wait
	proceed
)


predicate('akl_rights'/1
	switch_on_term(2,2,1,1,1,1,1)
	label(1)
	fail
	label(2)
	try_single(0)
	label(0)
	allocate(0)
	get_constant_x0('All rights reserved.')
	guard_wait
	proceed
)


predicate('library_directory'/1
	switch_on_term(1,1,1,1,1,1,1)
	label(1)
	try_single(0)
	label(0)
	allocate(1)
	get_y_variable(0,0)
	guard_commit
	put_y_value(0,1)
	get_constant('LIBRARY',1)
	proceed
)
'END_OF_TEXT'
echo "version.pam created."
