
# Makefile for blinkensisters/LostPixels
#
# (C) 2006 Wolfgang Dautermann
# See LICENSE 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 (if you have problems, try using 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
STRIP=strip

PREFIX=/usr/local
BINDIR=$(PREFIX)/bin
SHAREDIR=$(PREFIX)/share/blinkensisters
DOCDIR=$(PREFIX)/share/doc/blinkensisters
MENUDIR=/usr/share/applications/
ICONDIR=/usr/share/pixmaps/

# track dependencies (will require the makedepend program, recommended for developers)
# or not (then I recommend a "make clean" before each "make")
# set either "DEPENDENCIES=" or "DEPENDENCIES=true")
#
# you can also call "make DEPENDENCIES=true"
DEPENDENCIES=


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

# sound support enabled in globals.h?
DISABLESOUND=$(shell $(GREP) "^.define.DISABLE_SOUND" globals.h | $(SED) "s/^.define.//")
# network support enabled in globals.h?
DISABLENETWORK=$(shell $(GREP) "^.define.DISABLE_NETWORK" globals.h | $(SED) "s/^.define.//")

CFLAGS += -c -I. -IBlinkenBMF/ -IBlinkenLUA/LuaSocket/headers/ -IBlinkenLUA/LuaBindings/headers/ -IBlinkenLUA/LuaMain/headers/ -IBlinkenLib/headers/ -ISDL_gfx/
CFLAGS += $(shell $(SDLCONFIG) --cflags)
CFLAGS += -DRESPATH="\"$(SHAREDIR)/\""
# only on Gnu compilers:
CFLAGS += -Wall -O3

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

LIBS = -lSDL -lSDL_image -lSDL_ttf
ifneq ($(DISABLESOUND),DISABLE_SOUND)
LIBS += -lSDL_mixer
else
$(warning "Sound disabled. If you want to enable sound support, you can do this by commenting out the DISABLE_SOUND define in globals.h")
endif

ifneq ($(DISABLENETWORK),DISABLE_NETWORK)
LIBS += -lSDL_net
else
$(warning "Network disabled. If you want to enable networking support, you can do this by commenting out the DISABLE_NETWORK define in globals.h")
endif

LOSTPIXELSOURCES = ${wildcard *.cpp BlinkenLUA/LuaSocket/source/*.cpp BlinkenLUA/LuaBindings/source/*.cpp BlinkenLUA/LuaMain/source/*.cpp  BlinkenBMF/minilzo.cpp BlinkenBMF/bmfconvert.cpp BlinkenLib/source/*.cpp SDL_gfx/*.cpp}
BMFCOMPRESSSOURCES = BlinkenBMF/minilzo.cpp BlinkenBMF/bmfcompress.cpp BlinkenBMF/bmfconvert.cpp
BMFDECOMPRESSSOURCES = BlinkenBMF/minilzo.cpp BlinkenBMF/bmfdecompress.cpp BlinkenBMF/bmfconvert.cpp

HEADERS = $(wildcard *.h BlinkenLUA/source/*.h BlinkenBMF/*.h BlinkenLUA/LuaSocket/headers/*.h BlinkenLUA/LuaBindings/headers/*.h BlinkenLUA/LuaMain/headers/*.h BlinkenLib/headers/*.h SDL_gfx/*.h)

LOSTPIXELOBJECTS = $(LOSTPIXELSOURCES:.cpp=.o)
BMFCOMPRESSOBJECTS = $(BMFCOMPRESSSOURCES:.cpp=.o)
BMFDECOMPRESSOBJECTS = $(BMFDECOMPRESSSOURCES:.cpp=.o)

# the name of the package - used as name of the generated tar.gz, tar.bz2, rpm, ...
PACKAGENAME = LostPixels

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


all: LostPixels bmfcompress bmfdecompress basedata.bmf blinkenplayer

LostPixels: $(LOSTPIXELOBJECTS) Makefile
	$(CC) $(LDFLAGS) $(LOSTPIXELOBJECTS) -o $@ $(LIBS)

bmfcompress: $(BMFCOMPRESSOBJECTS) Makefile
	$(CC) $(LDFLAGS) $(BMFCOMPRESSOBJECTS) -o $@

bmfdecompress: $(BMFDECOMPRESSOBJECTS) Makefile
	$(CC) $(LDFLAGS) $(BMFDECOMPRESSOBJECTS) -o $@

blinkenplayer: BlinkenPlayer/*.cpp BlinkenPlayer/*.h *.cpp *.h
ifneq ($(DISABLESOUND),DISABLE_SOUND)
	$(MAKE) -C BlinkenPlayer
else
	@echo "BlinkenPlayer will not be build - sound disabled"
endif

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

basedata.bmf: bmfcompress BASEDATA/config BASEDATA/GFX/* BASEDATA/SND/* BASEDATA/TTF/* BASEDATA/LEVELS/*
	$(CD) BASEDATA ; ../bmfcompress META config ../basedata.bmf

install: LostPixels bmfcompress bmfdecompress basedata.bmf blinkenplayer
	$(INSTALL) -m 755 -d ${DESTDIR}${BINDIR}
	$(INSTALL) -m 755 -d ${DESTDIR}${SHAREDIR}
	$(INSTALL) -m 755 -d ${DESTDIR}${DOCDIR}
	$(CP) LostPixels ${DESTDIR}${BINDIR}
	$(CP) bmfcompress ${DESTDIR}${BINDIR}
	$(CP) bmfdecompress ${DESTDIR}${BINDIR}
	$(CP) basedata.bmf ${DESTDIR}$(SHAREDIR)
ifneq ($(DISABLESOUND),DISABLE_SOUND)
	$(CP) blinkenplayer ${DESTDIR}${BINDIR}
endif
	$(CP) DOC/* ${DESTDIR}$(DOCDIR)
	$(CP) BASEDATA/GFX/splash.jpg ${DESTDIR}$(SHAREDIR)
	$(CP) BASEDATA/TTF/*.ttf ${DESTDIR}$(SHAREDIR)
	## create a "blinkensisters" symlink for compatibility with previous versions.
	#$(CD) ${DESTDIR}${BINDIR} ; $(LN) -s $(EXECUTABLE) blinkensisters

uninstall:
	$(RM) ${DESTDIR}${BINDIR}/LostPixels
	$(RM) ${DESTDIR}${BINDIR}/bmfcompress
	$(RM) ${DESTDIR}${BINDIR}/bmfdecompress
	$(RM) ${DESTDIR}${SHAREDIR}/basedata.bmf
ifneq ($(DISABLESOUND),DISABLE_SOUND)
	$(RM) ${DESTDIR}${BINDIR}/blinkenplayer
endif
	#$(RM) ${DESTDIR}${BINDIR}/blinkensisters # symlink for compatibility with previous versions.
	$(RM) -rf ${DESTDIR}$(SHAREDIR)
	$(RM) -rf ${DESTDIR}$(DOCDIR)

menu-install:
	$(INSTALL) -m 755 -d ${DESTDIR}$(MENUDIR)
	$(INSTALL) -m 755 -d ${DESTDIR}$(ICONDIR)
	$(CP) LostPixels.desktop ${DESTDIR}$(MENUDIR)
	$(CP) BASEDATA/GFX/lostpixels.ico ${DESTDIR}$(ICONDIR)
ifneq ($(DISABLESOUND),DISABLE_SOUND)
	$(CP) BlinkenPlayer.desktop ${DESTDIR}$(MENUDIR)
	$(CP) BASEDATA/GFX/blinkenplayer.ico ${DESTDIR}$(ICONDIR)
endif

menu-uninstall:
	$(RM) ${DESTDIR}$(MENUDIR)/LostPixels.desktop
	$(RM) ${DESTDIR}$(ICONDIR)/lostpixels.ico
ifneq ($(DISABLESOUND),DISABLE_SOUND)
	$(RM) ${DESTDIR}$(MENUDIR)/BlinkenPlayer.desktop
	$(RM) ${DESTDIR}$(ICONDIR)/blinkenplayer.ico
endif


# currently not used (als library) in our project, may be useful for other projects?
# FIXME: Naming of the library? Is a such small library useful? 
libsmallcmdopt.a: cmdopts.h cmdopts.o
	ar rc $@ cmdopts.o
	ranlib $@

clean:
	$(RM) -f LostPixels bmfcompress bmfdecompress basedata.bmf $(LOSTPIXELOBJECTS) $(BMFCOMPRESSOBJECTS) $(BMFDECOMPRESSOBJECTS) 
	$(RM) -f $(PACKAGENAME)-$(VERSION).tar $(PACKAGENAME)-$(VERSION).tar.gz $(PACKAGENAME)-$(VERSION).tar.bz2
	$(RM) -rf cmake-build/*
	$(MAKE) -C BlinkenPlayer clean
	@echo "# DO NOT DELETE" >.depend

strip: LostPixels bmfcompress bmfdecompress blinkenplayer
	$(STRIP) LostPixels
	$(STRIP) bmfcompress
	$(STRIP) bmfdecompress
ifneq ($(DISABLESOUND),DISABLE_SOUND)
	$(STRIP) blinkenplayer
endif


#### Usage
help:
	@echo "Useful make targets:"
	@echo "  all                  - build all as needed (default)"
	@echo "  install              - install files"
	@echo "  uninstall            - remove installed files"
	@echo "  menu-install         - integration in Windowmanager (KDE,...) menu"
	@echo "  menu-uninstall       - remove integration in Windowmanager (KDE,...) menu"
	@echo "  clean                - remove all generated files"
	@echo "  tar                  - build a tar-archive of all 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 package (experimental)"
	@echo "                         (read the comments in the Makefile before using that target)"
	@echo "  solaris-pkg          - build solaris package (experimental)"
	@echo "  help                 - display this help"
	@echo
	@echo "  You can add the following variables to the build process:"
	@echo "  make PREFIX=/path/to/somewhere"
	@echo "  make install PREFIX=/path/to/somewhere"
	@echo "  Build and installs in /path/to/somewhere instead /usr/local"
	@echo
	@echo "  make DEPENDENCIES=true"
	@echo "  build LostPixels using full dependency tracking."
	@echo "  (needs the 'makedepend'-program which is often not installed"
	@echo "  - so this is not the default)"
	@echo "  If you do not build using full dependency tracking a warning is given"
	@echo "  I recommend always do a 'make clean' before a new 'make' in that case"



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


ifdef DEPENDENCIES
# rule for building dependency lists, and writing them to a file ".depend".
.depend: $(LOSTPIXELSOURCES) $(BMFCOMPRESSSOURCES) $(BMFDECOMPRESSSOURCES) $(HEADERS)
	@echo "# DO NOT DELETE" >.depend
	$(MAKEDEPEND) -f- -- $(CFLAGS) -- $(LOSTPIXELSOURCES) $(BMFCOMPRESSSOURCES) $(BMFDECOMPRESSSOURCES) $(HEADERS) > .depend
else
# output a warning, that no full dependency tracking is enabled by default
$(warning "Warning: no full dependency tracking (regarding the c++-files) enabled (one needs the 'makedepend' program for that task and often it is not installed). I suggest either a 'make clean' before a new build or doing a 'make DEPENDENCIES=true' (which will enable full dependency tracking)")

endif

tar: clean
	$(LN) -s . $(PACKAGENAME)-$(VERSION)
	$(FIND) $(PACKAGENAME)-$(VERSION)/ -type f  -o -type l | $(GREP) -v .svn  | $(TAR) -T - -cvf $(PACKAGENAME)-$(VERSION).tar
	$(RM) $(PACKAGENAME)-$(VERSION)

tar.gz: tar
	$(GZIP) $(PACKAGENAME)-$(VERSION).tar


tar.bz2: tar
	$(BZIP2) $(PACKAGENAME)-$(VERSION).tar

#
# "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 $(PACKAGENAME)-${VERSION}.tar.gz


# experimental target for creating solaris packages
solaris-pkg: 
	$(MAKE) install DESTDIR=/tmp/blinkensisterspackage
	(echo 'i pkginfo'; pkgproto /tmp/blinkensisterspackage=/ ) >prototype
	pkgmk -o
	pkgtrans -s /var/spool/pkg /tmp/blinkensisters.pkg blinkensisters
	rm -rf /var/spool/pkg/blinkensisters
	rm -rf /tmp/blinkensisterspackage
	@echo "Package written to /tmp/blinkensisters.pkg"
	@echo "you can install it now using "pkgadd -d /tmp/blinkensisters.pkg"

.PHONY: clean .depend

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