NASM?=nasm
NASMFLAGS=-Ox -I ../../lmacros/ -I ../../scanptab/
CONVLIST?=convlist.pl
LZEXEDAT?=lzexedat.sh
HEATSHRINK?=heatshrink

ifeq ($(BUILDNAME),)
include ../tmp/lastname.dat
endif

ifeq ($(BUILDNAME),debug)
BUILDOPTIONS= 
else ifeq ($(BUILDNAME),debugx)
BUILDOPTIONS=-D_PM
else ifeq ($(BUILDNAME),ddebug)
BUILDOPTIONS=-D_DEBUG
else ifeq ($(BUILDNAME),ddebugx)
BUILDOPTIONS=-D_PM -D_DEBUG
else ifeq ($(BUILDNAME),cdebug)
BUILDOPTIONS=-D_DEBUG -D_DEBUG_COND
else ifeq ($(BUILDNAME),cdebugx)
BUILDOPTIONS=-D_PM -D_DEBUG -D_DEBUG_COND
else ifeq ($(BUILDNAME),oader)
BUILDOPTIONS=-D_LOADER
endif
OPTIONS=
DEPACKOPTIONS=
VERSIONOPTIONS?=
LINKOPTIONS?=

HELPSOURCES = $(wildcard help/*.asm)
HELPTXT = $(patsubst help/%.asm,../tmp/$(BUILDNAME).txt/%.txt,$(HELPSOURCES))
HELPHLZ = $(patsubst help/%.asm,../tmp/$(BUILDNAME).txt/%.hlz,$(HELPSOURCES))
HELPHHS = $(patsubst help/%.asm,../tmp/$(BUILDNAME).txt/%.hhs,$(HELPSOURCES))

MACS =\
 debug.mac \
 sections.mac \
 amis.mac \
 eld.mac \
 install.mac \
 iniload.mac \
 mzheader.mac \
 isvariab.mac \
 386.mac \
 options.mac \
 extern.mac

OBJS =\
 ../tmp/$(BUILDNAME).obj/aa.obj \
 ../tmp/$(BUILDNAME).obj/amis.obj \
 ../tmp/$(BUILDNAME).obj/bb.obj \
 ../tmp/$(BUILDNAME).obj/boot.obj \
 ../tmp/$(BUILDNAME).obj/cc.obj \
 ../tmp/$(BUILDNAME).obj/dd.obj \
 ../tmp/$(BUILDNAME).obj/debug.obj \
 ../tmp/$(BUILDNAME).obj/ee.obj \
 ../tmp/$(BUILDNAME).obj/expr.obj \
 ../tmp/$(BUILDNAME).obj/ff.obj \
 ../tmp/$(BUILDNAME).obj/fptostr.obj \
 ../tmp/$(BUILDNAME).obj/helpdepk.obj \
 ../tmp/$(BUILDNAME).obj/hh.obj \
 ../tmp/$(BUILDNAME).obj/iioo.obj \
 ../tmp/$(BUILDNAME).obj/immasm.obj \
 ../tmp/$(BUILDNAME).obj/init.obj \
 ../tmp/$(BUILDNAME).obj/install.obj \
 ../tmp/$(BUILDNAME).obj/ints.obj \
 ../tmp/$(BUILDNAME).obj/lineio.obj \
 ../tmp/$(BUILDNAME).obj/ll.obj \
 ../tmp/$(BUILDNAME).obj/mm.obj \
 ../tmp/$(BUILDNAME).obj/msg.obj \
 ../tmp/$(BUILDNAME).obj/pmdata.obj \
 ../tmp/$(BUILDNAME).obj/pmentry.obj \
 ../tmp/$(BUILDNAME).obj/pminit.obj \
 ../tmp/$(BUILDNAME).obj/rr.obj \
 ../tmp/$(BUILDNAME).obj/run.obj \
 ../tmp/$(BUILDNAME).obj/serialp.obj \
 ../tmp/$(BUILDNAME).obj/ss.obj \
 ../tmp/$(BUILDNAME).obj/symbols.obj \
 ../tmp/$(BUILDNAME).obj/uu.obj \
 ../tmp/$(BUILDNAME).obj/vv.obj \
 ../tmp/$(BUILDNAME).obj/ww.obj

all: ../lst/$(BUILDNAME)l.tls

.SUFFIXES: .big .obj .asm .txt .hlz .hhs

../tmp/$(BUILDNAME).obj:
	mkdir -p ../tmp/$(BUILDNAME).obj

../tmp/$(BUILDNAME).txt:
	mkdir -p ../tmp/$(BUILDNAME).txt

../lst/$(BUILDNAME).obj:
	mkdir -p ../lst/$(BUILDNAME).obj

include ../tmp/$(BUILDNAME).obj/option.dat
include ../tmp/$(BUILDNAME).obj/optionde.dat
include ../tmp/$(BUILDNAME).txt/optionlz.dat
include ../tmp/$(BUILDNAME).txt/optionhs.dat

ifeq ($(EXTERNALHELPTARGET),)
include ../tmp/$(BUILDNAME).obj/exthelp.dat
endif

../tmp/lastname.dat:
	echo "BUILDNAME=debug" > $@

../tmp/$(BUILDNAME).obj/exthelp.dat: | ../tmp/$(BUILDNAME).obj
	echo "EXTERNALHELPTARGET=none" > $@

../tmp/$(BUILDNAME).obj/option.dat: | ../tmp/$(BUILDNAME).obj
	echo "OPTIONS=" > $@
	echo "BUILDOPTIONS=" >> $@

../tmp/$(BUILDNAME).obj/optionde.dat: | ../tmp/$(BUILDNAME).obj
	echo "DEPACKOPTIONS=" > $@

../tmp/$(BUILDNAME).txt/optionlz.dat: | ../tmp/$(BUILDNAME).txt
	echo "LZEXEDAT_L_SWITCH=" > $@

../tmp/$(BUILDNAME).txt/optionhs.dat: | ../tmp/$(BUILDNAME).txt
	echo "HEATSHRINK_Z_SWITCH=" > $@

../tmp/$(BUILDNAME).obj/version.mac: FORCE | ../tmp/$(BUILDNAME).obj
	@$(NASM) $(NASMFLAGS) $(BUILDOPTIONS) $(OPTIONS) \
	  $(VERSIONOPTIONS) version.asm \
	  -o ../tmp/$(BUILDNAME).obj/version.new -fbin
	@diff -Nq ../tmp/$(BUILDNAME).obj/version.mac ../tmp/$(BUILDNAME).obj/version.new \
	  || cp -a ../tmp/$(BUILDNAME).obj/version.new ../tmp/$(BUILDNAME).obj/version.mac

../tmp/$(BUILDNAME).obj/revision.mac: FORCE | ../tmp/$(BUILDNAME).obj
	@diff -Nq ../tmp/$(BUILDNAME).obj/revision.mac ../tmp/$(BUILDNAME)r.mac \
	  || cp -a ../tmp/$(BUILDNAME)r.mac ../tmp/$(BUILDNAME).obj/revision.mac

FORCE:

helptxt: $(HELPTXT)

helphlz: $(HELPHLZ)

helphhs: $(HELPHHS)

../tmp/$(BUILDNAME).txt/%.txt: help/%.asm $(MACS) \
 ../tmp/$(BUILDNAME).obj/option.dat | ../tmp/$(BUILDNAME).txt
	$(NASM) $(NASMFLAGS) $(BUILDOPTIONS) $(OPTIONS) \
		help/$*.asm -D_BUILDINGHELPONLY \
		-o ../tmp/$(BUILDNAME).txt/$*.txt -fbin

../tmp/$(BUILDNAME).txt/%.hlz: ../tmp/$(BUILDNAME).txt/%.txt \
 ../tmp/$(BUILDNAME).txt/optionlz.dat
	cd ..; $(LZEXEDAT) -4 $(LZEXEDAT_L_SWITCH) tmp/$(BUILDNAME).txt/$*.txt \
		tmp/$(BUILDNAME).txt/$*.hlz

../tmp/$(BUILDNAME).txt/%.hhs: ../tmp/$(BUILDNAME).txt/%.txt \
 ../tmp/$(BUILDNAME).txt/optionhs.dat
	cd ..; HEATSHRINK=$(HEATSHRINK) ./source/heatshr.sh \
		tmp/$(BUILDNAME).txt/$*.txt \
		tmp/$(BUILDNAME).txt/$* .hhs $(HEATSHRINK_Z_SWITCH)

../tmp/$(BUILDNAME).obj/%.obj: %.asm $(MACS) \
 ../tmp/$(BUILDNAME).obj/option.dat | ../tmp/$(BUILDNAME).obj ../lst/$(BUILDNAME).obj
	$(NASM) -DVERSIONMAC="'../tmp/$(BUILDNAME).obj/version.mac'" \
		-DREVISIONMAC="'../tmp/$(BUILDNAME).obj/revision.mac'" \
		 $(NASMFLAGS) $(BUILDOPTIONS) $(DEPACKOPTIONS) $(OPTIONS) \
		$*.asm -l ../lst/$(BUILDNAME).obj/$*.lst \
		-o ../tmp/$(BUILDNAME).obj/$*.obj -fobj \
		-I ../tmp/$(BUILDNAME).txt/

../tmp/$(BUILDNAME).obj/amis.obj: ../tmp/$(BUILDNAME).obj/version.mac
../tmp/$(BUILDNAME).obj/init.obj: ../tmp/$(BUILDNAME).obj/version.mac
../tmp/$(BUILDNAME).obj/msg.obj: ../tmp/$(BUILDNAME).obj/version.mac

../tmp/$(BUILDNAME).obj/msg.obj: ../tmp/$(BUILDNAME).obj/revision.mac

../tmp/$(BUILDNAME).obj/debug.obj: ../tmp/$(BUILDNAME).obj/optionde.dat
../tmp/$(BUILDNAME).obj/helpdepk.obj: ../tmp/$(BUILDNAME).obj/optionde.dat
../tmp/$(BUILDNAME).obj/init.obj: ../tmp/$(BUILDNAME).obj/optionde.dat
../tmp/$(BUILDNAME).obj/lineio.obj: ../tmp/$(BUILDNAME).obj/optionde.dat
../tmp/$(BUILDNAME).obj/msg.obj: ../tmp/$(BUILDNAME).obj/optionde.dat

ifeq ($(EXTERNALHELPTARGET),none)
../tmp/$(BUILDNAME).obj/msg.obj: $(HELPSOURCES)
else ifeq ($(EXTERNALHELPTARGET),helptxt)
../tmp/$(BUILDNAME).obj/msg.obj: $(HELPTXT)
else ifeq ($(EXTERNALHELPTARGET),helphlz)
../tmp/$(BUILDNAME).obj/msg.obj: $(HELPHLZ)
else ifeq ($(EXTERNALHELPTARGET),helphhs)
../tmp/$(BUILDNAME).obj/msg.obj: $(HELPHHS)
endif

../tmp/$(BUILDNAME).obj/msg.obj: help/bootdisc.asm
../tmp/$(BUILDNAME).obj/aa.obj: asmops.mac asmtabs.asm debugtbl.inc
../tmp/$(BUILDNAME).obj/boot.obj: ../../scanptab/scanptab.asm
../tmp/$(BUILDNAME).obj/dd.obj: dishared.asm dmshared.asm dxshared.asm
../tmp/$(BUILDNAME).obj/debug.obj: patch.mac asmops.mac deb.asm xxshared.asm
../tmp/$(BUILDNAME).obj/expr.obj: deb.asm exprops.mac
../tmp/$(BUILDNAME).obj/iioo.obj: if.asm
../tmp/$(BUILDNAME).obj/init.obj: deb.asm pathshar.asm help/bootdisc.asm isstring.asm
../tmp/$(BUILDNAME).obj/lineio.obj: isstring.asm reclaims.asm loadeld.asm
../tmp/$(BUILDNAME).obj/msg.obj: linkinfo.asm nasmhash.mac dimsg.asm dmmsg.asm xxmsg.asm
../tmp/$(BUILDNAME).obj/rr.obj: exprops.mac rnshared.asm rmshared.asm
../tmp/$(BUILDNAME).obj/ss.obj: ssshared.asm
../tmp/$(BUILDNAME).obj/uu.obj: asmops.mac

../tmp/$(BUILDNAME).obj/debug.rsp: template.rsp \
 | ../tmp/$(BUILDNAME).obj
	cp -a template.rsp ../tmp/$(BUILDNAME).obj/debug.rsp
	perl -i -pe "s/OBJPATH/$(BUILDNAME).obj/" ../tmp/$(BUILDNAME).obj/debug.rsp

../tmp/$(BUILDNAME)l.big: $(OBJS) ../tmp/$(BUILDNAME).obj/debug.rsp
	cd ..; ./source/warplink.sh $(LINKOPTIONS) @tmp/$(BUILDNAME).obj/debug.rsp,tmp/$(BUILDNAME)l.big,lst/$(BUILDNAME)l.map\; || (rm -f tmp/$(BUILDNAME)l.big && false)

../lst/$(BUILDNAME)l.tls: ../tmp/$(BUILDNAME)l.big
	@echo === Creating trace listing
	@$(CONVLIST) ../lst/$(BUILDNAME)l.map \
	  $$(cat ../tmp/$(BUILDNAME).obj/debug.rsp | \
	    perl -ne ' \
	      if (/(\S+\.obj)/) { \
	        $$_ = $$1; \
	        s/\\/\//g; \
	        s/^tmp\//..\/lst\//; \
	        s/\.obj$$/\.lst/; \
	        print "$$_\n"; \
	      }' ) > ../lst/$(BUILDNAME)l.tls
