-include      Makelocal.mk
PATH_SDCC     ?= /usr/bin
PATH_BIN      := bin
BUILD_ALL     := "Use 'make help' for possible targets."
BUILD_HELP    := "Use one of the following build targets;"
BUILD_VERSION ?= 1.0
BUILD_NAME    := omsxctl-$(BUILD_VERSION)

.SUFFIXES:
.PHONY: all
all:
	@echo $(BUILD_ALL)

BUILD_HELP += \\n\\t* help
.PHONY: help
help:
	@echo $(BUILD_HELP)

BUILD_HELP += \\n\\t* clean
.PHONY: clean
clean:
	rm bin -rf

bin/omsxctl:
	mkdir -p bin/omsxctl

bin/omsxctl/%.rel: src/%.asm | bin/omsxctl
	$(PATH_SDCC)/sdasz80 -g -l -c -o $@ $<

bin/omsxctl/omsxctl.hex: bin/omsxctl/omsxctl.rel
	$(PATH_SDCC)/sdcc -mz80 --no-std-crt0 --code-loc 0x0100 -o $@ $<

BUILD_HELP += \\n\\t* bin/omsxctl/omsxctl.com
bin/omsxctl/omsxctl.com: bin/omsxctl/omsxctl.hex
	$(PATH_SDCC)/sdobjcopy -I ihex --output-target=binary $< $@

bin/dist:
	mkdir -p bin/dist
	
bin/dist/$(BUILD_NAME): | bin/dist
	mkdir -p bin/dist/$(BUILD_NAME)

bin/dist/$(BUILD_NAME)/omsxctl.com: bin/dist/$(BUILD_NAME) | bin/omsxctl/omsxctl.com
	cp bin/omsxctl/omsxctl.com bin/dist/$(BUILD_NAME)/omsxctl.com

bin/dist/$(BUILD_NAME)/omsxctl.tcl: bin/dist/$(BUILD_NAME)
	cp src/omsxctl.tcl bin/dist/$(BUILD_NAME)/omsxctl.tcl

bin/dist/$(BUILD_NAME)/omsxctl.asm: bin/dist/$(BUILD_NAME)
	cp src/omsxctl.asm bin/dist/$(BUILD_NAME)/omsxctl.asm

bin/dist/$(BUILD_NAME)/usage.txt: bin/dist/$(BUILD_NAME)
	echo "# Usage\r" > $@
	echo "\r" >> $@
	echo "Pass the tcl script to openMSX on the command line:\r" >> $@
	echo "    openmsx -script <path-to>/omsxctl.tcl\r" >> $@
	echo "\r" >> $@
	echo "And then in MSX-DOS execute something like:\r" >> $@
	echo "    omsxctl set throttle off\r" >> $@
	echo "    ...\r" >> $@
	echo "    omsxctl exit\r" >> $@
	echo "\r" >> $@

BUILD_HELP += \\n\\t* bin/dist/$(BUILD_NAME).tar.gz
bin/dist/$(BUILD_NAME).tar.gz: \
bin/dist/$(BUILD_NAME)/omsxctl.com \
bin/dist/$(BUILD_NAME)/omsxctl.tcl \
bin/dist/$(BUILD_NAME)/omsxctl.asm \
bin/dist/$(BUILD_NAME)/usage.txt
	cd bin/dist && tar -czvf $(BUILD_NAME).tar.gz $(BUILD_NAME)