# $Id: Imakefile,v 1.18 1994/02/03 17:36:02 queinnec Exp $
# Copyright (c) 1990-94 by Christian Queinnec. All rights reserved.

###############################################################################
###                        LiSP2TeX
###   Christian Queinnec             or to:  Christian Queinnec
###   <queinnec@polytechnique.fr>            <Christian.Queinnec@inria.fr>
###   Laboratoire d'Informatique de l'X      INRIA -- Rocquencourt
###   Ecole Polytechnique                    Domaine de Voluceau, BP 105
###   91128 Palaiseau                        78153 Le Chesnay Cedex
###   France                                 France
###############################################################################
### This program is distributed in the hope that it will be useful.
### Use and copying of this software and preparation of derivative works
### based upon this software are permitted, so long as the following
### conditions are met:
###      o credit to the authors is acknowledged following current
###        academic behaviour
###      o no fees or compensation are charged for use, copies, or
###        access to this software
###      o this copyright notice is included intact.
### This software is made available AS IS, and no warranty is made about
### the software or its performance.

work : qnc.hack do.work

###############################################################################
###     		Public variables

# Define here your own maker. By default, make should be correct. The master
# file is an Imakefile that you can run directly by replacing make by 
# `imake -e'. If you (sadly) do not use imake then just use the regular make
# program on the Makefile that appears in the distribution. 

MAKE	=	make
#MAKE	=	imake -e

# Choose your Scheme compiler to regenerate LiSP2TeX. It can be Bigloo or
# Scheme->C, just comment out one of these two lines or add SCM=sthing to 
# your make command to void modifying this Imakefile.

#SCM		= bigloo
#SCM		= s2c

# Define the type of CPU you have. The HOSTTYPE variable is often set
# up by shells such as tcsh. Its value is only used to define in which
# subdirectory compilation is done. This allows you to rebuild
# LiSP2TeX concurrently for different architectures without messing
# files.

#HOSTTYPE	=	sun4
#HOSTTYPE	=	news_mips

# DOCDIR is the directory where will go the customisation files and
# the documentation. BINDIR is where will go the executable (be sure
# to have it in your PATH). STYDIR is the directory where (La)TeX style
# files are stored. MANDIR is the directory where man pages are kept.
# This INSTALL variable only copies files so you may use the `make
# clean' entry to save disk space.

MANEXTENSION	= 1
MANDIR		= /usr/man/man${MANEXTENSION}
BINDIR		= /usr/local/bin
DOCDIR		= /usr/local/lib/LiSP2TeX
STYDIR		= /usr/local/lib/tex/inputs
INSTALL		= install -c

# Choose your C compiler. Bigloo only uses gcc while Scheme->C can generally
# use both. Additional flags for the C compiler.

CC		= gcc
CFLAGS		= -O

# These are the parameters for the Bigloo compilation. The BIGLOODIR
# is the directory where Bigloo hides its bigloo.h file (and others).
# The following warning will appear, just ignore it!!!
# "*** WARNING:bigloo:eval redefinition of expander -- DEFINE-PERVASIVE-MACRO

BIGLOO		= bigloo
BIGLOODIR	= /usr/local/lib/bigloo/1.5
BIGLOOFLAGS	= -O2 -farithmetic

# These are the parameters for the Scheme->C compilation. The S2CDIR
# is the directory where Scheme->C hides its object.h file [You can
# find it by reading the scc shell file under flag LIBDIR]. 

SCC  		= scc -schf 14
SCI  		= sci
S2CDIR 		= /usr/local/lib/schemetoc

###			End of public variables
###############################################################################
# An useful default rule to convert a bk file into a TeX file. 
# Insert it in your own makefiles (or Imakefiles).

LiSP2TeX	= ${BINDIR}/LiSP2TeX
LiSP2TeXFLAGS	= -v -I${DOCDIR} 

.SUFFIXES: .tex .bk
.bk.tex : ;	${LiSP2TeX} ${LiSP2TeXFLAGS} -- $*.bk > $*.tex

#############################################################################x
# Public Install procedure: You must probably be root to make the
# final installation.

install : o/${HOSTTYPE}/${SCM}.LiSP2TeX
	@echo
	@echo '	This may fail if you are not root !'
	@echo
	${INSTALL} -s o/${HOSTTYPE}/${SCM}.LiSP2TeX ${BINDIR}/LiSP2TeX
	${INSTALL} LiSP2TeX.1   ${MANDIR}/LiSP2TeX.${MANEXTENSION}
	${INSTALL} LiSP2TeX.sty ${STYDIR}
	-mkdir ${DOCDIR}
	${INSTALL} LiSP2TeX.bk  ${DOCDIR}
	${INSTALL} LiSP2TeX.dvi ${DOCDIR}
	${INSTALL} README       ${DOCDIR}
	for f in *.l2t ; do ${INSTALL} $$f ${DOCDIR} ; done

#############################################################################
# By default, regenerate everything in the o/HOSTTYPE subdirectory and
# install the result in the usual directories (see below).

MAKEOPTIONS	= 	"SCM=${SCM}" 		\
			"HOSTTYPE=${HOSTTYPE}"	\
			"IMAKECONFIGFILES="
do.work : check-shell-variables
	@echo "	Binary files will go in o/${HOSTTYPE}/"
	@echo "	and I will use ${SCM} as Scheme compiler."
	@echo
	${MAKE} mkdir ${MAKEOPTIONS}
	${MAKE} o/${HOSTTYPE}/${SCM}.LiSP2TeX ${MAKEOPTIONS}
	@echo
	@echo "	Testing the new o/${HOSTTYPE}/${SCM}.LiSP2TeX..."
	${MAKE} test ${MAKEOPTIONS}
	@echo
	@echo -n " Do you want to install LiSP2TeX (yes,no)[n]? "
	@read ans ; case X$$ans in X[yY]*) 		\
		echo "	Installing LiSP2TeX..."		;\
		${MAKE} install ${MAKEOPTIONS}		;;\
	    *) echo "	Binary left in o/${HOSTTYPE}/${SCM}.LiSP2TeX" ;; esac
	@echo "	Everything seems OK! Happy LiSP2TeX'ing..."


#############################################################################
# The FINDER scheme file is used to implement how to efficiently search for
# LiSP2TeX directives in a file to be processed. It has to be very efficient.
# Don't set this variable yourself, this will be done automatically below.
### PORT Note: If you have Scheme->C just use this
#FINDER		= find-string.scm
### Otherwise use the already expanded version of the previous file:
### This is provided since macros are not standard enough.
#FINDER 	= look.scm
### But if you have Bigloo, use this one, which uses regular grammar.
#FINDER		= find-string.bgl

### PORT Note: Dependencies are gathered in files: port-${SCM}.scm
PORT 	= port-${SCM}.scm

### The sources: Before them must be loaded the PORT prologue and after them
### must be loaded the FINDER file.
LiSPsources =  	read-objects.scm	\
		read.scm		\
	    	scan-objects.scm	\
		scan-file.scm		\
		emit.scm		\
		pp.scm			\
	  	commands.scm		\
		version.scm

### The documentation:
LiSPdocs    = 	LiSP2TeX.sty		\
		LiSP2TeX.bk		\
	    	LiSP2TeX.tex		\
		LiSP2TeX.bbl		\
		LiSP2TeX.dvi		\
		LiSP2TeX.ps		\
	   	LiSP2TeX.1		\
		reading.test		\
		README			\
		util.scm		\
		ChangeLog

### Some customization files for example
LiSPcustom  =	default.l2t		\
		number.l2t		\
		pp.l2t			\
		qnc.l2t			\
		domain.l2t

### The files to include in the distribution:
LiSPallFiles =  Imakefile		\
		Makefile		\
		port-s2c.scm		\
		port-bigloo.scm		\
		${LiSPsources}		\
		find-string.scm		\
		find-string.bgl		\
		look.scm		\
		${LiSPdocs}		\
		${LiSPcustom}		\
		${HOME}/TeX/ApocaLIXP.sty
#add other ports, contrib                              FUTURE

############### Generate directories for binaries
# create subdirectories to store binaries
mkdir :
	-mkdir o
	-mkdir o/${HOSTTYPE}

# Check if the SCM and HOSTTYPE variables are set. 

check-shell-variables :
	@if [ X${SCM} = X ] ; then \
		echo "	Read the Imakefile and define SCM please. "	;\
		exit 1 ; else : ; fi
	@if [ X${HOSTTYPE} = X ] ; then \
		echo "	Read the Imakefile and define HOSTTYPE please."	;\
		exit 1 ; else : ; fi

# A little hack that saves me to remember how this Imakefile is used. I
# stick to just say `make'.

qnc.hack : 
	@if [ X$$USER = Xqueinnec ] ; then \
		${MAKE} do.qnc ${MAKEOPTIONS} ; \
		exit 1 ; else : ; fi
do.qnc :
	@echo -n "make distribution or rebuild only (d,r)[r]? "
	@read ans ; case X$$ans in \
		Xd*) ${MAKE} distribution ${MAKEOPTIONS} ;;\
		X*)  ${MAKE} rebuild      ${MAKEOPTIONS} ;; esac

################################################################################
############### This generation is adapted to Scheme->C

# All the necessary sources 
s2c_sources	= port-s2c.scm ${LiSPsources} find-string.scm

# Gather all files in a single module to simplify linking (if not 
# compilation time :).
o/${HOSTTYPE}/LiSP2TeX.sc : ${s2c_sources}
	( echo "(module lisp2tex (main unix-main))"	;\
	  for f in ${s2c_sources}			;\
	  do echo "(include \"$$f\")" 			;\
	  done ) 				> o/${HOSTTYPE}/LiSP2TeX.sc

# Takes 200 seconds on a Sony News3200 (20MHz r3000 MIPS).
# If your machine has a sufficient virtual memory you can set compile
# in one go instead of splitting the compilation into two steps. For that
# only define one of the two following macros.
#define S2C_ONE_STEP
#undefine S2C_TWO_STEPS

#ifdef 					S2C_ONE_STEP
o/${HOSTTYPE}/look.scm \
o/${HOSTTYPE}/s2c.LiSP2TeX : ${s2c_sources} o/${HOSTTYPE}/LiSP2TeX.sc 
	cd o/${HOSTTYPE} ; ${SCC} -O -o s2c.LiSP2TeX -I ../.. LiSP2TeX.sc
	if cmp look.scm o/${HOSTTYPE}/look.scm ; then : ;\
	else mv o/${HOSTTYPE}/look.scm . ; fi
#endif
#ifdef 					S2C_TWO_STEPS
o/${HOSTTYPE}/look.scm \
o/${HOSTTYPE}/s2c.LiSP2TeX.c : ${s2c_sources} o/${HOSTTYPE}/LiSP2TeX.sc 
	cd o/${HOSTTYPE} ; ${SCC} -O -C -I ../.. LiSP2TeX.sc
	if cmp look.scm o/${HOSTTYPE}/look.scm ; then : ;\
	else mv o/${HOSTTYPE}/look.scm . ; fi
	cd o/${HOSTTYPE} ; mv LiSP2TeX.c s2c.LiSP2TeX.c

o/${HOSTTYPE}/s2c.LiSP2TeX : o/${HOSTTYPE}/s2c.LiSP2TeX.c 
	cd o/${HOSTTYPE} ; ${CC} ${CFLAGS} -o s2c.LiSP2TeX s2c.LiSP2TeX.c \
		-I${S2CDIR} ${S2CDIR}/libsc.a -lm
#endif

###############################################################################
############### This generation is adapted to Bigloo

# All the necessary sources.
bigloo_sources	= port-bigloo.scm ${LiSPsources} find-string.bgl

# Gather all files in a single module to simplify linking (if not 
# compilation time :).  The produced executable is named bigloo.LiSP2TeX.
o/${HOSTTYPE}/LiSP2TeX.bgl : ${bigloo_sources}
	( echo "(module lisp2tex (main start)"			;\
	  for f in ${bigloo_sources}				;\
	  do echo "(include \"../../$$f\")"			;\
	  done ; echo ")					\
(define (start args) (unix-main args))" )	> o/${HOSTTYPE}/LiSP2TeX.bgl

# As for Scheme->C, you can compile in one or two steps. For that just
# define one of the appropriate following define.
#define BGL_ONE_STEP
#undefine BGL_TWO_STEPS

#ifdef BGL_ONE_STEP
o/${HOSTTYPE}/bigloo.LiSP2TeX : o/${HOSTTYPE}/LiSP2TeX.bgl
	cd o/${HOSTTYPE} ; \
		${BIGLOO} ${BIGLOOFLAGS} -o bigloo.LiSP2TeX LiSP2TeX.bgl
#endif

#ifdef BGL_TWO_STEPS
o/${HOSTTYPE}/bigloo.LiSP2TeX.c : o/${HOSTTYPE}/LiSP2TeX.bgl
	cd o/${HOSTTYPE} ; \
		${BIGLOO} ${BIGLOOFLAGS} -C+ \
		-o bigloo.LiSP2TeX.c LiSP2TeX.bgl
o/${HOSTTYPE}/bigloo.LiSP2TeX : o/${HOSTTYPE}/bigloo.LiSP2TeX.c 
	cd o/${HOSTTYPE} ; \
	  	${CC} ${CFLAGS} -o bigloo.LiSP2TeX -I${BIGLOODIR} \
		bigloo.LiSP2TeX.c 
#endif

###############################################################################
############### Obtaining the documentation
# These entries use the installed LiSP2TeX
LiSP2TeX.tex : LiSP2TeX.bk version.scm
	${LiSP2TeX} ${LiSP2TeXFLAGS} -- LiSP2TeX.bk > LiSP2TeX.tex
LiSP2TeX.dvi : LiSP2TeX.tex LiSP2TeX.sty 
	latex  LiSP2TeX.tex
	bibtex LiSP2TeX
	latex  LiSP2TeX.tex
	latex  LiSP2TeX.tex
LiSP2TeX.ps : LiSP2TeX.dvi
	dvips LiSP2TeX.dvi -o LiSP2TeX.ps
LiSP2TeX.spell : LiSP2TeX.tex
	delatex LiSP2TeX.tex | ispell -l | sort -u > LiSP2TeX.spell

###############################################################################
############### Print the readable files
PRINT 		= print
PRDVI 		= prdvi
print : print.sources print.man print.dvi
print.sources : 
	${PRINT} ${LiSPsources}
print.dvi : LiSP2TeX.dvi
	${PRDVI} LiSP2TeX.dvi
print.man :
	man LiSP2TeX | colcrt - | ${PRINT}

###############################################################################
############### Tags
TAGS-MAKER      = etags -t
tags : ;	${TAGS-MAKER} *.scm

################################################################################
############### Test compiled LiSP2TeX
# Test the fresh LiSP2TeX on the documentation, compare with the reference.
# There 
TIME	= time
test : 
	@echo "	Regenerate documentation ..."
	${TIME} o/${HOSTTYPE}/${SCM}.LiSP2TeX ${LiSP2TeXFLAGS} -- LiSP2TeX.bk \
		> o/${HOSTTYPE}/test.LiSP2TeX.tex
	@echo "	Compare with reference ..."
	@cd o/${HOSTTYPE} ; sed -e "/end{document}/q" 		\
		< test.LiSP2TeX.tex > LiSP2TeX.tex 
	rm o/${HOSTTYPE}/test.LiSP2TeX.tex
	if [ -r LiSP2TeX.tex ]  					;\
	then ( C=`diff o/${HOSTTYPE}/LiSP2TeX.tex LiSP2TeX.tex 		\
		| tee /dev/tty | wc -l` 				;\
	       echo "	$$C different lines"				;\
	       if test $$C -gt 16  					;\
	       then echo " Maybe incorrect!!!"				;\
	            echo " Perform the installation by hand if wanted."	;\
	 	    exit 2 						;\
	       else : ; fi )					;\
	else : ; fi

exploit.test : 
	@-if [ X$$USER = Xqueinnec ] 			;\
	then mv o/${HOSTTYPE}/LiSP2TeX.tex LiSP2TeX.tex	;\
	fi	

################################################################################
############### Test with interpreter Scheme->C
test.read.sci : 
	@( for f in ${s2c_sources}			;\
	   do echo "(load \"$$f\")" 			;\
	      done					;\
	   echo "(test-read \"reading.test\")"		) | ${SCI}

interp.sci : 
	@( for f in ${s2c_sources}			;\
	   do echo "(load \"$$f\")" 			;\
	      done					;\
	   echo "(unix-main '(sci))" 	; tee ) | ${SCI}

TEST_OPTIONS	= 	\"-v\" \"--\" \"LiSP2TeX.bk\"
test.interp.sci : 
	@( for f in ${s2c_sources}					;\
	   do echo "(load \"$$f\")" 					;\
	      done							;\
	   echo "(LiSP2TeX '(${TEST_OPTIONS}))"	 			;\
	   tee ) | ${SCI}

################################################################################
############### Test with interpreter Bigloo
test.read.bgl : 
	@( for f in ${bigloo_sources}			;\
	   do echo "(load \"$$f\")" 			;\
	      done					;\
	   echo "(test-read \"reading.test\")"		) | ${BIGLOO}

interp.bgl : 
	@( for f in ${bigloo_sources}			;\
	   do echo "(load \"$$f\")" 			;\
	      done					;\
	   echo "(unix-main '(bgl))" 	; tee ) | ${BIGLOO}

tmp.interp.bgl : 
	@( for f in ${bigloo_sources}			;\
	   do echo "(load \"$$f\")" 			;\
	      done					;\
	   echo "(unix-main '(bgl))" )
	${BIGLOO}

test.interp.bgl : 
	@( for f in ${bigloo_sources}					;\
	   do echo "(load \"$$f\")" 					;\
	      done							;\
	   echo "(LiSP2TeX '(${TEST_OPTIONS}))"	 			;\
	   tee ) | ${BIGLOO}

################################################################################
############### Making Distribution
DIST_DIR	= LiSP2TeX
PACK		= gzip

LiSP2TeX.tar : ${LiSPallFiles}
	-mkdir ${DIST_DIR}
	cp -p ${LiSPallFiles} ${DIST_DIR}
	chmod ug=rw,o=r ${DIST_DIR}/?*
	tar -cvf LiSP2TeX.tar ${DIST_DIR}
	rm -rf ${DIST_DIR}

LiSP2TeX.tar.gz : LiSP2TeX.tar
	${PACK} -f LiSP2TeX.tar

distribution : check-shell-variables
	-@rm -f version.scm ; ${MAKE} version.scm ${MAKEOPTIONS}
	@echo "	Check if all files are checked-in wrt CVS"
	${MAKE} notci
	@echo -n "	make ckpt (yes,no)[n]? " 
	@read ans ; case X$$ans in X[yY]*) ${MAKE} ckpt ;; *) : ;; esac
	${MAKE} do.rebuild ${MAKEOPTIONS}
	cvs tag `date +LiSP2TeX%y%h%d`
	${MAKE} LiSP2TeX.tar.gz
	mv LiSP2TeX.tar.gz `date +LiSP2TeX%y%h%d.tar.gz`

version.scm :
	echo ";;; Automatically generated, do not touch!!!"	> version.scm
	date "+(define version \"%y%h%d\")"			>> version.scm
	chmod a=r version.scm

FLOPPYDIR	= /fd

to.pc :
	@echo -n "Insert PC 720K floppy. Continue (yes,no)[n]? "
	@read ans ; case X$$ans in X[yY]*) : ;; *) exit 1 ;; esac
	mountu -t pcfs /dev/fd0 ${FLOPPYDIR}
	grep " ${FLOPPYDIR}" /etc/mtab
	-mkdir ${FLOPPYDIR}/lisptex
	@for f in ${LiSPallFiles} 					;\
	do (ff=`file2pc.prl $$f`					;\
	    echo copying $$f into ${FLOPPYDIR}/lisptex/$$ff...		;\
	    nl2crlf.prl $$f > ${FLOPPYDIR}/lisptex/$$ff 		)\
	done
	umountfd
	eject

############### My own entry to regenerate and test LiSP2TeX 
# It is similar to the work entry except that it ignores test results and
# systematically rebuilds the version.scm file.

rebuild : check-shell-variables
	-rm -f version.scm ; ${MAKE} version.scm ${MAKEOPTIONS}
	${MAKE} do.rebuild ${MAKEOPTIONS}

do.rebuild : 
	@echo "	Binary files will go in o/${HOSTTYPE}/"
	@echo "	and I will use ${SCM} as Scheme compiler."
	@echo
	${MAKE} mkdir ${MAKEOPTIONS}
	${MAKE} o/${HOSTTYPE}/${SCM}.LiSP2TeX ${MAKEOPTIONS}
	@echo
	@echo "	Testing ..."
	-${MAKE} test ${MAKEOPTIONS}
	${MAKE} ${MAKEOPTIONS} exploit.test
	@echo
	@echo -n " Do you want to install LiSP2TeX (yes,no)[n]? "
	@read ans ; case X$$ans in X[yY]*) 		\
		echo "	Installing LiSP2TeX..."		;\
		${MAKE} install ${MAKEOPTIONS}		;;\
	    *) echo "	Binary left in o/${HOSTTYPE}/${SCM}.LiSP2TeX" ;; esac

############### Clean 
clean :: cleanMakefile cleantex
	-rm TAGS 
	-rm o/${HOSTTYPE}/SC-TO-C* 
	-rm -rf LiSP2TeX*.tar* LiSP2TeX LiSP2TeX.ps
clean.all : clean
	-rm -rf o/${HOSTTYPE} LiSP2TeX.dvi LiSP2TeX*.tar.gz LiSP2TeX*.tar.Z
	-rm LiSP2TeX.tex 
	-rm look.scm version.scm

# end of Imakefile
