# Makefile for blinkensisters
#
# (C) 2006 Wolfgang Dautermann
# See COPYING for licensing information
#
# I believe, that some of the constructs are GNU Make specific,
# so if GNU make is not the default make on your system, you will
# need to install it (and then usually execute "gmake" instead of "make")
#
# I will try to make that build system as portable as possible, but if you have some trouble,
# try using the gnu tools


# Commands to use (I recommend the GNU versions (sometimes called gsed, gcp, gtar, ...)
CC=g++
GREP=grep
SED=sed
SDLCONFIG=sdl-config
INSTALL=install
MAKEDEPEND=makedepend
CP=cp
CD=cd
RM=rm
LN=ln
FIND=find
TAR=tar
GZIP=gzip
BZIP2=bzip2
RPMBUILD=rpmbuild

PREFIX=/usr/local
BINDIR=$(PREFIX)/bin
SHAREDIR=$(PREFIX)/share/blinkensisters
DOCDIR=$(PREFIX)/share/doc/blinkensisters

# get the version number from globals.h
VERSION=$(shell $(GREP) "^.define.VERSION" globals.h | $(SED) "s/^.define.VERSION.//" )

CFLAGS  = -c -Wall -O3 -IBlinkenLUA/headers
CFLAGS += $(shell $(SDLCONFIG) --cflags)
CFLAGS += -DRESPATH="\"$(SHAREDIR)/\""

LDFLAGS = $(shell $(SDLCONFIG) --libs)


SOURCES = ${wildcard *.cpp BlinkenLUA/source/*.cpp}

# SDLMain.h is only necessary for MAC Xcode - for an usual UNIX build not,
# so do not consider that file as a 'normal' header-file.. 
# TODO: That file should go in an own subdirectory (MAC, XCODE or something simliar, then the filter will become unnecessary)
HEADERS = $(filter-out SDLMain.h, ${wildcard *.h BlinkenLUA/headers/*.h})

OBJECTS = $(SOURCES:.cpp=.o)
# the name of the compiled executable - *AND* the name of the generated tar.gz, tar.bz2, rpm, ...
EXECUTABLE = LostPixels

# Enable "make install DESTDIR=some-path" to make it easier
# to build RPMs, DEBs, ...
DESTDIR=


all: $(EXECUTABLE)

$(EXECUTABLE): $(OBJECTS) Makefile
	$(CC) $(LDFLAGS) $(OBJECTS) -o $@ -lSDL -lSDL_mixer -lSDL_image -lSDL_ttf

.cpp.o: Makefile
	$(CC) $(CFLAGS) $< -o $@

install: $(EXECUTABLE)
	$(INSTALL) -m 755 -d ${DESTDIR}${BINDIR}
	$(INSTALL) -m 755 -d ${DESTDIR}${SHAREDIR}
	$(INSTALL) -m 755 -d ${DESTDIR}${DOCDIR}
	$(CP) $(EXECUTABLE) ${DESTDIR}${BINDIR}
	$(CP) TTF/*.ttf ${DESTDIR}$(SHAREDIR)
	$(CP) SND/*.ogg ${DESTDIR}$(SHAREDIR)
	$(CP) GFX/*.bmp GFX/*.jpg ${DESTDIR}$(SHAREDIR)
	$(CP) LEVELS/*.dat LEVELS/*.conf LEVELS/*.bsl ${DESTDIR}$(SHAREDIR)
	$(CP) AUTHORS HOWTOPLAY INSTALL LICENSE README SCRIPTING ${DESTDIR}$(DOCDIR)
	# create a "blinkensisters" symlink for compatibility with previous versions.
	$(CD) ${DESTDIR}${BINDIR} ; $(LN) -s $(EXECUTABLE) blinkensisters

uninstall:
	$(RM) ${DESTDIR}${BINDIR}/$(EXECUTABLE)
	$(RM) ${DESTDIR}${BINDIR}/blinkensisters # symlink for compatibility with previous versions.
	$(RM) -rf ${DESTDIR}$(SHAREDIR)
	$(RM) -rf ${DESTDIR}$(DOCDIR)

clean:
	$(RM) -f $(EXECUTABLE) $(OBJECTS) $(EXECUTABLE)-$(VERSION).tar.gz $(EXECUTABLE)-$(VERSION).tar.bz2
	@echo "# DO NOT DELETE" >.depend


#### Usage
help:
	@echo "Useful make targets:"
	@echo "  all                  - build all as needed (default)"
	@echo "  install              - install files"
	@echo "  uninstall            - remove installed files"
	@echo "  clean                - remove all generated files"
	@echo "  tar.gz               - build a tar.gz-archive of all files"
	@echo "  tar.bz2              - build a tar.bz2-archive of all files"
	@echo "  rpm                  - build rpm packages"
	@echo "                         (read the comments in the Makefile before using that target)"
	@echo "  help                 - display this help"


# Every Makefile should have that targets
love:
	@echo "Not war!"
war:
	@echo "No! Make love!"


# rule for building dependency lists, and writing them to a file ".depend".
.depend: $(SOURCES) $(HEADERS)
	$(MAKEDEPEND) -f- -- $(CFLAGS) -- $(SOURCES) $(HEADERS) > .depend


tar.gz: clean
	$(LN) -s . $(EXECUTABLE)-$(VERSION)
	$(FIND) $(EXECUTABLE)-$(VERSION)/ -type f | $(GREP) -v CVS  | $(TAR) -T - -cvf - | $(GZIP) >$(EXECUTABLE)-$(VERSION).tar.gz
	$(RM) $(EXECUTABLE)-$(VERSION)


tar.bz2: clean
	$(LN) -s . $(EXECUTABLE)-$(VERSION)
	$(FIND) $(EXECUTABLE)-$(VERSION)/ -type f | $(GREP) -v CVS | $(TAR) -T - -cvf - | $(BZIP2) >$(EXECUTABLE)-$(VERSION).tar.bz2
	$(RM) $(EXECUTABLE)-$(VERSION)

#
# "make rpm" will work only (on RPM-based systems, of course) if you have
# write permissions to /usr/src/rpm/* (usually only root) or you
# tell rpm to use a different build area, see
# http://www.rpm.org/max-rpm/s1-rpm-anywhere-different-build-area.html
#
# 
rpm: tar.gz
	$(RPMBUILD)  --define 'version $(VERSION)' -ta $(EXECUTABLE)-${VERSION}.tar.gz

.PHONY: clean .depend

# include a dependency file if one exists
ifeq (.depend,$(wildcard .depend))
include .depend
endif