#### 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 ####

# Supported OSes
OS =		amigaos pos

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


# Directories that depend on the CPU and OS type
CPUOSONLY =	ixnet


# Directories that depend on the CPU, FPU and OS type
CPUFPUOS =	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) $(CPUOSONLY) $(CPUFPUOS) $(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.
###
###  There is one special combination: "notrap.soft-float".
###  In this case an 68000.soft-float version will be compiled with the -DNOTRAP
###  option. See the OTHER_CFLAGS explanation for more information.
###
###  You can remove specific CPU/FPU combinations that you are not interested in.

CPU-FPU-TYPES =	68000.soft-float notrap.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

POS_CFLAGS = -nostdinc -D__pos__ -I/p -I/pos/pOS_Dev/pOSxA


###############################################
###                                         ###
###  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 ., ,$@))
os   = $(word 4, $(subst ., ,$@))


FLAGS_TO_PASS =	"AS=$(AS)"			\
		"BASE=$(base)"			\
		"BUILDDIR=$(dir)"		\
		"CC=$(CC)"			\
		"CFLAGS=$(CFLAGS)"		\
		"CPU=$(cpu)"			\
		"FPU=$(fpu)"			\
		"LDFLAGS=$(LDFLAGS)"		\
		"OS=$(os)"			\
		"OTHER_CFLAGS=$(OTHER_CFLAGS)"	\
		"POS_CFLAGS=$(POS_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-os-types	   = $(addprefix $(d).,$(sort $(CPU-FPU-TYPES:.68881=.soft-float)))

cpu-fpu-os-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-os-targets       := $(foreach o, $(OS), $(addsuffix .$(o),$(foreach d, $(CPUOSONLY), $(cpu-os-types))))
cpu-fpu-os-targets   := $(foreach o, $(OS), $(addsuffix .$(o),$(foreach d, $(CPUFPUOS), $(cpu-fpu-os-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-os-targets) $(cpu-fpu-os-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.
###  Override the settings CPU and OTHER_CFLAGS if CPU is "notrap".

$(cpu-os-targets):
	@if [ ! -d $(dir)/$(cpu)/$(os) ]; then				\
		echo mkdir -p $(dir)/$(cpu)/$(os);			\
		mkdir -p $(dir)/$(cpu)/$(os);				\
	fi;								\
	if [ $(cpu) -eq "notrap" ]; then				\
		(cd $(dir)/$(cpu)/$(os) ; 				\
		 $(MAKE) -f ../../Makefile $(FLAGS_TO_PASS) 		\
		  CPU=68000 "OTHER_CFLAGS=$(OTHER_CFLAGS) -DNOTRAP"); 	\
	else								\
		(cd $(dir)/$(cpu)/$(os) ; 				\
		 $(MAKE) -f ../../Makefile $(FLAGS_TO_PASS));		\
	fi;


###  This makes targets that are only dependent on CPU and FPU.
###  Override the settings CPU and OTHER_CFLAGS if CPU is "notrap".

$(cpu-fpu-os-targets):
	@if [ ! -d $(dir)/$(cpu)/$(fpu)/$(os) ]; then		\
		echo mkdir -p $(dir)/$(cpu)/$(fpu)/$(os);	\
		mkdir -p $(dir)/$(cpu)/$(fpu)/$(os);		\
	fi;						        \
	if [ $(cpu) -eq "notrap" ]; then			\
		(cd $(dir)/$(cpu)/$(fpu)/$(os) ; 		\
		 $(MAKE) -f ../../../Makefile $(FLAGS_TO_PASS) 	\
		  CPU=68000 "OTHER_CFLAGS=$(OTHER_CFLAGS) -DNOTRAP"); \
	else							\
		(cd $(dir)/$(cpu)/$(fpu)/$(os) ; 		\
		 $(MAKE) -f ../../../Makefile $(FLAGS_TO_PASS));\
	fi;

###  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.

ixemul-targets   := $(addprefix ixemul.,$(foreach o, $(OS), $(addsuffix .$(o),$(CPU-FPU-TYPES))))
ixcpu  = $(subst 68,,$(word 2, $(subst ., ,$@)))


$(ixemul-targets):
	@echo Installing $@;						\
	if [ "$(fpu)" = "68881" ]; then 				\
		fpu=-fpu; 						\
	else								\
		fpu=;							\
	fi;								\
	if [ "$(cpu)" = "notrap" ]; then 				\
		fpu=-notrap; cpu=000;					\
	else								\
		cpu=$(ixcpu);						\
	fi;								\
	if [ "$(os)" = "pos" ]; then 					\
		os=-pos; 						\
	else								\
		pos=; 							\
	fi;								\
	if [ -f library/$(cpu)/$(fpu)/$(os)/ixemul.library ]; then 	\
		$(INSTALL) library/$(cpu)/$(fpu)/$(os)/ixemul.library 	\
			$(syslibsdir)/ixemul-$$cpu$$fpu$$os.library;	\
	else true; fi;							\
	if [ -f library/$(cpu)/$(fpu)/$(os)/ixemul.trace ]; then 	\
		$(INSTALL) library/$(cpu)/$(fpu)/$(os)/ixemul.trace	\
			$(syslibsdir)/ixemul-$$cpu$$fpu$$os.trace;	\
	else true; fi;							\
	if [ -z "$$fpu" -a -f ixnet/$(cpu)/$(os)/ixnet.library ]; then	\
		$(INSTALL) ixnet/$(cpu)/$(os)/ixnet.library 		\
			$(syslibsdir)/ixnet-$$cpu$$os.library; 	\
	else true; fi

install: installdirs $(ixemul-targets)
	@echo Installing library/68000/soft-float/amigaos/ixemul.library
	@if [ -f library/68000/soft-float/amigaos/ixemul.library ] ; then \
		$(INSTALL) library/68000/soft-float/amigaos/ixemul.library \
			$(syslibsdir)/ixemul.library; \
	else true; fi
	@echo Installing library/68000/soft-float/amigaos/ixemul.trace
	@if [ -f library/68000/soft-float/amigaos/ixemul.trace ] ; then \
		$(INSTALL) library/68000/soft-float/amigaos/ixemul.trace \
			$(syslibsdir)/ixemul.trace; \
	else true; fi
	@echo Installing ixnet/68000/amigaos/ixnet.library
	@if [ -f ixnet/68000/amigaos/ixnet.library ] ; then \
		$(INSTALL) ixnet/68000/amigaos/ixnet.library \
			$(syslibsdir)/ixnet.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/libm020/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_PROGRAM) utils/ipcs $(bindir)/ipcs
	$(INSTALL_PROGRAM) utils/ipcrm $(bindir)/ipcrm
	$(INSTALL) utils/ixpipe-handler $(sysldir)/ixpipe-handler
	(cd $(srcdir)/include && cp -pr . $(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/libm020


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.

ixdist-targets   := $(addprefix ixdist.,$(foreach o, $(OS), $(addsuffix .$(o),$(CPU-FPU-TYPES))))

$(ixdist-targets):
	@echo Distributing $@;						\
	mkdir -p ade/Sys/libs;						\
	ext=library;							\
	if [ "$(fpu)" = "68881" ]; then 				\
		fpulha=f; fpu=-fpu;					\
	else								\
		fpulha=s; fpu=;						\
	fi;								\
	if [ "$(cpu)" = "notrap" ]; then 				\
		cpu=000; fpulha=n; fpu=-notrap;	ext=trace;		\
	else								\
		cpu=$(ixcpu); 						\
	fi;								\
	if [ "$(os)" = "pos" ]; then 				        \
		os=-pos; 						\
	else								\
		os=; 							\
	fi;								\
	mkdir -p ade/Sys/libs;	 					\
	cp -p library/$(cpu)/$(fpu)/$(os)/ixemul.$$ext	 		\
		ade/Sys/libs/ixemul$$cpu$$fpu$$os.library;		\
	cp -p ixnet/$(cpu)/$(os)/ixnet.library 				\
		ade/Sys/libs/ixnet$$cpu$$os.library;			\
	cp -p $(srcdir)/README ade/Sys/libs/README;			\
	cp -p $(srcdir)/README.pOS ade/Sys/libs/README.pOS;		\
	(cd ade && lha -mraxeq a /distrib/ixemul-$$cpu$$fpulha$$os.lha Sys);\
	rm -f ade/Sys/libs/*

dist-prepare:
	#
	# First get rid of any leftovers from a previous "make dist"
	#
	rm -rf ade ixemul distrib
	mkdir -p distrib

dist:	dist-prepare $(ixdist-targets)
	#
	# 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/ipcs ade/bin/ipcs
	cp -p utils/ipcrm ade/bin/ipcrm
	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/libm020
	cp -p libsrc/crt0.o ade/lib/crt0.o && strip -g ade/lib/crt0.o
	cp -p libsrc/bcrt0.o ade/lib/bcrt0.o && strip -g ade/lib/bcrt0.o
	cp -p libsrc/gcrt0.o ade/lib/gcrt0.o && strip -g ade/lib/gcrt0.o
	cp -p libsrc/mcrt0.o ade/lib/mcrt0.o && strip -g ade/lib/mcrt0.o
	cp -p libsrc/rcrt0.o ade/lib/rcrt0.o && strip -g ade/lib/rcrt0.o
	cp -p libsrc/lcrt0.o ade/lib/lcrt0.o && strip -g ade/lib/lcrt0.o
	cp -p libsrc/scrt0.o ade/lib/scrt0.o && strip -g ade/lib/scrt0.o
	cp -p libsrc/libc.a ade/lib/libc.a && strip -g ade/lib/libc.a
	cp -p libsrc/libbc.a ade/lib/libb/libc.a && strip -g ade/lib/libb/libc.a
	cp -p libsrc/libb32c.a ade/lib/libb32/libm020/libc.a && \
	  strip -g ade/lib/libb32/libm020/libc.a
	cp -p libsrc/libc_p.a ade/lib/libc_p.a && strip -g 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)/README.pOS ixemul/README.pOS
	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-000t-<os>.lha
	#
	mkdir -p ade/Sys/libs
	cp -p library/68000/soft-float/amigaos/ixemul.trace ade/Sys/libs/ixemul000.trace
	cp -p $(srcdir)/README ade/Sys/libs/README
	cp -p $(srcdir)/README.pOS ade/Sys/libs/README.pOS
	(cd ade && lha -mraxeq a /distrib/ixemul-000t.lha Sys)
	rm -f ade/Sys/libs/*
	cp -p library/68000/soft-float/pos/ixemul.trace ade/Sys/libs/ixemul000-pos.trace
	cp -p $(srcdir)/README ade/Sys/libs/README
	cp -p $(srcdir)/README.pOS ade/Sys/libs/README.pOS
	(cd ade && lha -mraxeq a /distrib/ixemul-000t-pos.lha Sys)
	rm -f ade/Sys/libs/*
	sleep 5
	rm -rf ade
