#!/usr/bin/make -f
# Made with the aid of dh_make, by Craig Small
# Sample debian/rules that uses debhelper. GNU copyright 1997 by Joey Hess.
# Also some stuff taken from debmake scripts, by Cristopt Lameter.

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

PACKAGE=freeciv

INSTALL = install
INSTALL_DIR = $(INSTALL) -p -d -o root -g root  -m  755

DEB_HOST_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

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

COMMON_CONFIG := --cache-file=$(CURDIR)/config.cache \
		 --sysconfdir=/etc --prefix=/usr \
		 --datadir=\$${prefix}/share/games \
		 --bindir=\$${prefix}/games --mandir=\$${prefix}/share/man \
		 --host $(DEB_BUILD_GNU_TYPE) --build $(DEB_HOST_GNU_TYPE) \
		 --enable-debug=no

build: build-xaw3d build-gtk build-sdl build-server

build-server:
	dh_testdir

	-rm -f config.sub config.guess
	ln -sf /usr/share/misc/config.sub bootstrap/config.sub
	ln -sf /usr/share/misc/config.guess bootstrap/config.guess

	mkdir build-server
	(cd build-server && ../configure $(COMMON_CONFIG) --disable-client \
		--without-ggz-client && \
	$(MAKE) CFLAGS="$(CFLAGS)")

	touch build-server-stamp

build-xaw3d:
	dh_testdir

	-rm -f config.sub config.guess
	ln -sf /usr/share/misc/config.sub bootstrap/config.sub
	ln -sf /usr/share/misc/config.guess bootstrap/config.guess

	mkdir build-xaw3d
	(cd build-xaw3d && 	\
	 ../configure $(COMMON_CONFIG) --disable-server --enable-client=xaw \
	 	--with-xaw3d --without-ggz-client --without-ggz-server && \
	$(MAKE) CFLAGS="$(CFLAGS)")

	touch build-xaw3d-stamp

build-gtk:
	dh_testdir

	-rm -f config.sub config.guess
	ln -sf /usr/share/misc/config.sub bootstrap/config.sub
	ln -sf /usr/share/misc/config.guess bootstrap/config.guess

	mkdir build-gtk
	(cd build-gtk && \
	../configure $(COMMON_CONFIG) --disable-server --enable-client=gtk-2.0 \
		--without-ggz-server --enable-noregistry && \
	$(MAKE) CFLAGS="$(CFLAGS)")
	
	touch build-gtk-stamp

build-sdl:
	dh_testdir

	-rm -f config.sub config.guess
	ln -sf /usr/share/misc/config.sub bootstrap/config.sub
	ln -sf /usr/share/misc/config.guess bootstrap/config.guess

	mkdir build-sdl
	(cd build-sdl && \
	../configure $(COMMON_CONFIG) --disable-server --enable-client=sdl \
		--without-ggz-client --without-ggz-server && \
	$(MAKE) CFLAGS="$(CFLAGS)")
	
	touch build-sdl-stamp

clean:
	dh_testdir
	dh_testroot
	
	-rm -f install-stamp build-server-stamp build-xaw3d-stamp build-gtk-stamp build-sdl-stamp
	-rm -f config.cache config.sub config.guess
	-rm -f bootstrap/config.sub bootstrap/config.guess
	-rm -rf build-stamp build-xaw3d build-gtk build-sdl build-server 

	dh_clean

	-rm -f debian/*~ Makefile Makefile.bak core */Makefile

install: install-stamp
install-stamp: build-server-stamp build-gtk-stamp build-xaw3d-stamp build-sdl-stamp
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs

	(cd build-server; $(MAKE) DESTDIR=$(CURDIR)/debian/tmp \
		localedir=$(DESTDIR)/usr/share/locale install)

	(cd build-gtk; $(MAKE) DESTDIR=$(CURDIR)/debian/tmp \
		localedir=$(DESTDIR)/usr/share/locale install)
	$(INSTALL_DIR) $(CURDIR)/debian/tmp/usr/share/ggz/modules/freeciv
	cp build-gtk/data/civclient.dsc debian/tmp/usr/share/ggz/modules/freeciv
	cp debian/tmp/usr/share/applications/freeciv.desktop \
           debian/tmp/usr/share/applications/freeciv-gtk.desktop

	(cd build-sdl; $(MAKE) DESTDIR=$(CURDIR)/debian/tmp \
		localedir=$(DESTDIR)/usr/share/locale install)
	sed 's/Exec=freeciv-gtk2/Exec=freeciv-sdl/' \
	   debian/tmp/usr/share/applications/freeciv.desktop \
           >debian/tmp/usr/share/applications/freeciv-sdl.desktop

	(cd build-xaw3d; $(MAKE) DESTDIR=$(CURDIR)/debian/tmp \
		localedir=$(DESTDIR)/usr/share/locale install)
	sed 's/Exec=freeciv-gtk2/Exec=freeciv-xaw/' \
	   debian/tmp/usr/share/applications/freeciv.desktop \
           >debian/tmp/usr/share/applications/freeciv-xaw3d.desktop
	rm debian/tmp/usr/share/applications/freeciv.desktop
	
	# Move stuff that gets lost thanks to ${datadir}
	mv debian/tmp/usr/share/games/icons debian/tmp/usr/share

	dh_install

	# Remove sounds from -data
	rm -rf debian/freeciv-data/usr/share/games/freeciv/stdsounds*

	# Remove ttf fonts from -data, now provided by dependencies.
	rm -rf debian/freeciv-data/usr/share/games/freeciv/themes/gui-sdl/human/*.ttf
	rm -rf debian/freeciv-data/usr/share/games/freeciv/themes/gui-sdl/human/COPYING.*

	touch install-stamp

binary-indep: build install
	dh_testdir -i
	dh_testroot -i
	dh_installdocs -i
	dh_installchangelogs -i
	dh_installmenu -i
	dh_installman -i
	dh_compress -i
	dh_fixperms -i
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

binary-arch: build install
	dh_testdir -a
	dh_testroot -a
#	dh_installdocs -a
#	dh_installexamples -a
#	dh_installchangelogs -a ChangeLog
	# Remove doc dirs for arch-dependent packages and symlink
	# to freeciv-data's.
	for i in freeciv-server freeciv-client-gtk freeciv-client-xaw3d freeciv-client-sdl; do \
		rm -rf $(CURDIR)/debian/$i/usr/share/doc/$i; \
	done
	dh_link -a
	dh_installmenu -a
	dh_strip -a
	dh_compress -a
	dh_fixperms -a
	dh_installdeb -a
	dh_shlibdeps -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

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