#!/usr/bin/make -f
#-*- makefile -*-
# Made with the aid of dh_make, by Craig Small
# Sample debian/rules that uses debhelper. GNU copyright 1997 by Joey Hess.
# Some lines taken from debmake, by Christoph Lameter.

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1
export DH_COMPAT=4

PACKAGE = moon-buggy
VERSION = 1.0.51
SOUNDVERSION = 1.0.51

# no patches to be applied at the moment
#USEPATCH = yes

# uncomment the next line to build the moon-buggy-pause package
#WITHPAUSE = yes

# debian-policy 3.5.10: support DEB_BUILD_OPTIONS
CFLAGS = -Wall -g
INSTALL = install
INSTALL_FILE    = $(INSTALL) -p    -o root -g root  -m  644
INSTALL_PROGRAM = $(INSTALL) -p    -o root -g root  -m  755
INSTALL_GAME    = $(INSTALL) -p    -o root -g games -m  775
INSTALL_SCRIPT  = $(INSTALL) -p    -o root -g root  -m  755
INSTALL_DIR     = $(INSTALL) -p -d -o root -g root  -m  755
          
ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
CFLAGS += -O0
else
CFLAGS += -O2
endif
export CFLAGS
# end debian-policy 3.5.10

# autotools-dev 
DEBEMAIL = "cts@debian.org"
DEBFULLNAME = "Christian T. Steigies"
# Calling GNU configure properly
export DEB_HOST_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
export DEB_BUILD_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

# FOR AUTOCONF 2.52 AND NEWER ONLY
ifeq ($(DEB_BUILD_GNU_TYPE), $(DEB_HOST_GNU_TYPE))
  confflags += --build $(DEB_HOST_GNU_TYPE)
else
  confflags += --build $(DEB_BUILD_GNU_TYPE) --host $(DEB_HOST_GNU_TYPE)
endif

autotools:
	OLDDATESUB=`./config.sub -t | tr -d -` ;\
	OLDDATEGUESS=`./config.guess -t | tr -d -` ;\
	NEWDATESUB=`/usr/share/misc/config.sub -t | tr -d -` ;\
	NEWDATEGUESS=`/usr/share/misc/config.guess -t | tr -d -` ;\
	if [ $$OLDDATESUB -lt $$NEWDATESUB -o \
	     $$OLDDATEGUESS -lt $$NEWDATEGUESS ]; then \
	   dch -a -p "GNU config automated update: config.sub\
	     ($$OLDDATESUB to $$NEWDATESUB), config.guess\
	     ($$OLDDATEGUESS to $$NEWDATEGUESS)" ;\
	   cp -f /usr/share/misc/config.sub config.sub ;\
	   cp -f /usr/share/misc/config.guess config.guess ;\
	   echo WARNING: GNU config scripts updated from master copies  1>&2 ;\
	fi

# end autotools-dev

#debian/po/templates.pot: debian/templates
#	@debconf-updatepo

#build: debian/po/templates.pot
build: 
# policy requires an empty build target

build-non-esd: build-non-esd-stamp
build-non-esd-stamp:
	dh_testdir
	-rm build-non-esd-stamp

	-mkdir non-esd
	cd non-esd ; \
	tar xvfz ../upstream/$(PACKAGE)-$(VERSION).tar.gz

	# patch the source?
ifeq ($(USEPATCH),yes)
	cd non-esd/$(PACKAGE)-$(VERSION) ; \
		patch -p1 < ../../patches/001_highscore.diff ; 
	cd non-esd/$(PACKAGE)-$(VERSION) ; \
		patch -p1 < ../../patches/002_boss.diff ; 
endif

# fix for time skew problem
#	cd non-esd/$(PACKAGE)-$(VERSION) ; \
#	touch configure.in && \
#	touch aclocal.m4 && \
#	touch configure

	cd non-esd/$(PACKAGE)-$(VERSION) ; \
	rm -rf debian ; \
	./configure $(confflags) \
		--prefix=/usr \
		--bindir=/usr/games \
		--mandir=/usr/share/man \
		--infodir=/usr/share/info \
		--sharedstatedir=/var/games \
		--with-setgid=games 

	# Add here commands to compile the package.
	$(MAKE) -C non-esd/$(PACKAGE)-$(VERSION)
	$(MAKE) -C non-esd/$(PACKAGE)-$(VERSION) $(PACKAGE).ps 
	sed s/#DIR#/"non-esd\/$(PACKAGE)-$(VERSION)"/ \
	  < debian/$(PACKAGE).docs.in \
	  > debian/$(PACKAGE).docs

	touch build-non-esd-stamp

build-esd: build-esd-stamp
build-esd-stamp:
	dh_testdir

	-rm build-esd-stamp

	-mkdir esd
	cd esd ; \
	tar xvfz ../upstream/$(PACKAGE)-$(VERSION).tar.gz ; \
	tar xvfz ../upstream/$(PACKAGE)-sound-$(SOUNDVERSION).tar.gz

	cd esd/$(PACKAGE)-$(VERSION) ; \
	patch -p1 < ../$(PACKAGE)-$(SOUNDVERSION)/sound.patch

	# patch the source?
ifeq ($(USEPATCH),yes)
	cd esd/$(PACKAGE)-$(VERSION) ; \
		patch -p1 < ../../patches/001_highscore.diff ; 
	cd esd/$(PACKAGE)-$(VERSION) ; \
		patch -p1 < ../../patches/002_boss.diff ; 
endif

	cd esd/$(PACKAGE)-$(VERSION) ; \
	rm -rf debian ; \
	./configure $(confflags) \
		--prefix=/usr \
		--bindir=/usr/games \
		--mandir=/usr/share/man \
		--infodir=/usr/share/info \
		--sharedstatedir=/var/games \
		--with-setgid=games

	# Add here commands to compile the package.
	-rm -f esd/$(PACKAGE)-$(VERSION)/$(PACKAGE).info
	$(MAKE) -C esd/$(PACKAGE)-$(VERSION)
	$(MAKE) -C esd/$(PACKAGE)-$(VERSION) $(PACKAGE).ps 

	sed s/#DIR#/"esd\/$(PACKAGE)-$(VERSION)"/ \
	  < debian/$(PACKAGE)-esd.docs.in \
	  > debian/$(PACKAGE)-esd.docs

	touch build-esd-stamp

build-pause: build-pause-stamp
build-pause-stamp:
ifeq ($(WITHPAUSE),yes)
	dh_testdir

	-rm build-pause-stamp

	-mkdir pause
	cd pause ; \
	tar xvfz ../upstream/$(PACKAGE)-$(VERSION).tar.gz ; \
	tar xvfz ../upstream/$(PACKAGE)-sound-$(SOUNDVERSION).tar.gz

	cd pause/$(PACKAGE)-$(VERSION) ; \
	patch -p1 < ../../patches/003_pause.diff

	# patch the source?
ifeq ($(USEPATCH),yes)
	cd pause/$(PACKAGE)-$(VERSION) ; \
		patch -p1 < ../../patches/001_highscore.diff ; 
	cd pause/$(PACKAGE)-$(VERSION) ; \
		patch -p1 < ../../patches/002_boss.diff ; 
endif

	cd pause/$(PACKAGE)-$(VERSION) ; \
	rm -rf debian ; \
	./configure $(confflags) \
		--prefix=/usr \
		--bindir=/usr/games \
		--mandir=/usr/share/man \
		--infodir=/usr/share/info \
		--sharedstatedir=/var/games \
		--with-setgid=games

	# Add here commands to compile the package.
	-rm -f pause/$(PACKAGE)-$(VERSION)/$(PACKAGE).info
	$(MAKE) -C pause/$(PACKAGE)-$(VERSION)
	$(MAKE) -C pause/$(PACKAGE)-$(VERSION) $(PACKAGE).ps 

	sed s/#DIR#/"pause\/$(PACKAGE)-$(VERSION)"/ \
	  < debian/$(PACKAGE)-pause.docs.in \
	  > debian/$(PACKAGE)-pause.docs

endif
	touch build-pause-stamp

clean: autotools
	dh_testdir
	dh_testroot
	-rm -f build-non-esd-stamp build-esd-stamp build-pause-stamp

	# Add here commands to clean up after the build process.
	-rm -rf non-esd
	-rm -rf esd
	-rm -rf pause
	-rm debian/$(PACKAGE).docs
	-rm debian/$(PACKAGE)-esd.docs
	-rm debian/$(PACKAGE)-pause.docs
	dh_clean

install-non-esd: install-non-esd-stamp

install-non-esd-stamp: build-non-esd-stamp
	dh_testdir
	dh_testroot
#	dh_clean -k
	dh_installdirs

	# Add here commands to install the package into debian/tmp.
	$(INSTALL_GAME)  non-esd/$(PACKAGE)-$(VERSION)/$(PACKAGE) \
		debian/$(PACKAGE)/usr/games/
	$(INSTALL_FILE) non-esd/$(PACKAGE)-$(VERSION)/$(PACKAGE).info \
                      debian/$(PACKAGE)/usr/share/info/
	$(INSTALL_FILE) non-esd/$(PACKAGE)-$(VERSION)/$(PACKAGE).6 \
                      debian/$(PACKAGE)/usr/share/man/man6/

	$(INSTALL_FILE) non-esd/$(PACKAGE)-$(VERSION)/$(PACKAGE).xpm \
                      debian/$(PACKAGE)/usr/share/pixmaps/

	touch install-non-esd-stamp

install-esd: install-esd-stamp

install-esd-stamp: build-esd-stamp
	dh_testdir
	dh_testroot
#	dh_clean -k
	dh_installdirs

	# Add here commands to install the package into debian/tmp.
	$(INSTALL_GAME) esd/$(PACKAGE)-$(VERSION)/$(PACKAGE) \
		debian/$(PACKAGE)-esd/usr/games/
	$(INSTALL_FILE) esd/$(PACKAGE)-$(VERSION)/$(PACKAGE).info \
		debian/$(PACKAGE)-esd/usr/share/info/
	$(INSTALL_FILE) esd/$(PACKAGE)-$(VERSION)/$(PACKAGE).6 \
		debian/$(PACKAGE)-esd/usr/share/man/man6/
	$(INSTALL_FILE) esd/$(PACKAGE)-$(SOUNDVERSION)/jump.wav \
		debian/$(PACKAGE)-esd/usr/share/sounds/$(PACKAGE)/
	$(INSTALL_FILE) esd/$(PACKAGE)-$(SOUNDVERSION)/laser.wav \
		debian/$(PACKAGE)-esd/usr/share/sounds/$(PACKAGE)/
	$(INSTALL_FILE) esd/$(PACKAGE)-$(SOUNDVERSION)/crash.wav \
		debian/$(PACKAGE)-esd/usr/share/sounds/$(PACKAGE)/

	$(INSTALL_FILE) esd/$(PACKAGE)-$(VERSION)/$(PACKAGE).xpm \
                      debian/$(PACKAGE)-esd/usr/share/pixmaps/

	touch install-esd-stamp

install-pause: install-pause-stamp

install-pause-stamp: build-pause-stamp
ifeq ($(WITHPAUSE),yes)
	dh_testdir
	dh_testroot
#	dh_clean -k
	dh_installdirs

	# Add here commands to install the package into debian/tmp.
	$(INSTALL_GAME) pause/$(PACKAGE)-$(VERSION)/$(PACKAGE) \
		debian/$(PACKAGE)-pause/usr/games/
	$(INSTALL_FILE) pause/$(PACKAGE)-$(VERSION)/$(PACKAGE).info \
                      debian/$(PACKAGE)-pause/usr/share/info/
	$(INSTALL_FILE) pause/$(PACKAGE)-$(VERSION)/$(PACKAGE).6 \
                      debian/$(PACKAGE)-pause/usr/share/man/man6/

	$(INSTALL_FILE) pause/$(PACKAGE)-$(VERSION)/$(PACKAGE).xpm \
                      debian/$(PACKAGE)-pause/usr/share/pixmaps/

endif
	touch install-pause-stamp

install: install-esd install-non-esd install-pause

# Build architecture-independent files here.
binary-indep: build install
# We have nothing to do by default.

# Build architecture-dependent files here.
binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installdebconf -A
	dh_installdocs -A
# README.Debian for all packages
	cp debian/README.Debian \
		debian/$(PACKAGE)-esd/usr/share/doc/$(PACKAGE)-esd/
ifeq ($(WITHPAUSE),yes)
	cp debian/README.Debian \
		debian/$(PACKAGE)-pause/usr/share/doc/$(PACKAGE)-pause/
endif
	dh_installexamples
	dh_installmenu -A
	dh_installman -A
	dh_installchangelogs non-esd/$(PACKAGE)-$(VERSION)/ChangeLog
	dh_installchangelogs esd/$(PACKAGE)-$(VERSION)/ChangeLog -p $(PACKAGE)-esd
ifeq ($(WITHPAUSE),yes)
	dh_installchangelogs pause/$(PACKAGE)-$(VERSION)/ChangeLog -p $(PACKAGE)-pause
endif
#	dh_link
	dh_strip
	dh_compress
	dh_fixperms
	# You may want to make some executables suid here
	-chown root:games debian/$(PACKAGE)/usr/games/$(PACKAGE)
	-chmod g+s debian/$(PACKAGE)/usr/games/$(PACKAGE)
	-chown root:games debian/$(PACKAGE)-esd/usr/games/$(PACKAGE)
	-chmod g+s debian/$(PACKAGE)-esd/usr/games/$(PACKAGE)
ifeq ($(WITHPAUSE),yes)
	-chown root:games debian/$(PACKAGE)-pause/usr/games/$(PACKAGE)
	-chmod g+s debian/$(PACKAGE)-pause/usr/games/$(PACKAGE)
endif

	dh_installdeb -p$(PACKAGE) -Pdebian/$(PACKAGE)
	dh_shlibdeps -p$(PACKAGE) -Pdebian/$(PACKAGE)
	dh_gencontrol -p$(PACKAGE) -Pdebian/$(PACKAGE)
	dh_md5sums -p$(PACKAGE) -Pdebian/$(PACKAGE)
	dh_builddeb -p$(PACKAGE) -Pdebian/$(PACKAGE)

	dh_installdeb -p$(PACKAGE)-esd -Pdebian/$(PACKAGE)-esd
	dh_shlibdeps -p$(PACKAGE)-esd -Pdebian/$(PACKAGE)-esd
	dh_gencontrol -p$(PACKAGE)-esd -Pdebian/$(PACKAGE)-esd
	dh_md5sums -p$(PACKAGE)-esd -Pdebian/$(PACKAGE)-esd
	dh_builddeb -p$(PACKAGE)-esd -Pdebian/$(PACKAGE)-esd

ifeq ($(WITHPAUSE),yes)
	dh_installdeb -p$(PACKAGE)-pause -Pdebian/$(PACKAGE)-pause
	dh_shlibdeps -p$(PACKAGE)-pause -Pdebian/$(PACKAGE)-pause
	dh_gencontrol -p$(PACKAGE)-pause -Pdebian/$(PACKAGE)-pause
	dh_md5sums -p$(PACKAGE)-pause -Pdebian/$(PACKAGE)-pause
	dh_builddeb -p$(PACKAGE)-pause -Pdebian/$(PACKAGE)-pause
endif

source diff:                                                                  
	@echo >&2 'source and diff are obsolete - use dpkg-source -b'; false

binary: binary-indep binary-arch
.PHONY: autotools build clean binary-indep binary-arch binary
