##==============================================================================
## Makefile für TriOS-R57
##------------------------------------------------------------------------------
## TODO:
##
## - Umbenennung BINS -> TARGETS.
## - Eventuell etwas Luft zwischen den Ausgaben einfuegen.
## - "make clean".
## - "make dist" oder aehnlich.
##
## Lieber nicht:
##
## - "make flash_props" ist aufgrund des umschalten Muessens am Maeuseklavier
##   keine große Erleichterung.
## - "make copy_to_SD" moechte ich nicht bauen, das Device der SD-Karte kann zig
##   verschiedene Namen auf den verschiedenen Systemen haben.
##==============================================================================
## Pfade
##
SD	= bin/sdcard
SDSYS	= bin/sdcard/system
FLASH	= bin/flash
LIB	= lib
BSTC	= ./bstc.linux

##
## Laessig oder pingelig?
##
## Laessig: Aenderungen im Makefile sollen kein Neubauen veranlassen
## Pingelig: Geaendertes Makefile als Abhängigkeit erzwingt Neu-Bau
##
#DEPS	=
DEPS	= Makefile

##
## Alles, was zu bearbeiten ist, wird inkrementell in dieser Variable gesammelt.
##
BINS	=

##------------------------------------------------------------------------------
## Zeit am Anfang und am Ende ausgeben
##

##
## HHGTTG 1,25:
## (41) Deep Thought paused for a moment's reflection.
## (42) ''Tricky,'' he said finally.
##

all: do_it
	date '+END %F %T %Z'

start:
	date '+START %F %T %Z'

##------------------------------------------------------------------------------
## Alte Versionen loeschen
##
## del ".\bin\flash"\*.* /Q
## del ".\bin\sdcard"\*.* /Q
## del ".\bin\sdcard\system"\*.* /Q

##
## Am Start alte Versionen loeschen wuerde den Zweck von make untergraben.
## Als "make clean" wird es aber später wieder auferstehen. (yeti@20170522)
##

#-------------------------------------------------------------------------------
## Flashdateien erzeugen
## --> bin/flash

##
## Das "mv" könnte in manchen Regeln durch BSTCs "-o"-Option ersetzt werden,
## aber das greift nur ohne Klimmzüge, solange die Extension ".binary" ist und
## da im Folgenden auch andere Extensions gebraucht werden, meide ich "-o" in
## jedem der auftauchenden Kontexte, um den Regeln einen gemeinsamen Stil und
## Aufbau zu erhalten. (yeti@20170522)
##

BINS += $(FLASH)/admflash.binary

$(FLASH)/admflash.binary: flash/administra/admflash.spin \
	$(DEPS) \
	$(LIB)/adm-fat.spin \
	$(LIB)/adm-rtc.spin \
	$(LIB)/adm-hss.spin \
	$(LIB)/adm-wav.spin \
	$(LIB)/adm-rtc.spin \
	$(LIB)/adm-com.spin \
	$(LIB)/adm-plx.spin \
	$(LIB)/glob-con.spin
	$(BSTC) -L $(LIB) -b -O a flash/administra/admflash.spin
	mv admflash.binary $@

BINS += $(SDSYS)/admsys.adm

$(SDSYS)/admsys.adm: $(FLASH)/admflash.binary
	cp $< $@

BINS += $(FLASH)/belflash.binary

$(FLASH)/belflash.binary: flash/bellatrix/belflash.spin \
	$(DEPS) \
	flash/bellatrix/belf-vga.spin \
	$(LIB)/bel-vga.spin \
	$(LIB)/bel-keyb.spin \
	$(LIB)/bel-bus.spin \
	$(LIB)/glob-con.spin
	$(BSTC) -L $(LIB) -D __VGA -b -O a flash/bellatrix/belflash.spin
	mv belflash.binary $(FLASH)/belflash.binary

BINS += $(SDSYS)/vga.bel

$(SDSYS)/vga.bel: $(FLASH)/belflash.binary
	cp $< $@

##
## Hier ausnahmsweise ein "-o", damit nicht bei Parallelverarbeitung der selbe
## Name (belflash.binary) wie in der vorherigen Regel entstehen kann.
## (yeti@20170523)
##
BINS += $(SDSYS)/tv.bel

$(SDSYS)/tv.bel: flash/bellatrix/belflash.spin \
	$(DEPS) \
	flash/bellatrix/belf-tv.spin \
	$(LIB)/bel-tv.spin \
	$(LIB)/bel-keyb.spin \
	$(LIB)/bel-bus.spin \
	$(LIB)/glob-con.spin
	$(BSTC) -L $(LIB) -D __TV -b -O a -o tv flash/bellatrix/belflash.spin
	mv tv.binary $@

BINS += $(FLASH)/regflash.binary

$(FLASH)/regflash.binary: flash/regnatix/regflash.spin \
	$(DEPS)
	$(BSTC) -L $(LIB) -b -O a flash/regnatix/regflash.spin
	mv regflash.binary $@

##------------------------------------------------------------------------------
## Startdateien erzeugen
## reg.sys	(Regime)
## --> bin/sdcard

BINS += $(SDSYS)/regime.bin

$(SDSYS)/regime.bin: system/regnatix/regime.spin \
	$(DEPS) \
	$(LIB)/reg-ios.spin \
	$(LIB)/glob-con.spin \
	$(LIB)/glob-string.spin
	$(BSTC) -L $(LIB) -b -O a system/regnatix/regime.spin
	mv regime.binary $(SDSYS)/regime.bin

BINS += $(SD)/reg.sys

$(SD)/reg.sys: $(SDSYS)/regime.bin
	cp $< $@

##------------------------------------------------------------------------------
## Slave-Dateien erzeugen
## admsid, admay, admterm
## htxt, g0key

BINS += $(SDSYS)/admsid.adm

$(SDSYS)/admsid.adm: system/administra/admsid/admsid.spin \
	$(DEPS) \
	$(LIB)/adm-fat.spin \
	$(LIB)/adm-rtc.spin \
	$(LIB)/adm-sid.spin \
	$(LIB)/glob-con.spin
	$(BSTC) -L $(LIB) -b -O a system/administra/admsid/admsid.spin
	mv admsid.binary $@

BINS += $(SDSYS)/admay.adm

$(SDSYS)/admay.adm: system/administra/admay/admay.spin \
	$(DEPS) \
	$(LIB)/adm-fat.spin \
	$(LIB)/adm-rtc.spin \
	$(LIB)/adm-ay.spin \
	$(LIB)/glob-con.spin
	$(BSTC) -L $(LIB) -b -O a system/administra/admay/admay.spin
	mv admay.binary $@

BINS += $(SDSYS)/admnet.adm

$(SDSYS)/admnet.adm: system/administra/admnet/admnet.spin \
	$(DEPS) \
	$(LIB)/adm-fat.spin \
	$(LIB)/adm-rtc.spin \
	$(LIB)/adm-hss.spin \
	$(LIB)/adm-rtc.spin \
	$(LIB)/adm-com.spin \
	$(LIB)/adm-socket.spin \
	$(LIB)/adm-enc28j60.spin \
	$(LIB)/glob-con.spin \
	$(LIB)/glob-numbers.spin
	$(BSTC) -L $(LIB) -b -O a system/administra/admnet/admnet.spin
	mv admnet.binary $@

BINS += $(SDSYS)/g0key.bel

$(SDSYS)/g0key.bel: system/bellatrix/bel-g0/g0key.spin \
	$(DEPS) \
	$(LIB)/bel-tv.spin \
	$(LIB)/glob-con.spin
	$(BSTC) -L $(LIB) -b -O a system/bellatrix/bel-g0/g0key.spin
	mv g0key.binary $@

BINS += $(SDSYS)/htext.bel

$(SDSYS)/htext.bel: system/bellatrix/bel-htext/htext.spin \
	$(DEPS) \
	$(LIB)/bel-htext.spin \
	$(LIB)/bel-keyb.spin
	$(BSTC) -L $(LIB) -b -O a system/bellatrix/bel-htext/htext.spin
	mv htext.binary $@

##-------------------------------------------------------------------------------
## Systemdateien erzeugen
## - div. externe Kommandos
## - div. Systemdateien (Farbtabellen usw.) 
## --> bin/sdcard/system

##
## Die hier ursprünglich im Original eingesetzte for-Schleife laesst sich nicht
## auf ein Makefile abbilden, da sich die einzelnen Binaries in ihren
## Abhaengigkeiten (inclusive Hauptprogramm, das offensichtlich ueberall anders
## ist) unterscheiden. Es bleibt also nur, jedem Programm eine eigene
## Makefile-Regel zu basteln. (yeti@20170522)
##

BINS += $(SDSYS)/admtest.bin

$(SDSYS)/admtest.bin: system/regnatix/admtest.spin \
	$(DEPS) \
	$(LIB)/reg-ios.spin \
	$(LIB)/glob-con.spin \
	$(LIB)/glob-string.spin
	$(BSTC) -L $(LIB) -b -O a system/regnatix/admtest.spin
	mv admtest.binary $@

BINS += $(SDSYS)/beltest.bin

$(SDSYS)/beltest.bin: system/regnatix/beltest.spin \
	$(DEPS) \
	$(LIB)/reg-ios.spin \
	$(LIB)/glob-con.spin \
	$(LIB)/glob-string.spin
	$(BSTC) -L $(LIB) -b -O a system/regnatix/beltest.spin
	mv beltest.binary $@

BINS += $(SDSYS)/charmap.bin

$(SDSYS)/charmap.bin: system/regnatix/charmap.spin \
	$(DEPS) \
	$(LIB)/reg-ios.spin \
	$(LIB)/glob-con.spin
	$(BSTC) -L $(LIB) -b -O a system/regnatix/charmap.spin
	mv charmap.binary $@

BINS += $(SDSYS)/eram.bin

$(SDSYS)/eram.bin: system/regnatix/eram.spin \
	$(DEPS) \
	$(LIB)/glob-numbers.spin \
	$(LIB)/reg-ios.spin \
	$(LIB)/glob-con.spin
	$(BSTC) -L $(LIB) -b -O a system/regnatix/eram.spin
	mv eram.binary $@

BINS += $(SDSYS)/flash.bin

$(SDSYS)/flash.bin: system/regnatix/flash.spin \
	$(DEPS) \
	$(LIB)/reg-ios.spin \
	$(LIB)/glob-con.spin \
	$(LIB)/glob-sdspi.spin \
	$(LIB)/glob-numbers.spin
	$(BSTC) -L $(LIB) -b -O a system/regnatix/flash.spin
	mv flash.binary $@

BINS += $(SDSYS)/fm.bin

$(SDSYS)/fm.bin: system/regnatix/fm.spin \
	$(DEPS) \
	$(LIB)/reg-ios.spin \
	$(LIB)/glob-con.spin \
	$(LIB)/gui-dlbox.spin \
	$(LIB)/fm-con.spin \
	$(LIB)/gui-pbar.spin \
	$(LIB)/gui-wbox.spin \
	$(LIB)/gui-input.spin \
	$(LIB)/glob-string.spin
	$(BSTC) -L $(LIB) -b -O a system/regnatix/fm.spin
	mv fm.binary $@

BINS += $(SDSYS)/fterm.bin

$(SDSYS)/fterm.bin: system/regnatix/fterm.spin \
	$(DEPS) \
	$(LIB)/reg-ios.spin \
	$(LIB)/glob-con.spin \
	$(LIB)/glob-fds.spin
	$(BSTC) -L $(LIB) -b -O a system/regnatix/fterm.spin
	mv fterm.binary $@

BINS += $(SDSYS)/ftp.bin

$(SDSYS)/ftp.bin: system/regnatix/ftp.spin \
	$(DEPS) \
	$(LIB)/reg-ios.spin \
	$(LIB)/glob-con.spin \
	$(LIB)/glob-string.spin \
	$(LIB)/glob-numbers.spin
	$(BSTC) -L $(LIB) -b -O a system/regnatix/ftp.spin
	mv ftp.binary $@

BINS += $(SDSYS)/g0test.bin

$(SDSYS)/g0test.bin: system/regnatix/g0test.spin \
	$(DEPS) \
	$(LIB)/reg-ios.spin \
	$(LIB)/glob-con.spin
	$(BSTC) -L $(LIB) -b -O a system/regnatix/g0test.spin
	mv g0test.binary $@

BINS += $(SDSYS)/hplay.bin

$(SDSYS)/hplay.bin: system/regnatix/hplay.spin \
	$(DEPS) \
	$(LIB)/reg-ios.spin \
	$(LIB)/glob-con.spin
	$(BSTC) -L $(LIB) -b -O a system/regnatix/hplay.spin
	mv hplay.binary $@

BINS += $(SDSYS)/ipconfig.bin

$(SDSYS)/ipconfig.bin: system/regnatix/ipconfig.spin \
	$(DEPS) \
	$(LIB)/reg-ios.spin \
	$(LIB)/glob-con.spin \
	$(LIB)/glob-string.spin \
	$(LIB)/glob-numbers.spin
	$(BSTC) -L $(LIB) -b -O a system/regnatix/ipconfig.spin
	mv ipconfig.binary $@

BINS += $(SDSYS)/irc.bin

$(SDSYS)/irc.bin: system/regnatix/irc.spin \
	$(DEPS) \
	$(LIB)/reg-ios.spin \
	$(LIB)/glob-con.spin \
	$(LIB)/glob-string.spin \
	$(LIB)/glob-numbers.spin \
	$(LIB)/glob-led-engine.spin
	$(BSTC) -L $(LIB) -b -O a system/regnatix/irc.spin
	mv irc.binary $@

BINS += $(SDSYS)/keycode.bin

$(SDSYS)/keycode.bin: system/regnatix/keycode.spin \
	$(DEPS) \
	$(LIB)/reg-ios.spin \
	$(LIB)/glob-con.spin
	$(BSTC) -L $(LIB) -b -O a system/regnatix/keycode.spin
	mv keycode.binary $@

BINS += $(SDSYS)/m.bin

$(SDSYS)/m.bin: system/regnatix/m.spin \
	$(DEPS) \
	$(LIB)/reg-ios.spin \
	$(LIB)/glob-con.spin \
	$(LIB)/m-glob-con.spin
	$(BSTC) -L $(LIB) -b -O a system/regnatix/m.spin
	mv m.binary $@

BINS += $(SDSYS)/man.bin

$(SDSYS)/man.bin: system/regnatix/man.spin \
	$(DEPS) \
	$(LIB)/reg-ios.spin \
	$(LIB)/glob-con.spin \
	$(LIB)/glob-string.spin
	$(BSTC) -L $(LIB) -b -O a system/regnatix/man.spin
	mv man.binary $@

BINS += $(SDSYS)/perplex.bin

$(SDSYS)/perplex.bin: system/regnatix/perplex.spin \
	$(DEPS) \
	$(LIB)/reg-ios.spin \
	$(LIB)/glob-con.spin \
	$(LIB)/glob-numbers.spin
	$(BSTC) -L $(LIB) -b -O a system/regnatix/perplex.spin
	mv perplex.binary $@

BINS += $(SDSYS)/ramtest.bin

$(SDSYS)/ramtest.bin: system/regnatix/ramtest.spin \
	$(DEPS) \
	$(LIB)/reg-ios.spin \
	$(LIB)/glob-con.spin
	$(BSTC) -L $(LIB) -b -O a system/regnatix/ramtest.spin
	mv ramtest.binary $@

BINS += $(SDSYS)/rom.bin

$(SDSYS)/rom.bin: system/regnatix/rom.spin \
	$(DEPS) \
	$(LIB)/reg-ios.spin \
	$(LIB)/glob-con.spin \
	$(LIB)/glob-sdspi.spin
	$(BSTC) -L $(LIB) -b -O a system/regnatix/rom.spin
	mv rom.binary $@

BINS += $(SDSYS)/sfxtool.bin

$(SDSYS)/sfxtool.bin: system/regnatix/sfxtool.spin \
	$(DEPS) \
	$(LIB)/reg-ios.spin \
	$(LIB)/glob-con.spin \
	$(LIB)/glob-numbers.spin \
	$(LIB)/glob-string.spin
	$(BSTC) -L $(LIB) -b -O a system/regnatix/sfxtool.spin
	mv sfxtool.binary $@

BINS += $(SDSYS)/splay.bin

$(SDSYS)/splay.bin: system/regnatix/splay.spin \
	$(DEPS) \
	$(LIB)/reg-ios.spin \
	$(LIB)/glob-con.spin
	$(BSTC) -L $(LIB) -b -O a system/regnatix/splay.spin
	mv splay.binary $@

BINS += $(SDSYS)/sysconf.bin

$(SDSYS)/sysconf.bin: system/regnatix/sysconf.spin \
	$(DEPS) \
	$(LIB)/reg-ios.spin \
	$(LIB)/glob-con.spin
	$(BSTC) -L $(LIB) -b -O a system/regnatix/sysconf.spin
	mv sysconf.binary $@

BINS += $(SDSYS)/tapecut.bin

$(SDSYS)/tapecut.bin: system/regnatix/tapecut.spin \
	$(DEPS) \
	$(LIB)/reg-ios.spin \
	$(LIB)/glob-con.spin \
	$(LIB)/glob-numbers.spin \
	$(LIB)/m-glob-con.spin
	$(BSTC) -L $(LIB) -b -O a system/regnatix/tapecut.spin
	mv tapecut.binary $@

BINS += $(SDSYS)/time.bin

$(SDSYS)/time.bin: system/regnatix/time.spin \
	$(DEPS) \
	$(LIB)/reg-ios.spin \
	$(LIB)/glob-con.spin \
	$(LIB)/glob-string.spin \
	$(LIB)/glob-numbers.spin
	$(BSTC) -L $(LIB) -b -O a system/regnatix/time.spin
	mv time.binary $@

BINS += $(SDSYS)/wplay.bin

$(SDSYS)/wplay.bin: system/regnatix/wplay.spin \
	$(DEPS) \
	$(LIB)/reg-ios.spin \
	$(LIB)/glob-con.spin
	$(BSTC) -L $(LIB) -b -O a system/regnatix/wplay.spin
	mv wplay.binary $@

BINS += $(SDSYS)/yplay.bin

$(SDSYS)/yplay.bin: system/regnatix/yplay.spin \
	$(DEPS) \
	$(LIB)/reg-ios.spin \
	$(LIB)/glob-con.spin
	$(BSTC) -L $(LIB) -b -O a system/regnatix/yplay.spin
	mv yplay.binary $@

##
## TODO: nach z.B. Extension sortieren
##

BINS += $(SDSYS)/adm.lib

$(SDSYS)/adm.lib: forth/adm.lib $(DEPS)
	cp $< $@

BINS += $(SDSYS)/ari.lib

$(SDSYS)/ari.lib: forth/ari.lib $(DEPS)
	cp $< $@

BINS += $(SDSYS)/basics.mod

$(SDSYS)/basics.mod: forth/basics.mod $(DEPS)
	cp $< $@

BINS += $(SDSYS)/bel.lib

$(SDSYS)/bel.lib: forth/bel.lib $(DEPS)
	cp $< $@

BINS += $(SDSYS)/cog.lib

$(SDSYS)/cog.lib: forth/cog.lib $(DEPS)
	cp $< $@

BINS += $(SDSYS)/debug.mod

$(SDSYS)/debug.mod: forth/debug.mod $(DEPS)
	cp $< $@

BINS += $(SDSYS)/fib.mod

$(SDSYS)/fib.mod: forth/fib.mod $(DEPS)
	cp $< $@

BINS += $(SDSYS)/g0.lib

$(SDSYS)/g0.lib: forth/g0.lib $(DEPS)
	cp $< $@

BINS += $(SDSYS)/hplay.mod

$(SDSYS)/hplay.mod: forth/hplay.mod $(DEPS)
	cp $< $@

BINS += $(SDSYS)/hss.lib

$(SDSYS)/hss.lib: forth/hss.lib $(DEPS)
	cp $< $@

BINS += $(SDSYS)/key.lib

$(SDSYS)/key.lib: forth/key.lib $(DEPS)
	cp $< $@

BINS += $(SDSYS)/rom.mod

$(SDSYS)/rom.mod: forth/rom.mod $(DEPS)
	cp $< $@

BINS += $(SDSYS)/scr.lib

$(SDSYS)/scr.lib: forth/scr.lib $(DEPS)
	cp $< $@

BINS += $(SDSYS)/sd0.lib

$(SDSYS)/sd0.lib: forth/sd0.lib $(DEPS)
	cp $< $@

BINS += $(SDSYS)/sfx.lib

$(SDSYS)/sfx.lib: forth/sfx.lib $(DEPS)
	cp $< $@

BINS += $(SDSYS)/splay.mod

$(SDSYS)/splay.mod: forth/splay.mod $(DEPS)
	cp $< $@

BINS += $(SDSYS)/tools.mod

$(SDSYS)/tools.mod: forth/tools.mod $(DEPS)
	cp $< $@

BINS += $(SDSYS)/tpix.f

$(SDSYS)/tpix.f: forth/tpix.f $(DEPS)
	cp $< $@

BINS += $(SDSYS)/v1.mod

$(SDSYS)/v1.mod: forth/v1.mod $(DEPS)
	cp $< $@

BINS += $(SDSYS)/wav.lib

$(SDSYS)/wav.lib: forth/wav.lib $(DEPS)
	cp $< $@

BINS += $(SDSYS)/win.lib

$(SDSYS)/win.lib: forth/win.lib $(DEPS)
	cp $< $@

BINS += $(SDSYS)/words.mod

$(SDSYS)/words.mod: forth/words.mod $(DEPS)
	cp $< $@

BINS += $(SDSYS)/wplay.mod

$(SDSYS)/wplay.mod: forth/wplay.mod $(DEPS)
	cp $< $@

##
## COL-Dateien
##

BINS += $(SDSYS)/BLUE.COL

$(SDSYS)/BLUE.COL: system/sonstiges/BLUE.COL $(DEPS)
	cp $< $@

BINS += $(SDSYS)/BW.COL

$(SDSYS)/BW.COL: system/sonstiges/BW.COL $(DEPS)
	cp $< $@

BINS += $(SDSYS)/BWHALF.COL

$(SDSYS)/BWHALF.COL: system/sonstiges/BWHALF.COL $(DEPS)
	cp $< $@

BINS += $(SDSYS)/CHESS.COL

$(SDSYS)/CHESS.COL: system/sonstiges/CHESS.COL $(DEPS)
	cp $< $@

BINS += $(SDSYS)/GREEN.COL

$(SDSYS)/GREEN.COL: system/sonstiges/GREEN.COL $(DEPS)
	cp $< $@

BINS += $(SDSYS)/PAR.COL

$(SDSYS)/PAR.COL: system/sonstiges/PAR.COL $(DEPS)
	cp $< $@

##
## man-Dateien
##

BINS += $(SDSYS)/eram.man

$(SDSYS)/eram.man: system/sonstiges/eram.man $(DEPS)
	cp $< $@

BINS += $(SDSYS)/error.man

$(SDSYS)/error.man: system/sonstiges/error.man $(DEPS)
	cp $< $@

BINS += $(SDSYS)/flash.man

$(SDSYS)/flash.man: system/sonstiges/flash.man $(DEPS)
	cp $< $@

BINS += $(SDSYS)/fm.man

$(SDSYS)/fm.man: system/sonstiges/fm.man $(DEPS)
	cp $< $@

BINS += $(SDSYS)/ftp.man

$(SDSYS)/ftp.man: system/sonstiges/ftp.man $(DEPS)
	cp $< $@

BINS += $(SDSYS)/hplay.man

$(SDSYS)/hplay.man: system/sonstiges/hplay.man $(DEPS)
	cp $< $@

BINS += $(SDSYS)/regime.man

$(SDSYS)/regime.man: system/sonstiges/regime.man $(DEPS)
	cp $< $@

BINS += $(SDSYS)/splay.man

$(SDSYS)/splay.man: system/sonstiges/splay.man $(DEPS)
	cp $< $@

BINS += $(SDSYS)/sysconf.man

$(SDSYS)/sysconf.man: system/sonstiges/sysconf.man $(DEPS)
	cp $< $@

BINS += $(SDSYS)/wplay.man

$(SDSYS)/wplay.man: system/sonstiges/wplay.man $(DEPS)
	cp $< $@

BINS += $(SDSYS)/yplay.man

$(SDSYS)/yplay.man: system/sonstiges/yplay.man $(DEPS)
	cp $< $@

##
## txt-Dateien
##

BINS += $(SDSYS)/test1.txt

$(SDSYS)/test1.txt: system/sonstiges/test1.txt $(DEPS)
	cp $< $@

BINS += $(SDSYS)/test2.txt

$(SDSYS)/test2.txt: system/sonstiges/test2.txt $(DEPS)
	cp $< $@

##------------------------------------------------------------------------------

do_it: start $(BINS)

##==============================================================================