# Makefile for Mortar sources
#
# SDL is used by default for video and sound.  If you don't have
# LibSDL/SDL_mixer, you need set NAME & SOUND to suitable alternatives
# from the supported targets (see below).  DESTDIR or PREFIX can be
# changed to set the Mortar installation path for your system
# (in Makefile or from command line).  Build it with:
#       make NAME=target
#
# After build, type just 'make install' and test Mortar with:
#	<mortar-binary> -p data <player1> <player2>
#
# This is free software; you can redistribute it and/or modify it
# under the terms specified in the GNU Public Licence (GPL).
#
# Copyright (C) 1998-2002,2005,2008 by Eero Tamminen


# TARGETS: fbmortar:   Linux + 8/16-bit framebuffer
#                      (with virtual console support)
#          wmortar:    W window system
#          xwmortar:   Wlib on X11
#          ggimortar:  libGGI
#          sdlmortar:  SDL + SDL_mixer
#          amortar:    Amiga OS
#          pmortar:    Linux + mono framebuffer (untested)
#          mortar.ttp: Atari TOS
#          mortar.exe: DOS

# should be set to one of TARGETs
ifndef NAME
TARGETS = fbmortar wmortar xwmortar ggimortar sdlmortar \
	amortar pmortar mortar.exe mortar.ttp
default:
	@echo "Select one of targets:"
	@for i in $(TARGETS); do echo "- $$i"; done
	@echo "and type 'make NAME=<target>'"
endif

# whether build environment support unix or just DOS stuff
UNIX = 1

# sound driver can be either of:
# - sdl:   -lSDL_mixer library (unix, windows...)
# - gsi:   -lgsi library (available as unix package)
# - tos:   sound effects on Atari TOS/MiNT/W
# - paula: sound effects on Amiga
# - none:  for no sound
SOUND ?= sdl

# Whether to use Maemo keyboard bindings
# - 0: no
# ->0: given Maemo version (1-4 so far)
MAEMO ?= 0

$(NAME): $(wildcard src/*.[ch])
	$(MAKE) -C src NAME=$(NAME) SOUND=$(SOUND) MAEMO=$(MAEMO)
	mv src/$(NAME) .

# --------------------------------

clean:
	$(MAKE) -C src clean
	$(RM) data/core snapshot.ppm

veryclean: clean
	$(RM) $(TARGETS) mortar*.zip mortar*.tgz
	if [ -e gui/Makefile ]; then \
		$(MAKE) -C gui/ distclean; \
	else \
		cd gui; $(MAKE) -f make.clean clean; \
	fi;


# ---------------------------------------------

ifdef UNIX

# overridable installation directory prefix for Debian
ifdef DESTDIR
PREFIX = /usr
else
# overridable installation directory prefix for direct compilation
PREFIX ?= /usr/local
endif

SCRIPT	= mortar
# note the PREFIX is used also in the install target
GAMEDIR = $(PREFIX)/games
BINDIR  = $(DESTDIR)$(GAMEDIR)
MANDIR	= $(DESTDIR)$(PREFIX)/share/man/man6
DATADIR = $(DESTDIR)$(PREFIX)/share/mortar
ICONDIR = $(DESTDIR)$(PREFIX)/share/icons/hicolor
ifeq ($(MAEMO),0)
DESKDIR = $(DESTDIR)$(PREFIX)/share/applications
APPS = apps
else
DESKDIR = $(DESTDIR)$(PREFIX)/share/applications/hildon
APPS = hildon
endif

# it's not that obvious how to escape $* from both make and shell in 'mort'...
install: $(NAME)
	@echo checking that package got default files...
	[ -f data/config.def  ] || { echo 'default config missing'; exit 1; }
	[ -f data/english     ] || { echo 'default language file missing'; exit 1; }
	[ -f data/ground.pic* ] || { echo 'ground image(s) missing'; exit 1; }
	[ -f data/cannon.pic* ] || { echo 'cannon image(s) missing'; exit 1; }
	[ -f data/font.pic*   ] || { echo 'font image(s) missing'; exit 1; }
	[ -f data/shot.pic*   ] || { echo 'shot image(s) missing'; exit 1; }
	[ -f data/sky.pic*    ] || { echo 'sky image(s) missing'; exit 1; }
	@echo removing previous version and creating dirs...
	rm -rf $(DATADIR)
	[ -d $(BINDIR) ] || install -d -m 755 $(BINDIR)
	[ -d $(DESKDIR) ] || install -d -m 755 $(DESKDIR)
	[ -d $(ICONDIR) ] || install -d -m 755 $(ICONDIR)
	[ -d $(MANDIR) ] || install -d -m 755 $(MANDIR)
	[ -d $(DATADIR) ] || install -d -m 755 $(DATADIR)
	[ -d $(DATADIR)/snd ] || install -d -m 755 $(DATADIR)/snd
	rm -f $(BINDIR)/$(NAME) $(BINDIR)/$(SCRIPT)
	rm -f $(MANDIR)/$(NAME).6 $(MANDIR)/$(SCRIPT).6
	@echo installing executables...
	install -m 755 $(NAME) $(BINDIR)/$(NAME)
	echo '#!/bin/sh' > $(BINDIR)/$(SCRIPT)
	echo 'export SDL_VIDEO_X11_WMCLASS=Mortar' >> $(BINDIR)/$(SCRIPT)
	echo $(GAMEDIR)/$(NAME) -p $(PREFIX)/share/mortar \$$\* >> $(BINDIR)/$(SCRIPT)
	chmod 755 $(BINDIR)/$(SCRIPT)
	@echo ...Maemo/Desktop stuff...
	install -m 644 mortar.desktop $(DESKDIR)
	for size in 26x26 48x48 64x64; do \
	  install -d -m 755 $(ICONDIR)/$$size/$(APPS); \
	  install -m 644 icons/mortar$$size.png $(ICONDIR)/$$size/$(APPS)/mortar.png; \
	done
	@echo ...manuals...
	install -m 644 doc/mortar.6 $(MANDIR)/$(SCRIPT).6
	cd $(MANDIR); ln -s $(SCRIPT).6 $(NAME).6
	@echo ...data files and configuration:
	for i in $$(cd data/; find . -name CVS -prune -o -type f -print); do install -m 644 data/$$i $(DATADIR)/$$i; done
	chmod 755 $(DATADIR)/snd/au2wav.sh
	@echo
	@echo "You can now run MORTAR just by typing '$(SCRIPT) [players]'."

else

# DOS installation is untested
ifeq ($(NAME),mortar.exe)

SCRIPT	= mort.bat
BATDIR	= c:\dos
BASEDIR = c:\games\mortar

install: $(NAME)
	@echo $(BASEDIR)\$(NAME) -p $(BASEDIR)\data > $(BATDIR)\$(SCRIPT)
	@echo "You can now run MORTAR just by typing '"$(SCRIPT)" [players]'."

endif

endif

# --------------------------------
# On Debian:
# * Build (only) binary package with:
#       dpkg-buildpackage -uc -b
# * Create also source package:
#       dpkg-buildpackage -uc -us -ICVS

package1: veryclean
	cd ..; zip -r mortar_s.zip mortar/
	mv ../mortar_s.zip .

package2: veryclean
	cd ..; tar -zcvf mortar_s.tgz --exclude CVS mortar/
	mv ../mortar_s.tgz .

binpack1: $(NAME)
	zip -r mortar.zip $(NAME) *.bat data/ doc/
	mv ../mortar.zip .

binpack2: $(NAME)
	tar -zcvf mortar.tgz --exclude CVS $(NAME) *.bat data/ doc/
	mv ../mortar.tgz .
