
# Written by Peter Ekberg, peda@lysator.liu.se

SHELL        = /bin/sh
CC           = gcc
DD           = dd

prefix       = /usr/local
BINDIR       = $(prefix)/games
MANDIR       = $(prefix)/man
STATEDIR     = /var/games

DEFINES      = $(strip \
                 -DHIGHSCOREFILE=\"$(STATEDIR)/$(PRG).highscore\" \
                 -DVERSION=\"$(VERSION)\" )
OPTIMIZE     = -fomit-frame-pointer -O2 -s -m486
COMPILE      = -Wall
ALL_CFLAGS   = $(DEFINES) $(OPTIMIZE) $(COMPILE) $(CFLAGS)
LDFLAGS      = -N

PRG          = thrust
MAN          = thrust.man
VERSION      = 0.59
SOURCE       = src
DATASRC      = datasrc

LIBS         = -lvgagl -lvga -lm

HELPPRG      = $(addprefix $(DATASRC)/, bin2c txt2c reverse)

BIN8         = $(addprefix $(DATASRC)/, \
                 blocks0.bin blocks1.bin blocks2.bin blocks3.bin \
                 blocks4.bin blocks5.bin blocks6.bin blocks7.bin \
                 blocks8.bin blocks9.bin blocksa.bin blocksb.bin \
                 blocksc.bin blocksd.bin blockse.bin blocksf.bin )

SOURCEOBJS   = $(addprefix $(SOURCE)/, \
                 thrust.o graphics.o level.o init.o things.o sinus.o \
                 fast_gr.o font5x5.o intro.o highscore.o keys.o)
DATAOBJS     = $(addprefix $(DATASRC)/, \
                 font.o blocks.o ship.o shield.o colors.o bullets.o \
                 level1.o level2.o level3.o level4.o demomoves.o)
OBJS         = $(SOURCEOBJS) $(DATAOBJS)

TARFILE      = $(PRG)-src-$(VERSION).tar.gz
BINTARFILE   = $(PRG)-ELF-$(VERSION).tar.gz
DISTFILES    = COPYING README INSTALL TODO CHANGES \
               Makefile* $(PRG)-$(VERSION).lsm $(MAN)
SRCDISTFILES = $(addsuffix .pod, $(basename $(MAN)))
DISTDIRS     = $(SOURCE) $(DATASRC)


#######################
#
# Targets.
#
#######################

all: $(PRG) man

install: $(PRG)
	cp $(PRG) $(BINDIR)/$(PRG)
	chown root.root $(BINDIR)/$(PRG)
	chmod a+rs $(BINDIR)/$(PRG)

uninstall:
	rm $(BINDIR)/$(PRG)
	rm $(STATEDIR)/$(PRG).highscore

install-man: man
	cp $(MAN) $(MANDIR)/man6/$(addsuffix .6,$(basename $(MAN)))

uninstall-man:
	rm $(MANDIR)/man6/$(addsuffix .6,$(basename $(MAN)))

clean:
	rm -rf $(strip *~ core $(PRG) $(OBJS) $(HELPPRG) \
                       $(DATASRC)/blocks*.bin $(TARFILE) )

distclean: clean
	rm -f $(SOURCE)/TAGS
	rm -f $(BINTARFILE)
	rm -f $(MAN)

mostlyclean:
	rm -rf *~ core $(PRG) $(SOURCEOBJS)

realclean: distclean

TAGS:
	etags $(SOURCE)/*.c -o $(SOURCE)/TAGS

info:
	@echo No documentation available.

dvi:
	@echo No documentation available.

man: $(MAN)

dist: distclean man
	mkdir $(PRG)
	mkdir $(PRG)/$(SOURCE)
	mkdir $(PRG)/$(DATASRC)
	cp -dpf $(DISTFILES) $(PRG)
	cp -dpf $(SRCDISTFILES) $(PRG)
	for i in $(DISTDIRS) ; do cp -dpf $$i/* $(PRG)/$$i ; done
	tar -czf $(TARFILE) $(PRG)/*
	rm -rf $(PRG)

bindist: $(PRG) man
	mv $(PRG) $(PRG).binary
	mkdir $(PRG)
	cp -dpf $(DISTFILES) $(PRG)
	rm -f $(PRG)/Makefile
	mv $(PRG)/Makefile.binary $(PRG)/Makefile
	mv $(PRG).binary $(PRG)/$(PRG)
	tar -czf $(BINTARFILE) $(PRG)/*
	mv $(PRG)/$(PRG) $(PRG).binary
	rm -rf $(PRG)
	mv $(PRG).binary $(PRG)

#######################
#
# Implicit rules.
#
#######################

# Compile C files to object files.
%.o: %.c
	$(CC) $(ALL_CFLAGS) -c -o $(addprefix $(dir $<), $(notdir $@)) $<
# Complie assembler files to object files. (Not used. Does it work?)
%.s: %.c
	$(CC) $(ALL_CFLAGS) -S -o $(addprefix $(dir $<), $(notdir $@)) $<


# Extract palette information.
%.bin: %.pal
	$(DD) of=$@ if=$< bs=1 skip=790
# Extract picture information.
%.rbin: %.bmp
	$(DD) of=$@ if=$< bs=1 skip=1078


# The bmp format stores rows backwards (last row first).
# These rules are used to "reverse" the order in which the rows are stored.
# There is one rule for each of the three used widths of the line.
%.bin: %.4.rbin $(DATASRC)/reverse
	$(DATASRC)/reverse  4 < $< > $@
%.bin: %.8.rbin $(DATASRC)/reverse
	$(DATASRC)/reverse  8 < $< > $@
%.bin: %.16.rbin $(DATASRC)/reverse
	$(DATASRC)/reverse 16 < $< > $@


# Make a C file out of a text file. The info is stored in an array of strings.
# One line in the original text file will be stored in one cell of the array.
%.c: %.def $(DATASRC)/txt2c
	$(DATASRC)/txt2c $(notdir $(basename $<)) < $< > $@
# Make a C file out of a binary file. The info is stored in a byte array.
%.c: %.bin $(DATASRC)/bin2c
	$(DATASRC)/bin2c bin_$(notdir $(basename $<)) < $< > $@


# Make a man page from a pod source
%.man: %.pod
	pod2man $< > $@

#######################
#
# Secondary targets.
#
#######################

$(PRG): $(OBJS)
	$(CC) $(ALL_CFLAGS) $(LDFLAGS) -o $(PRG) $(OBJS) $(LIBS)


$(DATASRC)/blocks.bin: $(BIN8)
	cat $^ > $@

$(DATASRC)/reverse: $(DATASRC)/reverse.c
	$(CC) $(ALL_CFLAGS) $< -o $@

$(DATASRC)/bin2c:  $(DATASRC)/bin2c.c
	$(CC) $(ALL_CFLAGS) $< -o $@

$(DATASRC)/txt2c:  $(DATASRC)/txt2c.c
	$(CC) $(ALL_CFLAGS) $< -o $@


#######################
#
# Dependencies.
#
#######################

$(SOURCE)/thrust.o:    $(SOURCE)/thrust.c    $(SOURCE)/thrust.h
$(SOURCE)/graphics.o:  $(SOURCE)/graphics.c  $(SOURCE)/thrust.h
$(SOURCE)/level.o:     $(SOURCE)/level.c     $(SOURCE)/thrust.h
$(SOURCE)/init.o:      $(SOURCE)/init.c      $(SOURCE)/thrust.h
$(SOURCE)/things.o:    $(SOURCE)/things.c    $(SOURCE)/thrust.h
$(SOURCE)/sinus.o:     $(SOURCE)/sinus.c     $(SOURCE)/thrust.h
$(SOURCE)/fast_gr.o:   $(SOURCE)/fast_gr.c   $(SOURCE)/thrust.h
$(SOURCE)/font5x5.o:   $(SOURCE)/font5x5.c
$(SOURCE)/highscore.o: $(SOURCE)/highscore.c $(SOURCE)/thrust.h
$(SOURCE)/keys.o:      $(SOURCE)/keys.c      $(SOURCE)/thrust.h
