#### Start of system configuration section. ####

srcdir =	@srcdir@
VPATH =		@srcdir@

# Common prefix for machine-independent installed files.
prefix =	@prefix@

# Common prefix for machine-dependent installed files.
exec_prefix =	@exec_prefix@

bindir =	$(exec_prefix)/bin
libdir =	$(exec_prefix)/lib
syslibsdir =	$(exec_prefix)/Sys/libs
sysldir =	$(exec_prefix)/Sys/l
incdir =	$(exec_prefix)/include
etcdir =	$(exec_prefix)/etc
mandir =	$(prefix)/man

INSTALL =	@INSTALL@
INSTALL_PROGRAM = @INSTALL_PROGRAM@
INSTALL_DATA =	@INSTALL_DATA@

CC =		@CC@
AS =		as
CFLAGS =	@CFLAGS@
LDFLAGS =	@LDFLAGS@

RANLIB =	@RANLIB@

#### End system configuration section ####

# Directories that depend on baserel/baserel32/no-baserel only
BASE =		glue stack static net db


# Directories that depend on the CPU type
CPUONLY =	ixnet


# Directories that depend on the CPU and FPU type
CPUFPU =	general string stdlib stdio library


# Directories that depend on both baserel/baserel32/no-baserel and CPU/FPU type
# None at the moment, although 'stack' and 'static' should be moved here
# once we can use a CPU/FPU specific libc.a.
BASECPUFPU =


# For use in 'make clean' and 'make clobber'
DIRS =		$(BASE) $(CPUONLY) $(CPUFPU) $(BASECPUFPU)


###  CPU-FPU types.
###
###  The default thing to do is to make all reasonable combinations of the
###  library.
###
###  Note that libsrc, which builds the runtime startup files and all
###  versions of libc.a, and utils completely ignore the base/cpu/fpu variables,
###  so these are handled in the all: target.
###
###  You can remove specific CPU/FPU combinations that you are not interested in.

CPU-FPU-TYPES =	68000.soft-float \
		68020.soft-float 68020.68881 \
		68040.soft-float 68040.68881

# Some additional options for OTHER_CFLAGS that may or may not be useful are:
#
# -DDEBUG_VERSION	(build a debugging version)
# -DNOTRAP              (build a version that uses tc_UserData instead of
#                        tc_TrapData and also doesn't install any traphandlers.
#                        This can be used for debugging with a debugger other
#                        than gdb.

OTHER_CFLAGS = -static -fomit-frame-pointer -Wall


###############################################
###                                         ###
###  DO NOT EDIT ANYTHING BELOW THIS LINE!  ###
###                                         ###
###############################################

### All targets that are generated look like this:
###
### 	directory-name.cpu.fpu.base
###
### The following four defines extract one of these fields from the target name:

dir  = $(word 1, $(subst ., ,$@))
cpu  = $(word 2, $(subst ., ,$@))
fpu  = $(word 3, $(subst ., ,$@))
base = $(word 4, $(subst ., ,$@))


FLAGS_TO_PASS =	"AS=$(AS)"			\
		"BASE=$(base)"			\
		"BUILDDIR=$(dir)"		\
		"CC=$(CC)"			\
		"CFLAGS=$(CFLAGS)"		\
		"CPU=$(cpu)"			\
		"FPU=$(fpu)"			\
		"LDFLAGS=$(LDFLAGS)"		\
		"OTHER_CFLAGS=$(OTHER_CFLAGS)"	\
		"RANLIB=$(RANLIB)"

### Generate the targets we have to build. First we create defines for each
### type (dependent on base only, or only on cpu-fpu, or dependent on all
### three). Only the directory needs to be filled in in the second stage.

base-types         = $(d).68000.soft-float.baserel \
		     $(d).68020.soft-float.baserel32 \
		     $(d).68000.soft-float.no-baserel

cpu-types	   = $(addprefix $(d).,$(sort $(CPU-FPU-TYPES:.68881=.soft-float)))

cpu-fpu-types      = $(addprefix $(d).,$(CPU-FPU-TYPES))

base-cpu-fpu-types = $(addsuffix .baserel, $(cpu-fpu-types)) \
		     $(addsuffix .baserel32, $(cpu-fpu-types)) \
		     $(addsuffix .no-baserel, $(cpu-fpu-types))


### Now fill in the directory name

base-targets         := $(foreach d, $(BASE),       $(base-types))
cpu-targets          := $(foreach d, $(CPUONLY),    $(cpu-types))
cpu-fpu-targets      := $(foreach d, $(CPUFPU),     $(cpu-fpu-types))
base-cpu-fpu-targets := $(foreach d, $(BASECPUFPU), $(base-cpu-fpu-types))


### Make all targets and libsrc, utils and man pages.

all:	gcc-ok $(base-targets) $(cpu-targets) $(cpu-fpu-targets) $(base-cpu-fpu-targets)
	@(cd libsrc && $(MAKE) $(FLAGS_TO_PASS))
	@(cd utils && $(MAKE) $(FLAGS_TO_PASS))
	@(cd man && $(MAKE) $(FLAGS_TO_PASS))

gcc-ok:
	@echo >t.c
	@echo If the next command fails, then your gcc is out of date! 
	gcc -c -m68020 -resident32 -mrestore-a4 t.c
	@echo >gcc-ok
	@rm -f t.c t.o

###  Some targets are only dependent on BASE, others on CPU-FPU, and
###  others depend on all three.

###  This makes targets that are only dependent on BASE.

$(base-targets):
	@if [ ! -d $(dir)/$(base) ]; then	\
		echo mkdir -p $(dir)/$(base);	\
		mkdir -p $(dir)/$(base);	\
	fi;					\
	(cd $(dir)/$(base) ; $(MAKE) -f ../Makefile $(FLAGS_TO_PASS));


###  This makes targets that are only dependent on the CPU.

$(cpu-targets):
	@if [ ! -d $(dir)/$(cpu) ]; then		\
		echo mkdir -p $(dir)/$(cpu);		\
		mkdir -p $(dir)/$(cpu);			\
	fi;						\
	(cd $(dir)/$(cpu) ; $(MAKE) -f ../Makefile $(FLAGS_TO_PASS))


###  This makes targets that are only dependent on CPU and FPU.

$(cpu-fpu-targets):
	@if [ ! -d $(dir)/$(cpu)/$(fpu) ]; then		\
		echo mkdir -p $(dir)/$(cpu)/$(fpu);	\
		mkdir -p $(dir)/$(cpu)/$(fpu);		\
	fi;						\
	(cd $(dir)/$(cpu)/$(fpu) ; $(MAKE) -f ../../Makefile $(FLAGS_TO_PASS))


###  This makes targets that are dependent on CPU, FPU, and BASE.

$(base-cpu-fpu-targets):
	@if [ ! -d $(dir)/$(base)/$(cpu)/$(fpu) ]; then		\
		echo mkdir -p $(dir)/$(base)/$(cpu)/$(fpu);	\
		mkdir -p $(dir)/$(base)/$(cpu)/$(fpu);		\
	fi;							\
	(cd $(dir)/$(base)/$(cpu)/$(fpu) ; $(MAKE) -f ../../../Makefile $(FLAGS_TO_PASS));


###  Install all the libraries, include files, runtime files, etc.

install: installdirs
	if [ -f library/68000/soft-float/ixemul.library ] ; then \
		$(INSTALL) library/68000/soft-float/ixemul.library \
			$(syslibsdir)/ixemul.library; \
	else true; fi
	if [ -f library/68000/soft-float/ixemul.trace ] ; then \
		$(INSTALL) library/68000/soft-float/ixemul.trace \
			$(syslibsdir)/ixemul.trace; \
	else true; fi
	if [ -f library/68000/soft-float/ixemul.library ] ; then \
		$(INSTALL) library/68000/soft-float/ixemul.library \
			$(syslibsdir)/ixemul000.library; \
	else true; fi
	if [ -f library/68000/soft-float/ixemul.trace ] ; then \
		$(INSTALL) library/68000/soft-float/ixemul.trace \
			$(syslibsdir)/ixemul000.trace; \
	else true; fi
	if [ -f library/68020/soft-float/ixemul.library ] ; then \
		$(INSTALL) library/68020/soft-float/ixemul.library \
			$(syslibsdir)/ixemul020.library; \
	else true; fi
	if [ -f library/68020/soft-float/ixemul.trace ] ; then \
		$(INSTALL) library/68020/soft-float/ixemul.trace \
			$(syslibsdir)/ixemul020.trace; \
	else true; fi
	if [ -f library/68040/soft-float/ixemul.library ] ; then \
		$(INSTALL) library/68040/soft-float/ixemul.library \
			$(syslibsdir)/ixemul040.library; \
	else true; fi
	if [ -f library/68040/soft-float/ixemul.trace ] ; then \
		$(INSTALL) library/68040/soft-float/ixemul.trace \
			$(syslibsdir)/ixemul040.trace; \
	else true; fi
	if [ -f library/68020/68881/ixemul.library ] ; then \
		$(INSTALL) library/68020/68881/ixemul.library \
			$(syslibsdir)/ixemul020fpu.library; \
	else true; fi
	if [ -f library/68020/68881/ixemul.trace ] ; then \
		$(INSTALL) library/68020/68881/ixemul.trace \
			$(syslibsdir)/ixemul020fpu.trace; \
	else true; fi
	if [ -f library/68040/68881/ixemul.library ] ; then \
		$(INSTALL) library/68040/68881/ixemul.library \
			$(syslibsdir)/ixemul040fpu.library; \
	else true; fi
	if [ -f library/68040/68881/ixemul.trace ] ; then \
		$(INSTALL) library/68040/68881/ixemul.trace \
			$(syslibsdir)/ixemul040fpu.trace; \
	else true; fi
	if [ -f library/68060/68881/ixemul.library ] ; then \
		$(INSTALL) library/68060/68881/ixemul.library \
			$(syslibsdir)/ixemul060fpu.library; \
	else true; fi
	if [ -f library/68060/68881/ixemul.trace ] ; then \
		$(INSTALL) library/68060/68881/ixemul.trace \
			$(syslibsdir)/ixemul060fpu.trace; \
	else true; fi

	if [ -f ixnet/68000/ixnet.library ] ; then \
		$(INSTALL) ixnet/68000/ixnet.library \
			$(syslibsdir)/ixnet.library; \
	else true; fi
	if [ -f ixnet/68000/ixnet.library ] ; then \
		$(INSTALL) ixnet/68000/ixnet.library \
			$(syslibsdir)/ixnet000.library; \
	else true; fi
	if [ -f ixnet/68020/ixnet.library ] ; then \
		$(INSTALL) ixnet/68020/ixnet.library \
			$(syslibsdir)/ixnet020.library; \
	else true; fi
	if [ -f ixnet/68040/ixnet.library ] ; then \
		$(INSTALL) ixnet/68040/ixnet.library \
			$(syslibsdir)/ixnet040.library; \
	else true; fi
	if [ -f ixnet/68060/ixnet.library ] ; then \
		$(INSTALL) ixnet/68060/ixnet.library \
			$(syslibsdir)/ixnet060.library; \
	else true; fi

	$(INSTALL) libsrc/crt0.o $(libdir)/crt0.o
	$(INSTALL) libsrc/bcrt0.o $(libdir)/bcrt0.o
	$(INSTALL) libsrc/gcrt0.o $(libdir)/gcrt0.o
	$(INSTALL) libsrc/mcrt0.o $(libdir)/mcrt0.o
	$(INSTALL) libsrc/rcrt0.o $(libdir)/rcrt0.o
	$(INSTALL) libsrc/scrt0.o $(libdir)/scrt0.o
	$(INSTALL) libsrc/lcrt0.o $(libdir)/lcrt0.o
	$(INSTALL) libsrc/libc.a $(libdir)/libc.a
	$(INSTALL) libsrc/libbc.a $(libdir)/libb/libc.a
	$(INSTALL) libsrc/libb32c.a $(libdir)/libb32/libc.a
	$(INSTALL) libsrc/libc_p.a $(libdir)/libc_p.a
	$(INSTALL_PROGRAM) utils/ixtrace $(bindir)/ixtrace
	$(INSTALL_PROGRAM) utils/ixprefs $(bindir)/ixprefs
	$(INSTALL_PROGRAM) utils/ixtimezone $(bindir)/ixtimezone
	$(INSTALL_PROGRAM) utils/ixrun $(bindir)/ixrun
	$(INSTALL_PROGRAM) utils/tzselect $(bindir)/tzselect
	$(INSTALL_PROGRAM) utils/ixstack $(bindir)/ixstack
	$(INSTALL) utils/ixpipe-handler $(sysldir)/ixpipe-handler
	(cd $(srcdir)/include && cp -r . $(incdir))
	find $(incdir) -type d -name CVS -print | xargs rm -rf
	(cd $(srcdir)/man && cp -r man? $(mandir))
	find $(mandir) -type d -name CVS -print | xargs rm -rf
	mkdir -p $(etcdir)/zoneinfo
	cp $(srcdir)/utils/*.tab $(etcdir)/zoneinfo
	(cd utils/zoneinfo && cp -r . $(etcdir)/zoneinfo)
		

installdirs:	mkinstalldirs
	$(srcdir)/mkinstalldirs $(bindir) $(libdir) $(syslibsdir) \
		$(sysldir) $(incdir) $(etcdir) $(mandir) $(libdir)/libb \
		$(libdir)/libb32


clean:
	@-for i in $(DIRS) libsrc utils man; \
		do (cd $$i ; $(MAKE) $(FLAGS_TO_PASS) clean) ; done


clobber:
	@-for i in $(DIRS) libsrc utils man; \
		do (cd $$i ; $(MAKE) $(FLAGS_TO_PASS) clobber) ; done
	-rm -f Makefile config.* conftest.c gcc-ok


# Build source and binary distribution files.

dist:
	#
	# First get rid of any leftovers from a previous "make dist"
	#
	rm -rf ade ixemul distrib
	mkdir -p distrib
	#
	# Create ixemul-bin.lha, which contains utilities
	# that are generally useful to ixemul.library users.
	#
	mkdir -p ade/bin ade/Sys/l ade/Sys/devs
	cp -p utils/ixprefs ade/bin/ixprefs
	cp -p utils/ixtrace ade/bin/ixtrace
	cp -p utils/ixrun ade/bin/ixrun
	cp -p utils/ixstack ade/bin/ixstack
	cp -p utils/ixpipe-handler ade/Sys/l/ixpipe-handler
	cp -p $(srcdir)/utils/Mountlist ade/Sys/devs/MountList.ixpipe
	(cd ade && lha -mraxeq a /distrib/ixemul-bin.lha bin Sys)
	sleep 5
	rm -rf ade
	#
	# Create ixemul-tz.lha, which has timezone related pieces.
	#
	mkdir -p ade/etc ade/bin
	cp -p utils/ixtimezone ade/bin/ixtimezone
	cp -p utils/zic ade/bin/zic
	cp -p utils/tzselect ade/bin/tzselect
	cp -pr utils/zoneinfo ade/etc/zoneinfo
	cp $(srcdir)/utils/*.tab ade/etc/zoneinfo
	(cd ade && lha -mraxeq a /distrib/ixemul-tz.lha bin etc)
	sleep 5
	rm -rf ade
	#
	# Create ixemul-sdk.lha, which contains the files that
	# programmers need to build applications that use ixemul.library.
	#
	mkdir -p ade/lib/libb
	mkdir -p ade/lib/libb32
	cp -p libsrc/crt0.o ade/lib/crt0.o
	cp -p libsrc/bcrt0.o ade/lib/bcrt0.o
	cp -p libsrc/gcrt0.o ade/lib/gcrt0.o
	cp -p libsrc/mcrt0.o ade/lib/mcrt0.o
	cp -p libsrc/rcrt0.o ade/lib/rcrt0.o
	cp -p libsrc/lcrt0.o ade/lib/lcrt0.o
	cp -p libsrc/scrt0.o ade/lib/scrt0.o
	cp -p libsrc/libc.a ade/lib/libc.a
	cp -p libsrc/libbc.a ade/lib/libb/libc.a
	cp -p libsrc/libb32c.a ade/lib/libb32/libc.a
	cp -p libsrc/libc_p.a ade/lib/libc_p.a
	cp -pr $(srcdir)/include ade/include
	mkdir ade/man
	cp -pr man/cat? ade/man
	find ade -name CVS -print | xargs rm -rf
	(cd ade && lha -mraxeq a /distrib/ixemul-sdk.lha lib include man)
	sleep 5
	rm -rf ade
	#
	# Create ixemul-doc.lha, which contains various documentation
	# files.
	#
	mkdir ixemul
	cp -p $(srcdir)/COPYING ixemul/COPYING
	cp -p $(srcdir)/COPYING.LIB ixemul/COPYING.LIB
	cp -p $(srcdir)/INSTALL ixemul/INSTALL
	cp -p $(srcdir)/NEWS ixemul/NEWS
	cp -p $(srcdir)/BUGS ixemul/BUGS
	cp -p $(srcdir)/TODO ixemul/TODO
	cp -p $(srcdir)/README ixemul/README
	cp -p $(srcdir)/utils/ixprefs.guide ixemul/ixprefs.guide
	cp -p $(srcdir)/utils/ixprefs.guide.info ixemul/ixprefs.guide.info
	lha -mraxeq a distrib/ixemul-doc.lha ixemul
	rm -rf ixemul
	#
	# Create ixemul-src.tgz, which contains the source code.
	# Do it in a temporary location so we don't have to
	# know where the build dir is, or translate between
	# Unix & AmigaOS forms.  We also delete any CVS dirs
	# at this time.
	#
	rm -rf ixemul-src.tgz ixemul
	cp -pr $(srcdir) ixemul
	find ixemul -name CVS -print | xargs rm -rf
	lha -mraxeq a distrib/ixemul-src.lha ixemul
	rm -rf ixemul
	#
	# Create ixemul-<cpu><fpu>.lha
	#
	mkdir -p ade/Sys/libs
	cp -p library/68000/soft-float/ixemul.library ade/Sys/libs/ixemul000.library
	cp -p ixnet/68000/ixnet.library ade/Sys/libs/ixnet000.library
	cp -p $(srcdir)/README ade/Sys/libs/README
	(cd ade && lha -mraxeq a /distrib/ixemul-000s.lha Sys)
	rm -f ade/Sys/libs/*
	cp -p library/68000/soft-float/ixemul.trace ade/Sys/libs/ixemul000.trace
	cp -p $(srcdir)/README ade/Sys/libs/README
	(cd ade && lha -mraxeq a /distrib/ixemul-000t.lha Sys)
	rm -f ade/Sys/libs/*
	cp -p library/68020/soft-float/ixemul.library ade/Sys/libs/ixemul020.library
	cp -p ixnet/68020/ixnet.library ade/Sys/libs/ixnet020.library
	cp -p $(srcdir)/README ade/Sys/libs/README
	(cd ade && lha -mraxeq a /distrib/ixemul-020s.lha Sys)
	rm -f ade/Sys/libs/*
	cp -p library/68040/soft-float/ixemul.library ade/Sys/libs/ixemul040.library
	cp -p ixnet/68040/ixnet.library ade/Sys/libs/ixnet040.library
	cp -p $(srcdir)/README ade/Sys/libs/README
	(cd ade && lha -mraxeq a /distrib/ixemul-040s.lha Sys)
	rm -f ade/Sys/libs/*
	cp -p library/68020/68881/ixemul.library ade/Sys/libs/ixemul020fpu.library
	cp -p ixnet/68020/ixnet.library ade/Sys/libs/ixnet020fpu.library
	cp -p $(srcdir)/README ade/Sys/libs/README
	(cd ade && lha -mraxeq a /distrib/ixemul-020f.lha Sys)
	rm -f ade/Sys/libs/*
	cp -p library/68040/68881/ixemul.library ade/Sys/libs/ixemul040fpu.library
	cp -p ixnet/68040/ixnet.library ade/Sys/libs/ixnet040fpu.library
	cp -p $(srcdir)/README ade/Sys/libs/README
	(cd ade && lha -mraxeq a /distrib/ixemul-040f.lha Sys)
	rm -f ade/Sys/libs/*
#	cp -p library/68060/68881/ixemul.library ade/Sys/libs/ixemul060fpu.library
#	cp -p ixnet/68060/ixnet.library ade/Sys/libs/ixnet060fpu.library
#	cp -p $(srcdir)/README ade/Sys/libs/README
#	(cd ade && lha -mraxeq a /distrib/ixemul-060f.lha Sys)
	sleep 5
	rm -rf ade
