# I prefer "handmade" make files, it is a little bit of more worktime to start with
# but I can read them, others can, and I know what they do!

BUILD_DIR_PURE := build.raspbian
BUILD_DIR := $(BUILD_DIR_PURE)/

PITREX_DIR := ../pitrex/pitrex/
VECTREX_DIR := ../pitrex/vectrex/
CFLAGS := -Ofast -I../pitrex/ -DPIZERO -DRPI0
CC := gcc

.PHONY: pitrexLib
.PHONY: dirCheck


all:	tailgunner

dirCheck:
	if [ ! -d $(BUILD_DIR_PURE) ]; then mkdir $(BUILD_DIR_PURE); fi

pitrexLib:
	$(MAKE) -C $(PITREX_DIR) -f Makefile.raspbian all
	$(MAKE) -C $(VECTREX_DIR) -f Makefile.raspbian all

translate/tailgunner-data.c:
	$(MAKE) -C ./translate -f Makefile tailgunner-data.c
#	cp ./translate/tailgunner-data.c ./tailgunner-data.i

$(BUILD_DIR)tailgunner.o: tailgunner.c translate/tailgunner-data.c
	$(CC) $(CFLAGS) -o $(BUILD_DIR)tailgunner.o -c tailgunner.c

tailgunr-ops.c: 
	$(MAKE) -C ./translate -f Makefile tailgunr-ops.c
	cp ./translate/tailgunr-ops.c ./tailgunr-ops.c

$(BUILD_DIR)ops.o: ops.c tailgunr-ops.c
	$(CC) $(CFLAGS) -o $(BUILD_DIR)ops.o -c ops.c

tailgunner: dirCheck pitrexLib $(BUILD_DIR)tailgunner.o $(BUILD_DIR)ops.o
	$(RM) tailgunner
	$(CC) $(CFLAGS) -o tailgunner \
	$(PITREX_DIR)$(BUILD_DIR)bcm2835.o \
	$(PITREX_DIR)$(BUILD_DIR)pitrexio-gpio.o \
	$(VECTREX_DIR)$(BUILD_DIR)vectrexInterface.o \
	$(VECTREX_DIR)$(BUILD_DIR)osWrapper.o \
	$(VECTREX_DIR)$(BUILD_DIR)baremetalUtil.o \
	$(BUILD_DIR)ops.o \
	$(BUILD_DIR)tailgunner.o

clean:
	$(RM) $(BUILD_DIR)*.* tailgunr-ops.c tailgunner-data.i tailgunner
	$(MAKE) -C ./translate -f Makefile clean

install:
	install -o root -g games -m 6555 tailgunner /opt/pitrex/bin
