# Makefile for C*Base ML-files
# Copyright © 2003-2007 David Weinehall <tao@triad.se>

D64IMAGE1 := cbase_side1
D64IMAGE2 := cbase_side2
D64LABEL1 := cbase v3.3 side1
D64LABEL2 := cbase v3.3 side2
D64ID := 2007

XA := xa
C1541 := c1541
PETCAT := petcat
STRIP := grep -v "^;"
INSTALL1 := $(C1541) -attach $(D64IMAGE1).d64 -write
INSTALL2 := $(C1541) -attach $(D64IMAGE2).d64 -write
# Ignore ':' inside comments
XAFLAGS := -M
RM := rm -f
# CFLAGS += -W -Wall -Wshadow -Wpointer-arith -Wundef -Wcast-align
# CFLAGS += -Wbad-function-cast -Wwrite-strings -Wsign-compare
# CFLAGS += -Waggregate-return -Wmissing-noreturn -Wnested-externs
# CFLAGS += -Wchar-subscripts -Wformat-security -Wmissing-prototypes

MLFILES := \
	ml0123
NMIFILES := \
	nmi \
	tmnmi \
	tm2nmi
SWIFTFILE := \
	swift
PROTOCOLS := \
	punter \
	x-modem \
	x-modem1k
DATA := \
	ansitab \
	asciitab \
	sprite1 \
	sprite2 \
	sprite3 \
	sprite4
OBJS := $(DATA) $(MLFILES) $(NMIFILES) $(PROTOCOLS)
BLITZOBJS := \
	app_mod \
	bbs \
	toplister
CBASICOBJS := \
	boot \
	configure \
	sys_setup
BASICOBJS := \
	autostart \
	d9060_boot
INCLUDES := \
	labels.a65

.PHONY: default
default: all

mlsplit: %: %.c Makefile
	$(CC) $(CFLAGS) -o $@ $<

$(SWIFTFILE): %: %.a65 $(INCLUDES) Makefile
	@$(XA) $(XAFLAGS) -DBASED700 $< -o $@.d7			&&\
	$(XA) $(XAFLAGS) -DBASEDE00 $< -o $@.de				&&\
	$(XA) $(XAFLAGS) -DBASEDF00 $< -o $@.df

$(OBJS): %: %.a65 $(INCLUDES) Makefile
	@$(XA) $(XAFLAGS) $< -o $@

$(BLITZOBJS): %: %.bas
	$(STRIP) $< > $@.tmp						;\
	$(PETCAT) -w2 -o $@ $@.tmp					;\
	$(RM) $@.tmp

.PHONY: all
all: $(OBJS) $(BLITZOBJS) $(SWIFTFILE) mlsplit Makefile
	if [ ! -f $(D64IMAGE1).d64 ]; then				\
		$(C1541) -format "$(D64LABEL1),$(D64ID)" d64 $(D64IMAGE1).d64;\
	fi								&&\
	if [ ! -f $(D64IMAGE2).d64 ]; then				\
		$(C1541) -format "$(D64LABEL2),$(D64ID)" d64 $(D64IMAGE2).d64;\
	fi								&&\
	for file in $(BLITZOBJS); do					\
		version=`grep "^;version: " $$file.bas | sed -e "s/;version://"`;\
		name=`echo $$file | sed -e 's/_/ /g'`			;\
		$(INSTALL1) $$file "$$name$$version"			;\
	done								&&\
	for file in $(CBASICOBJS); do					\
		version=`grep "^;version: " $$file.bas | sed -e "s/;version://"`;\
		name=`echo $$file | sed -e 's/_/ /g'`			;\
		$(STRIP) $$file.bas > $$file.tmp			;\
		$(PETCAT) -w2 -o $$file $$file.tmp			;\
		$(RM) $$file.tmp					;\
		$(INSTALL1) $$file "c/$$name$$version"			;\
	done								&&\
	for file in $(BASICOBJS); do					\
		version=`grep "^;version: " $$file.bas | sed -e "s/;version://"`;\
		name=`echo $$file | sed -e 's/_/ /g'`			;\
		$(STRIP) $$file.bas > $$file.tmp			;\
		$(PETCAT) -w2 -o $$file $$file.tmp			;\
		$(RM) $$file.tmp					;\
		$(INSTALL1) $$file "$$name$$version"			;\
	done								&&\
	./mlsplit							&&\
	version=`grep "^;version0: " ml0123.a65 | sed -e "s/;version0: //"`&&\
	$(INSTALL2) ml0 "k/ml 0.o $$version" &&\
	version=`grep "^;version1: " ml0123.a65 | sed -e "s/;version1: //"`&&\
	$(INSTALL2) ml1 "k/ml 1.o $$version" &&\
	version=`grep "^;version2: " ml0123.a65 | sed -e "s/;version2: //"`&&\
	$(INSTALL2) ml2 "k/ml 2.o $$version" &&\
	version=`grep "^;version3: " ml0123.a65 | sed -e "s/;version3: //"`&&\
	$(INSTALL2) ml3 "k/ml 3.o $$version" &&\
	version=`grep "^;version: " nmi.a65 | sed -e "s/;version: //"`	&&\
	$(INSTALL2) nmi "k/nmi.ml $$version"				&&\
	version=`grep "^;version: " tmnmi.a65 | sed -e "s/;version: //;s/.*/&tm/"`	&&\
	$(INSTALL2) tmnmi "k/nmi.ml $$version"				&&\
	version=`grep "^;version: " tm2nmi.a65 | sed -e "s/;version: //;s/.*/&tm2/"`&&\
	$(INSTALL2) tm2nmi "k/nmi.ml $$version"				&&\
	version=`grep "^;version: " swift.a65 | sed -e "s/;version: //"`&&\
	$(INSTALL2) swift.d7 "k/swift.d7 $$version"			&&\
	$(INSTALL2) swift.de "k/swift.de $$version"			&&\
	$(INSTALL2) swift.df "k/swift.df $$version"			&&\
	for file in $(PROTOCOLS); do					\
		version=`grep "^;version: " $$file.a65 | sed -e "s/;version: //"`;	\
		$(INSTALL2) $$file "p/$$file $$version";\
	done								&&\
	$(INSTALL2) sprite1 "sprite 1"					&&\
	$(INSTALL2) sprite2 "sprite 2"					&&\
	$(INSTALL2) sprite3 "sprite 3"					&&\
	$(INSTALL2) sprite4 "sprite 4"					&&\
	$(INSTALL2) ansitab "ansi tab"					&&\
	$(INSTALL2) asciitab "ascii tab"

.PHONY: clean
clean:
	@$(RM) $(SWIFTFILE).d7 $(SWIFTFILE).de $(SWIFTFILE).df
	@$(RM) $(OBJS) $(BLITZOBJS) $(CBASICOBJS) $(BASICOBJS)
	@$(RM) $(D64IMAGE1).d64 $(D64IMAGE2).d64
	@$(RM) mlsplit ml0 ml1 ml2 ml3

.PHONY: distclean
distclean: clean
