#!/usr/bin/make -f

export CFLAGS=-Wall -g -DXF86VIDMODE
  
ifneq (,$(filter noopt,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -O0
else
	CFLAGS += -O2
endif
ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
	NUMJOBS = $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
	MAKEFLAGS += -j$(NUMJOBS)
endif

build: build-arch build-indep
build-arch: build-stamp
build-indep: build-stamp
build-stamp:
	dh_testdir
	ln -sf /usr/share/misc/config.sub
	ln -sf /usr/share/misc/config.guess
	LIBS=-lXxf86vm ./configure \
		--prefix=/usr/share/games/xgalaga \
		--exec-prefix=/usr/games CFLAGS="$(CFLAGS)" 
	# hack SOUNDDEFS so we don't need symlinks
	$(MAKE) SOUNDDEFS=-DSOUNDSERVER=\\\"/usr/lib/games/xgalaga/$$\(SOUNDSRV\)\\\"
	touch build-stamp

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp
	if [ -e Makefile ]; then $(MAKE) -i distclean; fi
	dh_clean xgal.sndsrv.oss xgal.sndsrv.freebsd xgal.sndsrv.linux config.k.bak config.sub config.guess

binary-indep: build-indep

binary-arch: build-arch
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs usr/lib/games/xgalaga usr/games usr/share/man/man6
	$(MAKE) DESTDIR=`pwd`/debian/xgalaga install
	dh_link usr/share/games/xgalaga/CREDITS usr/share/doc/xgalaga/CREDITS
	# move the sound-server binary out of usr/share
	mv debian/xgalaga/usr/share/games/xgalaga/xgal.sndsrv.* \
		debian/xgalaga/usr/lib/games/xgalaga
	find debian/xgalaga/usr/share/games/xgalaga -type f | xargs chmod 644
	dh_install debian/xgalaga-icon.xpm usr/share/pixmaps
	dh_install debian/xgalaga.desktop usr/share/applications
	dh_install debian/xgalaga-hyperspace.desktop usr/share/applications
	dh_link usr/share/man/man6/xgalaga.6x usr/share/man/man6/xgalaga-hyperspace.6x
	dh_installdocs README* libsprite/README.libsprite
	dh_installexamples
	dh_installmenu
	dh_installchangelogs CHANGES
	dh_strip
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

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