# Scheme Shell Makefile
# Last updated October 1995 by BDC

# Documentation in files INSTALL and doc/install.txt

srcdir = @srcdir@
VPATH = @srcdir@
CC = @CC@
DEFS = @DEFS@
EXEEXT = @EXEEXT@
LIBS = @LIBS@
#DBOPEN = @DBOPEN@
CFLAGS = @CFLAGS@
CPPFLAGS= @CPPFLAGS@ -I$(srcdir)/cig -I$(srcdir)/scsh/regexp
INSTALL = @INSTALL@
INSTALL_PROGRAM = @INSTALL_PROGRAM@ -c
INSTALL_DATA = @INSTALL_DATA@ -c

LDFLAGS = -g @LDFLAGS@
LDFLAGS_AIX= @LDFLAGS_AIX@

RM	= rm -f

AR	= @AR@
RANLIB	= @RANLIB@

SHELL	= /bin/sh

prefix = @prefix@
exec_prefix = @exec_prefix@
bindir = $(exec_prefix)/bin
libdir = $(exec_prefix)/lib
incdir = $(exec_prefix)/include
mandir = $(prefix)/man
manext = 1

# HP 9000 series, if you don't have gcc
#   CC = cc 
#   CFLAGS = -Aa -O +Obb1800
#   DEFS = -D_HPUX_SOURCE -Dhpux

# Ultrix
#   LDFLAGS = -N

.c.o:
	$(CC) -c $(CPPFLAGS) $(DEFS) -I$(srcdir) -I. $(CFLAGS) -o $@ $<

# You might want to change RUNNABLE to "s48" # NOT -bri
RUNNABLE = scsh
MANPAGE = $(RUNNABLE).$(manext)
LIB = $(libdir)/$(RUNNABLE)

distdir = /users/ftp/pub/s48
#distdir = /net/mini-wheats/fs/admin/ftp/pub/s48

# If make barfs on this include line, just comment it out.  It's only
# really needed if you want to build the linker or rebuild initial.image.
include $(srcdir)/filenames.make
#
# Berkeley make wants to see this instead: (or use GNU make on BSD. -bri)
#.include "$(srcdir)/filenames.make"


# Static linker:
#
# You only need the linker if you're going to make changes to the
# things that go into the initial.image, which in general means the
# files in rts/.  If you decide you need to use the linker, then you
# gots your choice; it can run in just about any version of Scheme 48
# or Pseudoscheme.  (It has also been made to run in Scheme->C.)  It
# doesn't matter a whole lot which Scheme you use as long as it's not
# broken or unavailable.  The two best choices are:
# 1. As below: build the linker on the scheme48vm and scheme48.image
#    that are in the current directory.
# 2. LINKER_VM = $(RUNNABLE) $(BIG_HEAP)
#    LINKER_RUNNABLE = $(RUNNABLE)
#    These settings requires you to already have a $(RUNNABLE)
#    command.  This is desirable if you are making changes to the
#    system that might break scheme48vm and/or scheme48.image.  But it
#    requires you to have squirreled away a previous working version
#    of scheme48.

BIG_HEAP = -h 4000000
LINKER_VM = ./$(VM) $(BIG_HEAP)
LINKER_RUNNABLE = $(LINKER_VM) -i $(IMAGE)
LINKER_IMAGE = link/linker.image
LINKER = $(LINKER_VM) -i $(LINKER_IMAGE)
START_LINKER = echo ,batch; echo ,bench on;


# --------------------
# You shouldn't have to change anything below this point.

# Targets:

IMAGE = scheme48.image
INITIAL = $(srcdir)/initial.image
VM = scshvm
LIBCIG = cig/lib$(VM).a
CIG = cig/cig
CIGOBJS = cig/libcig.o cig/libcig1.o
LIBSCSH = scsh/lib$(VM).a
SCSHVMHACKS = proc2.o # postgcstub.o

SCSHOBJS = \
	scsh/dirstuff1.o \
	scsh/fdports1.o \
	scsh/flock.o scsh/flock1.o \
	scsh/machine/stdio_dep.o \
	scsh/machine/time_dep1.o \
	scsh/machine/signals1.o \
	scsh/machine/libansi.o \
	scsh/network.o scsh/network1.o \
	scsh/putenv.o \
	scsh/rdelim.o \
	scsh/rx/re-low.o scsh/rx/re1.o \
	scsh/select.o scsh/select1.o \
	scsh/sleep1.o \
	scsh/syscalls.o scsh/syscalls1.o \
	scsh/time.o scsh/time1.o \
	scsh/tty.o scsh/tty1.o \
	scsh/userinfo1.o \
	scsh/sighandlers1.o scsh/sighandlers.o \
	scsh/regexp/libregex.a

#	scsh/jcontrol2.o scsh/jcontrol1.o \
#    	scsh/$(DBOPEN) \
#    	scsh/dbm.o scsh/dbm1.o

OBJS = unix.o dynload.o prescheme.o extension.o scheme48vm.o \
	process_args.o $(CIGOBJS) $(SCSHVMHACKS) $(SCSHOBJS) 

# Sources:

CONFIG_FILES = interfaces.scm low-packages.scm rts-packages.scm \
	       comp-packages.scm

# Rules:

# The following is the first rule and therefore the "make" command's
# default target.
enough: $(VM) $(IMAGE) script $(LIBCIG) scsh $(LIBSCSH) $(MANPAGE) .notify

# The developers are curious to know.  Don't be concerned if this fails.
# You may comment these lines out if you wish to be discourteous.
.notify: minor-version-number
	touch .notify
	-echo SCSH 0.`cat $(srcdir)/scsh/minor-version-number` \
	   Scheme48 0.`cat $(srcdir)/minor-version-number` infestation. \
	   | mail scheme-48-notifications@zurich.ai.mit.edu 
	-echo Another scsh 0.`cat $(srcdir)/scsh/minor-version-number` \
	infestation. | mail scsh-notifications@zurich.ai.mit.edu 

# This says how to process .scm files with cig to make .c stubs.
.SUFFIXES: .scm
.scm.c:
	$(srcdir)/$(VM) -o $(srcdir)/$(VM) -i $(CIG) < $< > $*.c

# These .h files mediate between the code exported from foo1.c
# and imported into foo.scm's stub foo.c.

scsh/dirstuff1.o: scsh/dirstuff1.h
scsh/rdelim.o:    scsh/fdports1.h
scsh/userinfo1.o: scsh/userinfo1.h

scsh/fdports1.o	  scsh/fdports.o:  scsh/fdports1.h
scsh/flock1.o	  scsh/flock.o:	   scsh/flock1.h
scsh/network1.o	  scsh/network.o:  scsh/network1.h
scsh/select1.o	  scsh/select.o:   scsh/select1.h
scsh/syscalls1.o  scsh/syscalls.o: scsh/syscalls1.h
scsh/time1.o	  scsh/time.o:	   scsh/time1.h
scsh/tty1.o       scsh/tty.o:	   scsh/tty1.h

scsh/rx/re1.o	  scsh/rx/re-low.o:	   scsh/rx/re1.h

scsh/syscalls.o:  scsh/syscalls1.h scsh/dirstuff1.h scsh/fdports1.h \
	          scsh/select1.h   scsh/userinfo1.h

scsh/sighandlers1.o scsh/sighandlers.o: scsh/sighandlers1.h

# Not really, but making regexp/libregex.a makes the regexp/regex.h file that
# re-low.c actually does need.
scsh/rx/re-low.o: scsh/regexp/libregex.a

include $(srcdir)/scsh/machine/Makefile.inc
# Berkeley make wants to see this instead: (or use GNU make on BSD. -bri)
#.include "$(srcdir)/scsh/machine/Makefile.inc"

$(VM): main.o $(OBJS) $(AIX_P)
	if [ ! "$${OSTYPE}" = "cygwin32" ] ; then \
	    $(CC) $(LDFLAGS) $(LDFLAGS_AIX) -o $@ main.o $(OBJS) $(LIBS); \
	else \
	    dlltool  \
	        --export-all-symbols \
	        --output-def $@.def \
	        main.o $(OBJS); \
	    $(CC) -s -Wl,--base-file,$@.base -o $@ main.o $(OBJS); \
	    dlltool  \
	        --dllname $@ \
	        --input-def $@.def \
	        --base-file $@.base \
	        --output-exp $@.exp; \
	    $(CC) -s -Wl,--base-file,$@.base,$@.exp -o $@ main.o $(OBJS); \
	    dlltool \
	        --dllname $@ \
	        --input-def $@.def \
	        --base-file $@.base \
	        --output-exp $@.exp; \
	    $(CC) -Wl,$@.exp -o $@ main.o $(OBJS); \
	fi

$(LIBCIG): main.o $(OBJS)
#	$(CC) -r -o $@ main.o $(OBJS)
	$(RM) $@
	$(AR) $@ main.o $(OBJS) 
	$(RANLIB) $@

$(LIBSCSH): smain.o $(OBJS)
	$(RM) $@
	$(AR) $@ smain.o $(OBJS) 
	$(RANLIB) $@

main.o: main.c
	$(CC) -c -o $@ -DDEFAULT_IMAGE_NAME=\"$(LIB)/scsh.image\" \
	    $(CPPFLAGS) $(CFLAGS) $(srcdir)/main.c
scheme48vm.o: scheme48vm.c prescheme.h
extension.o: extension.c sysdep.h scheme48.h
unix.o: unix.c sysdep.h
dynload.o: dynload.c sysdep.h scheme48.h

# --------------------
# Make scheme48.image from initial.image and library .scm files.
#
# In the following, "make" passes $$USER to the shell as $USER, which
# the shell sees as an environment variable reference.
#
# For bootstrap reasons, initial.image is *not* listed as a source,
# even though it really is.

$(IMAGE): $(VM) env/init-defpackage.scm more-interfaces.scm link-packages.scm \
	   more-packages.scm $(usual-files) initial.debug build-usual-image
	sh $(srcdir)/build-usual-image "$(srcdir)" "$(LIB)" "$(IMAGE)" \
	   "$(VM)" "$(INITIAL)"

# Unix man page...

$(MANPAGE): scsh.man Makefile
	sed 's=LBIN=$(bindir)=g' $(srcdir)/scsh.man \
	 | sed 's=LLIB=$(LIB)=g' \
	 | sed 's=LSCSH=$(RUNNABLE)=g' >$@
	-chmod +r $@

script:
	(echo '#!/bin/sh'; \
	 echo exec '$(LIB)/$(VM)' -o '$(LIB)/$(VM)' -i '$(LIB)/$(IMAGE)' \"\$$\@\") \
	   > script
	-chmod +x script

### Fake targets:  all clean install man dist

install: install-dirs install-vm install-misc \
	install-man install-inc install-cig install-scsh

#install: script install-dirs install-vm install-image install-misc \
#	install-man install-inc install-cig install-scsh
#	This is now install-scsh
#	$(INSTALL_PROGRAM) script $(bindir)/$(RUNNABLE)

install-dirs:
	if [ ! -d $(prefix) ] ; then mkdir $(prefix); fi
	if [ ! -d $(exec_prefix) ] ; then mkdir $(exec_prefix); fi
	if [ ! -d $(bindir) ] ; then mkdir $(bindir); fi
	if [ ! -d $(libdir) ] ; then mkdir $(libdir); fi
	if [ ! -d $(incdir) ] ; then mkdir $(incdir); fi
	if [ ! -d $(mandir) ] ; then mkdir $(mandir); fi
	if [ ! -d $(mandir)/man$(manext) ] ; then mkdir $(mandir)/man$(manext); fi
	$(RM) -r $(LIB).old
	if [ -d $(LIB) ] ; then mv $(LIB) $(LIB).old ; fi
	mkdir $(LIB)
	mkdir $(LIB)/big
	mkdir $(LIB)/cig
	mkdir $(LIB)/doc
	mkdir $(LIB)/doc/scsh-manual
	mkdir $(LIB)/env
	mkdir $(LIB)/link
	mkdir $(LIB)/misc
	mkdir $(LIB)/opt
	mkdir $(LIB)/scsh
	mkdir $(LIB)/rts
	mkdir $(LIB)/vm

install-vm: $(VM)
	$(INSTALL_PROGRAM) $(VM)$(EXEEXT) $(LIB)/

install-image: $(IMAGE)
	$(INSTALL_DATA) $(IMAGE) $(LIB)/

install-man: $(MANPAGE)
	if test -d $(mandir)/man$(manext);  \
	  then $(INSTALL_DATA) $(MANPAGE) $(mandir)/man$(manext); \
          else echo "No man directory, not installing man page"; fi

install-inc: scheme48.h
	$(INSTALL_DATA) $(srcdir)/scheme48.h $(incdir)/

install-cig: cig
	$(INSTALL_PROGRAM) $(srcdir)/$(CIG) $(LIB)/cig
	$(INSTALL_PROGRAM) $(srcdir)/$(CIG).image $(LIB)/cig
	$(INSTALL_DATA) $(srcdir)/$(LIBCIG) $(LIB)/cig
	$(INSTALL_DATA) $(srcdir)/cig/libcig.h $(LIB)/cig

install-misc: $(LIB)/rts $(LIB)/env $(LIB)/big $(LIB)/opt \
	      $(LIB)/misc $(LIB)/link $(LIB)/vm $(LIB)/doc
	for f in $(srcdir)/rts/*num.scm; \
	    do $(INSTALL_DATA) $$f $(LIB)/rts/; done
	for f in $(srcdir)/env/*.scm; do $(INSTALL_DATA) $$f $(LIB)/env/; done
	for f in $(srcdir)/big/*.scm; do $(INSTALL_DATA) $$f $(LIB)/big/; done
	for f in $(srcdir)/opt/*.scm; do $(INSTALL_DATA) $$f $(LIB)/opt/; done
	for f in $(srcdir)/misc/*.scm; \
	    do $(INSTALL_DATA) $$f $(LIB)/misc/; done
	for f in $(srcdir)/link/*.scm; \
	    do $(INSTALL_DATA) $$f $(LIB)/link/; done
	for f in $(srcdir)/vm/*.scm; do $(INSTALL_DATA) $$f $(LIB)/vm/; done
	for f in $(srcdir)/doc/*.txt; do $(INSTALL_DATA) $$f $(LIB)/doc/; done
	for f in $(srcdir)/doc/*.tex; do $(INSTALL_DATA) $$f $(LIB)/doc/; done
	for f in $(srcdir)/doc/*.ps; do $(INSTALL_DATA) $$f $(LIB)/doc/; done
	for f in $(srcdir)/doc/scsh-manual/*.tex; \
	    do $(INSTALL_DATA) $$f $(LIB)/doc/scsh-manual/; done
	$(INSTALL_DATA) $(srcdir)/rts/jar-defrecord.scm $(LIB)/rts/
#  -p: Intermediate directories are created as necessary.
# -p not portable...it is POSIX 2, but its not widely available -bri
# just added install-dirs
$(LIB)/rts:
	mkdir $(LIB)/rts
$(LIB)/env:
	mkdir $(LIB)/env
$(LIB)/opt:
	mkdir $(LIB)/opt
$(LIB)/big:
	mkdir $(LIB)/big
$(LIB)/misc:
	mkdir $(LIB)/misc
$(LIB)/link:
	mkdir $(LIB)/link

configure: configure.in
	cd $(srcdir); autoconf

clean: clean-cig clean-scsh
	-rm -f $(VM)$(EXEEXT) *.o TAGS $(IMAGE) *.tmp script $(MANPAGE) \
	    link/*.image debug/*.image debug/*.debug mini mini-heap.c

clean-cig:
	-rm -f cig/*.o $(CIG) $(CIG).image $(LIBCIG)

distclean: clean
	$(RM) Makefile sysdep.h config.status config.log config.cache \
	        scsh/machine scsh/regexp/Makefile \
		scsh/endian.scm scsh/static.scm \
	        exportlist.aix
	$(RM) a.exe $(VM).base $(VM).def $(VM).exp 
	-find . -name '*~' -o -name '#*'  -o -name core -exec rm {} \;

man:	$(MANPAGE)

check:	$(VM) $(IMAGE) debug/check.scm
	(echo ,translate =scheme48/ $(srcdir)/; \
	 echo ,config ,load $(srcdir)/debug/test.scm; \
	 echo ,exec ,load $(srcdir)/debug/check.scm; echo ,exec "(done)") \
	| ./$(VM) -o ./$(VM) -i $(IMAGE) batch

# --------------------
# Rules from here on down are not essential for the basic installation
# procedure, and are not expected to work when srcdir is not the
# distribution directory.

all: vm linker
	$(MAKE) image
vm: $(VM)
linker: $(LINKER_IMAGE)
image: $(INITIAL)
	$(MAKE) $(IMAGE)

tags:
	etags scsh/*.scm scsh/*.c \
	  rts/*.scm bcomp/*.scm *.scm env/*.scm big/*.scm \
	  link/*.scm opt/*.scm debug/*.scm misc/*.scm \
	  vm/*.scm

# --------------------
# Increment the minor version number
inc:
	(cat minor-version-number; echo 1+p) | dc >minor-version-number.tmp
	mv minor-version-number.tmp minor-version-number
	echo \(define version-info \"0.`cat minor-version-number`\"\) \
	  >env/version-info.scm


# --------------------
# Generate filenames.make from *packages.scm
#
# This hack traces the module dependencies described in the
# various configuration files and converts them into dependency lists
# that "make" can use for its purposes.
#
# Since the distribution comes with a filenames.make, this rule
# shouldn't be invoked for simple installations.  But it will be used
# if you change any of the *-packages.scm files.
#
# You can actually the forms in filenames.scm to any Scheme
# implementation that has syntax-rules and explicit-renaming low-level
# macros (e.g., most versions of Scheme 48 and Pseudoscheme).
# If there are errors running this script, and you need to debug,
# don't use the initial.image, use something that has a reasonable
# environment.
#
# If this fails and you don't feel like debugging or fixing the problem,
# try "touch filenames.make" and hope for the best.

PACKAGES=packages.scm rts-packages.scm alt-packages.scm \
	        comp-packages.scm initial-packages.scm link-packages.scm \
	        more-packages.scm filenames.scm

filenames.make: $(PACKAGES)
	$(MAKE) $(VM) PACKAGES=
	./$(VM) -o ./$(VM) -i $(INITIAL) batch <filenames.scm
# or:	$(RUNNABLE) -s filenames.scm

# --------------------
# Static linker
#
# The linker is capable of rebuilding an image from sources, even
# across an incompatible change in VM data representations.

link/linker.image: $(linker-files) alt/init-defpackage.scm
	(echo ,batch; echo ,bench on;                  \
	 echo ,open signals handle features;           \
	 echo ,open bitwise ascii code-vectors record; \
	 echo ,load $(linker-files);	               \
	 echo ,load alt/init-defpackage.scm;	       \
	 echo ,dump link/linker.image)	               \
	| $(LINKER_RUNNABLE)

# Or, to bootstrap from Lucid Common Lisp: (last tested with
# Pseudoscheme 2.9 and Scheme 48 version 0.19)

PSEUDODIR = ../pseudo

link/linker-in-lucid: link/lucid-script.lisp $(linker-files) \
	    alt/pseudoscheme-features.scm alt/pseudoscheme-record.scm
	(echo \(defvar pseudoscheme-directory \"$(PSEUDODIR)/\"\); \
	 cat link/lucid-script.lisp; \
	 echo \(dump-linker\) \(lcl:quit\)) \
	| lisp

# --------------------
# Initial image
#
# The initial.image is built by the static linker.  The image contains
# Scheme, the byte-code compiler, and a minimal command processor, but
# no debugging environment to speak of.

$(INITIAL): $(LINKER_IMAGE) $(CONFIG_FILES) initial.scm $(initial-files) \
	scsh/scsh-read.scm scsh/here.scm # gross and Olin hates it -bri
	($(START_LINKER)               \
	 echo \(load-configuration \"interfaces.scm\"\); \
	 echo \(load-configuration \"packages.scm\"\); \
	 echo \(flatload initial-structures\); \
	 echo \(load \"initial.scm\"\);  \
	 echo \(link-initial-system\))   \
	| $(LINKER)

# --------------------
# Various small images for debugging low-level changes

LOAD_DEBUG = \
	 $(START_LINKER) \
	 echo \(load-configuration \"interfaces.scm\"\); \
	 echo \(load-configuration \"packages.scm\"\); \
	 echo \(flatload debug-structures\)

debug/tiny.image: $(LINKER_IMAGE) debug/tiny-packages.scm debug/tiny.scm
	($(START_LINKER) \
	 echo \(load-configuration \"debug/tiny-packages.scm\"\); \
	 echo \(link-simple-system \'\(debug tiny\) \'start tiny-system\)) \
	| $(LINKER)

debug/little.image: $(LINKER_IMAGE) $(CONFIG_FILES) debug-packages.scm
	($(LOAD_DEBUG); echo \(link-little-system\)) \
	| time $(LINKER)

debug/mini.image: $(LINKER_IMAGE) $(CONFIG_FILES)
	($(LOAD_DEBUG); echo \(link-mini-system\)) \
	| $(LINKER)

debug/medium.image: $(LINKER_IMAGE) $(CONFIG_FILES)
	($(LOAD_DEBUG); echo \(flatload compiler-structures\); \
	 echo \(link-medium-system\)) \
	| $(LINKER)

smain.o: main.c
	$(CC) -c $(CPPFLAGS) $(CFLAGS) -DSTATIC_AREAS -o $@ $(srcdir)/main.c

mini: mini-heap.o smain.o
	$(CC) $(LDFLAGS) -o $@ smain.o mini-heap.o $(OBJS) $(LIBS)
mini-heap.o: mini-heap.c
	$(CC) -c $(CPPFLAGS) $(CFLAGS) -o $@ $(srcdir)/mini-heap.c
mini-heap.c: debug/mini1.image
	(echo ,batch on; \
	 echo ,exec ,load misc/load-static.scm; \
	 echo \(do-it 150000 \"$(srcdir)/debug/mini1.image\" \"$@\"\)) \
	| ./$(VM) -o ./$(VM) -h 3000000  -i $(IMAGE)
debug/mini1.image: $(VM) debug/mini.image
	echo "(write-image \"debug/mini1.image\" \
	        	   (usual-resumer (lambda (args) \
	        	        	    (command-processor #f args))) \
	        	   \"foo\")" \
	| ./$(VM) -i debug/mini.image

# --------------------
# Generate scheme48.h from VM sources

scheme48.h: vm/arch.scm vm/data.scm link/generate-c-header.scm
	(echo ,bench; \
	 echo ,load-package big-scheme; echo ,open big-scheme; \
	 echo ,load link/generate-c-header.scm; \
	 echo \(make-c-header-file \"$@\" \
	        	           \"$(srcdir)/vm/arch.scm\" \"$(srcdir)/vm/data.scm\"\)) \
	| $(RUNNABLE) -s-

#scheme48vm.c: $(vm-files)
#	prescheme $(srcdir)/vm/compile-vm

cig: $(CIG) $(CIG).image $(LIBCIG)

$(CIG): $(VM) $(IMAGE) $(srcdir)/cig/cig.scm $(srcdir)/cig/libcig.scm
	(echo ",batch"; \
	 echo ",translate =scheme48/ $(srcdir)/"; \
	 echo ",config ,load $(srcdir)/cig/cig.scm"; \
	 echo ",config ,load $(srcdir)/cig/libcig.scm"; \
	 echo ",load-package cig-standalone"; \
	 echo ",in cig-standalone"; \
	 echo ",translate =scheme48/ $(LIB)/"; \
	 echo ",build cig-standalone-toplevel /tmp/cig") \
	| ./$(VM) -i ./$(IMAGE)
	$(srcdir)/cig/image2script $(LIB)/$(VM) </tmp/cig > $(CIG)
	-chmod +x $(CIG)
	$(RM) /tmp/cig

$(CIG)2:
	(echo ",batch"; \
	 echo ",translate =scheme48/ $(srcdir)/"; \
	 echo ",config ,load $(srcdir)/cig/cig2.scm"; \
	 echo ",config ,load $(srcdir)/cig/libcig.scm"; \
	 echo ",load-package cig-standalone"; \
	 echo ",in cig-standalone"; \
	 echo ",translate =scheme48/ $(LIB)/"; \
	 echo '(dump-scsh-program cig-standalone-toplevel "/tmp/cig")') \
	| ./$(VM) -o ./$(VM) -i ./scsh/scsh.image
	$(srcdir)/cig/image2script $(LIB)/$(VM) </tmp/cig > $(CIG)2
	-chmod +x $(CIG)2
	$(RM) /tmp/cig

$(CIG).image: $(IMAGE) $(VM) $(srcdir)/cig/cig.scm $(srcdir)/cig/libcig.scm
	(echo ",batch"; \
	 echo ",translate =scheme48/ $(srcdir)/"; \
	 echo ",config ,load $(srcdir)/cig/cig.scm"; \
	 echo ",config ,load $(srcdir)/cig/libcig.scm"; \
	 echo ",load-package cig-aux"; \
	 echo ",open define-foreign-syntax"; \
	 echo ",translate =scheme48/ $(LIB)/"; \
	 echo ",dump /tmp/cig \"(CIG Preloaded -bri)\"") \
	| ./$(VM) -o ./$(VM) -i ./$(IMAGE)
	$(srcdir)/cig/image2script $(LIB)/$(VM) \
	        	-o $(LIB)/$(VM) \
	        	</tmp/cig > $(CIG).image
	-chmod +x $(CIG).image
	$(RM) /tmp/cig

#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
# SCSH Specifics
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
scsh: cig scsh/scsh scsh/scsh.image

SCHEME =scsh/awk.scm \
	scsh/char-set.scm \
	scsh/defrec.scm \
	scsh/endian.scm \
	scsh/enumconst.scm \
	scsh/fdports.scm \
	scsh/fileinfo.scm \
	scsh/filemtch.scm \
	scsh/filesys.scm \
	scsh/flock.scm \
	scsh/fname.scm \
	scsh/fr.scm \
	scsh/glob.scm \
	scsh/here.scm \
	scsh/machine/bufpol.scm \
	scsh/machine/errno.scm \
	scsh/machine/fdflags.scm \
	scsh/machine/netconst.scm \
	scsh/machine/packages.scm \
	scsh/machine/signals.scm \
	scsh/machine/time_dep.scm \
	scsh/machine/tty-consts.scm \
	scsh/machine/waitcodes.scm \
	scsh/meta-arg.scm \
	scsh/network.scm \
	scsh/newports.scm \
	scsh/procobj.scm \
	scsh/pty.scm \
	scsh/rdelim.scm \
	scsh/rw.scm \
	scsh/scsh-condition.scm \
	scsh/scsh-interfaces.scm \
	scsh/scsh-package.scm \
	scsh/scsh-read.scm \
	scsh/scsh-version.scm \
	scsh/scsh.scm \
	scsh/select.scm \
	scsh/startup.scm \
	scsh/stringcoll.scm \
	scsh/syntax-helpers.scm \
	scsh/syntax.scm \
	scsh/syscalls.scm \
	scsh/time.scm \
	scsh/top.scm \
	scsh/tty.scm \
	scsh/utilities.scm

#    	scsh/dbm.scm db.scm ndbm.scm
#	static.scm static-heap.scm static1.scm
#	jcontrol

# Bogus, but it makes the scm->c->o two-ply dependency work.
# Explicitly giving the .o/.c dependency also makes it go.
############################################################
cig/libcig.c:	        cig/libcig.scm
scsh/flock.c:	        scsh/flock.scm
scsh/jcontrol2.c:	scsh/jcontrol2.scm
scsh/network.c:	        scsh/network.scm
scsh/rdelim.c:	        scsh/rdelim.scm
scsh/select.c:	    	scsh/select.scm
scsh/syscalls.c:	scsh/syscalls.scm
scsh/tty.c:	        scsh/tty.scm
scsh/time.c:	        scsh/time.scm

scsh/scsh: scsh/scsh-tramp.c
	$(CC) -o $@ $(CPPFLAGS) $(CFLAGS) \
	-DVM=\"$(LIB)/$(VM)\" \
	-DIMAGE=\"$(LIB)/scsh.image\" \
	scsh/scsh-tramp.c

loads = $(srcdir)/scsh/let-opt.scm $(srcdir)/scsh/scsh-interfaces.scm \
         $(srcdir)/scsh/machine/packages.scm \
	 $(srcdir)/scsh/rx/packages.scm \
	 $(srcdir)/scsh/rx/cond-package.scm \
	 $(srcdir)/scsh/scsh-package.scm \
	 $(srcdir)/scsh/lib/string-pack.scm \
	 $(srcdir)/scsh/lib/list-pack.scm \
	 $(srcdir)/scsh/lib/ccp-pack.scm


scsh/scsh.image: $(VM) $(SCHEME) $(CIG).image
	(echo ",translate =scheme48/ $(srcdir)/"; \
	 echo ",batch on"; \
	 echo ",load-package floatnums"; \
	 echo ",open externals"; \
	 echo "(lookup-all-externals)"; \
	 echo ",config"; \
	 echo ",load $(loads)"; \
	 echo ",load-package scsh"; \
	 echo ",load-package scsh-here-string-hax"; \
	 echo ",translate =scheme48/ $(LIB)/"; \
	 echo ",load-package list-lib"; \
	 echo ",load-package string-lib"; \
	 echo ",load-package ccp-lib"; \
	 echo ",in scsh-level-0"; \
	 echo "(%install-scsh-handlers)"; \
	 echo "(autoreap-policy 'early)"; \
	 echo ",user"; \
	 echo ",open floatnums"; \
	 echo ",open scsh"; \
	 echo ",open list-lib string-lib ccp-lib"; \
	 echo "(dump-scsh \"scsh/scsh.image\")") \
	| ./$(VM) -o ./$(VM) -h 10000000 -i $(CIG).image

# Removed these lines from scsh/scsh.image rule so you don't lose so
# badly when you are debugging in scsh. -Olin 6/95
#	 echo ",flush";
#	 echo ",flush maps source names files table";

#scsh/scsh.runnable: scsh/scsh.image
#	cig/image2script $(LIB)/$(VM) -o $(LIB)/$(VM) -h 1800000 \
#	    < scsh/scsh.image > $@
#	-chmod +x $@

scsh/regexp/libregex.a:
	cd ./scsh/regexp; $(MAKE) lib

scsh/scsh.vm: $(LIBSCSH) $(VM) scsh/scsh.image
	./$(VM) -o ./$(VM) -h 8000000 -i scsh/scsh.image \
	        -lm ./vm/ps-interface.scm \
	        -lm ./vm/interfaces.scm \
	        -lm ./vm/package-defs.scm \
	        -lm ./vm/s48-package-defs.scm \
	        -dm -m static-heaps -e static-heap-linker \
	        -s scsh/static.scm \
	        -i scsh/scsh.image -o $@

install-scsh: scsh
	$(RM) $(bindir)/$(RUNNABLE)$(EXEEXT)
	$(INSTALL_PROGRAM) $(srcdir)/scsh/scsh$(EXEEXT) \
	    $(bindir)/$(RUNNABLE)$(EXEEXT)
	$(INSTALL_PROGRAM) $(srcdir)/scsh/scsh.image $(LIB)/scsh.image
	$(INSTALL_PROGRAM) $(srcdir)/$(LIBSCSH) $(libdir)/$(LIBSCSH)
	$(RANLIB) $(libdir)/$(LIBSCSH)
	for f in $(srcdir)/scsh/*.scm; \
	    do $(INSTALL_DATA) $$f $(LIB)/scsh/; done

clean-scsh:
	$(RM) scsh/*.o scsh/regexp/*.o scsh/rx/*.o scsh/machine/*.o 
	$(RM) scsh/*.image 
	$(RM) $(LIBSCSH) scsh/scsh$(EXEEXT) scsh/scsh.vm
	-cd scsh/regexp; $(MAKE) clean
