#!/usr/bin/make -f
# Made with the aid of debmake, by Christoph Lameter,
# games on the sample debian/rules file for GNU hello by Ian Jackson.

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

# This is the debhelper compatibility version to use.
export DH_COMPAT=4

# This has to be exported to make some magic below work.
export DH_OPTIONS

# These are used for cross-compiling and for saving the configure script
# from having to guess our platform (since we know it already)
DEB_HOST_GNU_TYPE       ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE      ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

# To whoever changed this behind my back: your change caused a reversion
# to an old version of Debian policy.  Look at recent policy: debug
# is obsolete, nostrip is handled automatically by debhelper, and
# noopt has been added.  And the default flags _should_ be -g -Wall -O2.

CFLAGS = -g -Wall
CXXFLAGS = -g -Wall

ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
  CFLAGS += -O0
  CXXFLAGS += -O0
else
  CFLAGS += -O2
  CXXFLAGS += -O2
endif

ifeq ($(DEB_BUILD_GNU_TYPE),i386-linux)
  # Uncomment the following 2 lines to enable i686 optimziation
  # You may want to tweak the settings to your likings
  #CFLAGS=-g -Wall -O3 -march=i686 -ffast-math -finline-functions
  #CXXFLAGS=-g -Wall -O3 -march=i686 -ffast-math -finline-functions
endif

export CFLAGS
export CXXFLAGS

objdir = $(CURDIR)/obj-$(DEB_BUILD_GNU_TYPE)

-include debian/debiandirs
export kde_bindir=/usr/games

debian/debiandirs: admin/debianrules
	perl -w admin/debianrules echodirs > debian/debiandirs

configure: configure-stamp
configure-stamp:
	dh_testdir

# KDE CVS does not have aclocal.m4 or configure
# But upstream tarball does
# However, they're out of date...
	if test ! -f configure; then \
		$(MAKE) -f admin/Makefile.common; \
	fi

# make build directory
	mkdir -p $(objdir)

# run configure with build tree $(objdir)
	cd $(objdir) && \
	../configure $(configkde) --enable-shared --enable-static $(XINERAMA)

	touch configure-stamp

build: build-stamp
build-stamp: configure-stamp
	dh_testdir

	$(MAKE) -C $(objdir)

	touch build-stamp

clean:
	dh_testdir
	dh_testroot

	-rm -f build-stamp configure-stamp debian/debiandirs

# Remove old files in debian/
	-rm -f debian/kdecarddecks.*

	$(MAKE) -f admin/Makefile.common cvs-clean

# Remove build tree
	rm -rf $(objdir)
	dh_clean

install: build-stamp
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs

	$(MAKE) -C $(objdir) install DESTDIR=$(CURDIR)/debian/tmp/

# Put full paths to games into desktop files
	perl -pi -e 's:^Exec=:Exec=/usr/games/:' \
		`find debian/tmp/usr/share/applnk -name \*.desktop -print`

	dh_install --list-missing

# Build HTML docs
	set -e; \
	for pkg in `cd debian/tmp/usr/share/doc/kde/HTML/en; echo *`; do \
	  mkdir -p $(CURDIR)/debian/kdegames-doc-html/usr/share/doc/kde/HTML/en/$$pkg; \
	  cd $(CURDIR)/debian/kdegames-doc-html/usr/share/doc/kde/HTML/en/$$pkg; \
	  meinproc $(CURDIR)/debian/tmp/usr/share/doc/kde/HTML/en/$$pkg/index.docbook; \
	  mkdir -p $(CURDIR)/debian/$$pkg/usr/share/doc/$$pkg; \
	  ln -s ../kde/HTML/en/$$pkg $(CURDIR)/debian/$$pkg/usr/share/doc/$$pkg/html; \
	done

# Generate man pages for the games
	set -e; while read package binary; do \
		install -d -m755 debian/$$package/usr/share/man/man6; \
		soelim -Idebian debian/$$binary.man \
		 >debian/$$package/usr/share/man/man6/$$binary.6; \
	done <debian/manpages.in

# One doc file has to be renamed because there are 2 TODO files
# in libkdegames-dev.  dh_installdocs will take care of installing
# the rest of this directory later.
	install -d -m 755 debian/libkdegames-dev/usr/share/doc/libkdegames-dev
	install -m 644 libkdegames/kgame/TODO \
		debian/libkdegames-dev/usr/share/doc/libkdegames-dev/TODO.kgame

# This single target is used to build all the packages, all at once, or
# one at a time. So keep in mind: any options passed to commands here will
# affect _all_ packages. Anything you want to only affect one package
# should be put in another target, such as the install target.
binary-common:
	dh_testdir
	dh_testroot
	for p in `dh_listpackages`; do \
	    case $$p in \
	    kdegames) \
		DH_OPTIONS="-p$$p" dh_installchangelogs ChangeLog \
		;; \
	    atlantik | kasteroids | kbattleship | kenolaba | kjumpingcube | \
	    kmahjongg | konquest | kpoker | kreversi | ksame | kshisen | \
	    ksnake | kspaceduel | ktron) \
		DH_OPTIONS="-p$$p" dh_installchangelogs $$p/ChangeLog \
		;; \
	    kfouleggs | klickety | kmines | ksirtet) \
		DH_OPTIONS="-p$$p" dh_installchangelogs $$p/CHANGELOG \
		;; \
	    kblackbox | kpat) \
		DH_OPTIONS="-p$$p" dh_installchangelogs $$p/CHANGES \
		;; \
	    *) \
		DH_OPTIONS="-p$$p" dh_installchangelogs \
		;; \
	    esac; \
	done
	dh_installdocs
#	dh_installexamples
	dh_installmenu
#	dh_installdebconf
#	dh_installlogrotate
#	dh_installemacsen
#	dh_installpam
	dh_installmime
#	dh_installinit
#	dh_installcron
#	dh_installinfo
	dh_installman
	dh_strip
	dh_link
	dh_compress -X.docbook -X.css -X-license -X.dcl -X.bz2
	dh_fixperms
	for p in `dh_listpackages`; do \
	    case $$p in \
	    libkdegames1) \
		DH_OPTIONS="-p$$p" dh_makeshlibs -V'libkdegames1 (>= 4:3.1.0)' \
		;; \
	    atlantik | kbackgammon | kolf) \
		DH_OPTIONS="-p$$p" dh_makeshlibs -V \
		;; \
	    esac; \
	done
	dh_installdeb
#	dh_perl
	dh_shlibdeps -ldebian/libkdegames1/usr/lib:debian/atlantik/usr/lib:debian/kolf/usr/lib
	dh_gencontrol
	dh_md5sums
	dh_builddeb

# Build architecture independant packages using the common target.
binary-indep: build install
	$(MAKE) -f debian/rules DH_OPTIONS=-i binary-common

# Build architecture dependant packages using the common target.
binary-arch: build install
	$(MAKE) -f debian/rules DH_OPTIONS=-a binary-common

# Any other binary targets build just one binary package at a time.
binary-%: build install
	$(MAKE) -f debian/rules binary-common DH_OPTIONS=-p$*

binary: binary-indep binary-arch
.PHONY: build clean binary-indep binary-arch binary install configure
