# This Makefile requires GNU make.

# Install paths. DESTDIR is used for installing to an alternate location,
# for people making RPM/deb/tgz/etc packages.
DESTDIR=
PREFIX=/usr/local
BINDIR=$(PREFIX)/bin
MANDIR=$(PREFIX)/share/man
MAN1DIR=$(MANDIR)/man1
MAN5DIR=$(MANDIR)/man5
MAN7DIR=$(MANDIR)/man7
DOCDIR=$(PREFIX)/share/doc/bw-atari8-tools
PSFDIR=$(PREFIX)/share/kbd/consolefonts
BDFDIR=$(PREFIX)/share/share/fonts/misc
TTFDIR=$(PREFIX)/share/share/fonts/TTF

# Compiler stuff
COPT=-O2
CC=gcc
CFLAGS=-Wall $(COPT) -ansi -D_GNU_SOURCE -DVERSION=\"$(VERSION)\"

# BINS and SCRIPTS go in $BINDIR, DOCS go in $DOCDIR
BINS=a8eol atr2xfd atrsize axe blob2c blob2xex cart2xex cxrefbas dumpbas fenders protbas renumbas rom2cart unmac65 unprotbas vxrefbas xex1to2 xexamine xexcat xexsplit xfd2atr listbas a8cat a8xd whichbas bas2aplus listamsb
SCRIPTS=dasm2atasm diffbas a8diff
MANS=a8eol.1 xfd2atr.1 atr2xfd.1 blob2c.1 cart2xex.1 fenders.1 xexsplit.1 xexcat.1 atrsize.1 rom2cart.1 unmac65.1 axe.1 dasm2atasm.1 blob2xex.1 xexamine.1 xex1to2.1 unprotbas.1 protbas.1 renumbas.1 dumpbas.1 vxrefbas.1 cxrefbas.1 listbas.1 a8cat.1 a8xd.1 whichbas.1 diffbas.1 a8diff.1 bas2aplus.1 listamsb.1
MAN5S=xex.5
MAN7S=atascii.7 fauxtari.7
DOCS=README.txt equates.inc *.dasm LICENSE ksiders/atr.txt
PSFS=fonts/fauxtari-8.psf fonts/fauxtari-16.psf fonts/fauxtari-24.psf
BDFS=fonts/fauxtari-8.bdf fonts/fauxtari-16.bdf fonts/fauxtari-24.bdf
TTF=fonts/FauxtariScalableMono.ttf

SUBDIRS=ksiders jindroush

# All the programs share this version number...
VERSION=0.2.1

# If your system doesn't support gzipped man pages, comment this out:
GZIP_MAN=y

# unmac65 can be built for Atari 8-bit. Don't do it by default, but
# these variables are used for cross-compiling:
CC65=cl65
CC65FLAGS=-O -t atari

# Some distros have this with a .py extension, some don't. Only needed
# if you're rebuilding the man pages (users shouldn't have to).
# RST2MAN=rst2man.py
RST2MAN=rst2man

# Targets below. You probably don't need to edit below this point.
# WARNING: Don't do a "make realclean" unless you have the DASM or
# Atasm 6502 cross assembler installed!
# "make clean" and "make distclean" will not delete the 6502 object
# code (the *.bin files), but "make realclean" will.

all: $(BINS) manpages symlinks subdirs $(TTF)

unprotbas: bas.o

protbas: bas.o

renumbas: bas.o bcdfp.o linetab.o

dumpbas: bas.o

whichbas: bas.o

vxrefbas: bas.o

cxrefbas: bas.o bcdfp.o linetab.o

bas2aplus: bas.o

listbas: listbas.c bas.o bcdfp.o tokens.o atables.o turbo_tokens.o aplus_tokens.o bxl_tokens.o bxe_tokens.o
	$(CC) $(CFLAGS) -o listbas listbas.c bas.o bcdfp.o tokens.o atables.o turbo_tokens.o aplus_tokens.o bxl_tokens.o bxe_tokens.o -lm

bas.o: bas.c bas.h

wtable.o: wtable.c wtable.h

atables.o: atables.c atables.h

a8cat: a8cat.c atables.o wtable.o

a8xd: a8xd.c atables.o

subdirs:
	@for dir in $(SUBDIRS); do ${MAKE} --no-print-directory -C $$dir COPT="$(COPT)"; done

a8eol: a8eol.c

xfd2atr: xfd2atr.c

atr2xfd: atr2xfd.c

atables.c: mkatables.pl
	perl mkatables.pl > atables.c

# note to cross-compiler users: If you're building the *.bin targets,
# blob2c needs to be executable on the build host. It'd also be nice
# to build a blob2c for the target platform... Probably you can do
# something like this:

# make blob2c CC=/usr/bin/cc          # build host blob2c
# make CC=/path/to/cross/cc           # build everything else (uses blob2c)
# rm -f blob2c                        # get rid of host blob2c so we can...
# make blob2c CC=/path/to/cross/cc    # build the target system's blob2c
# make install DESTDIR=/tmp/whatever...

# Note that this is only needed if you're building the 6502 object code,
# which you don't need to do unless you've modified it (the distribution
# tarball comes with prebuilt *.bin files).

blob2c: blob2c.c

fenders.bin: fenders.dasm asmwrapper.sh
	sh asmwrapper.sh fenders

fenders_bin.c: fenders.bin blob2c
	./blob2c fenders.bin > fenders_bin.c 2>fenders_bin.h

fenders_offsets.h: fenders.bin fenders_offsets.pl
	perl fenders_offsets.pl < fenders.syms > fenders_offsets.h

fenders: fenders.o fenders_bin.c fendersdbl_bin.c

fenders.o: fenders.c fenders_bin.h fenders_offsets.h fendersdbl_bin.h fendersdbl_offsets.h

fendersdbl.bin: fendersdbl.dasm asmwrapper.sh
	sh asmwrapper.sh fendersdbl

fendersdbl_bin.c: fendersdbl.bin blob2c
	./blob2c fendersdbl.bin > fendersdbl_bin.c 2>fendersdbl_bin.h

fendersdbl_offsets.h: fendersdbl.bin fenders_offsets.pl
	perl fenders_offsets.pl < fendersdbl.syms > fendersdbl_offsets.h

loadscreen.bin: loadscreen.dasm asmwrapper.sh
	sh asmwrapper.sh loadscreen

loadscreen_bin.c: loadscreen.bin blob2c
	./blob2c loadscreen.bin > loadscreen_bin.c 2>loadscreen_bin.h

cart2xex: cart2xex.c loadscreen_bin.c get_address.o cart.o

rom2cart: rom2cart.c cart.o

cart.o: cart.c cart.h

get_address.o: get_address.c get_address.h

xex.o: xex.c xex.h

xextest: xextest.c xex.o

xexsplit: xexsplit.c xex.o

xexcat: xexcat.c xex.o get_address.o

blob2xex: blob2xex.c xex.o get_address.o

xexamine: xexamine.c xex.o get_address.o

unmac65.xex: unmac65.c
	@rm -f unmac65.o
	$(CC65) $(CC65FLAGS) -DVERSION=\"$(VERSION)\" -DTAG=\"$(TAG)\" -t atari -o unmac65.xex unmac65.c
	@rm -f unmac65.o

axe: axe.c axe.h axelib.c

symlinks: atrcheck cart2rom

atrcheck: atr2xfd
	ln -s atr2xfd atrcheck

cart2rom: rom2cart
	ln -s rom2cart cart2rom

manpages: $(MANS) $(MAN5S) $(MAN7S)

%.1: %.rst manhdr.rst manftr.rst genopts.rst
	$(RST2MAN) $< > $@

%.5: %.rst manhdr5.rst manftr.rst
	$(RST2MAN) $< > $@

%.7: %.rst manhdr7.rst manftr.rst
	$(RST2MAN) $< > $@

fonts/fauxtari-8.bdf: fonts/mkpsf.pl
	( cd fonts && perl mkpsf.pl )

$(TTF): fonts/fauxtari-8.bdf
	bitmapfont2ttf --family-name "Fauxtari Scalable Mono" fonts/fauxtari-8.bdf $(TTF)

# "make clean" does NOT remove the .bin or _bin.[ch] files. This is
# for people who don't have either dasm or atasm installed.
# also, it doesn't remove the man pages. these are checked into git, even.
clean:
	rm -f core *.o *~ $(BINS)
	for dir in $(SUBDIRS); do make -C $$dir clean; done

distclean: clean
	rm -rf *.syms *.atr 1 2 3 *.xex *.rom *.atasm *.m65 atrcheck cart2rom

realclean: distclean
	rm -f *.bin *_bin.[ch] *_offsets.h $(MANS) $(MAN5S) $(MAN7S)

install: all install-subdirs
	mkdir -p $(DESTDIR)/$(BINDIR) $(DESTDIR)/$(MAN1DIR) $(DESTDIR)/$(MAN5DIR) $(DESTDIR)/$(MAN7DIR) $(DESTDIR)/$(DOCDIR)
	strip $(BINS)
	for i in $(BINS) $(SCRIPTS) ; do \
		install -m0755 $$i   $(DESTDIR)/$(BINDIR) ; \
		install -m0644 $$i.1 $(DESTDIR)/$(MAN1DIR) ; \
		if [ "$(GZIP_MAN)" = "y" ]; then \
			gzip -f $(DESTDIR)/$(MAN1DIR)/$$i.1 ; \
		fi ; \
	done
	( cd $(DESTDIR)/$(BINDIR) && rm -f atrcheck && ln -s atr2xfd atrcheck )
	( cd $(DESTDIR)/$(BINDIR) && rm -f cart2rom && ln -s rom2cart cart2rom )
	for i in $(MAN5S) ; do \
		install -m0644 $$i $(DESTDIR)/$(MAN5DIR) ; \
		if [ "$(GZIP_MAN)" = "y" ]; then \
			gzip -f $(DESTDIR)/$(MAN5DIR)/$$i ; \
		fi ; \
	done
	for i in $(MAN7S) ; do \
		install -m0644 $$i $(DESTDIR)/$(MAN7DIR) ; \
		if [ "$(GZIP_MAN)" = "y" ]; then \
			gzip -f $(DESTDIR)/$(MAN7DIR)/$$i ; \
		fi ; \
	done
	if [ "$(GZIP_MAN)" = "y" ]; then \
		cd $(DESTDIR)/$(MAN1DIR) && rm -f atrcheck.1.gz && ln -s atr2xfd.1.gz atrcheck.1.gz ; \
		cd $(DESTDIR)/$(MAN1DIR) && rm -f cart2rom.1.gz && ln -s rom2cart.1.gz cart2rom.1.gz ; \
	else \
		cd $(DESTDIR)/$(MAN1DIR) && rm -f cart2rom.1 && ln -s rom2cart.1 cart2rom.1 ; \
	fi
	install -m0644 $(DOCS) $(DESTDIR)/$(DOCDIR)
	install -m0644 ksiders/README.txt $(DESTDIR)/$(DOCDIR)/README_ksiders.txt

install-subdirs: subdirs
	for dir in $(SUBDIRS); do \
		make -C $$dir DESTDIR=$(DESTDIR) PREFIX=$(PREFIX) BINDIR=$(BINDIR) MAN1DIR=$(MAN1DIR) GZIP_MAN=$(GZIP_MAN) DOCDIR=$(DOCDIR) install; \
	done

install-fonts: all
	sh install-fonts.sh "$(DESTDIR)" "$(BDFDIR)" "$(TTFDIR)"

user-font: user-fonts

user-fonts:
	$(MAKE) install-fonts BDFDIR=$(HOME)/.fonts TTFDIR=$(HOME)/.fonts

user-symlinks: all
	SRC=$$( pwd ) ; \
	mkdir -p ~/bin ; \
	cd ~/bin && \
	for i in $(BINS) $(SCRIPTS) \
		ksiders/atrdir ksiders/atrextr ksiders/makeatr ksiders/sortatr ksiders/unmakatr \
		jindroush/chkrom/chkrom jindroush/chkbas/chkbas jindroush/bas2boot/bas2boot \
		jindroush/chkexe/chkexe jindroush/adir/adir jindroush/acvt/acvt jindroush/aext/aext \
		; do rm -f $$i; ln -sf $$SRC/$$i . ; done