#!/usr/bin/make -f

build: build-stamp
build-stamp:
	dh_testdir

	# Set up wordlist
	cat /usr/share/dict/american-english /usr/share/dict/british-english | \
		sort | uniq > temp-dictionary

	./configure
	$(MAKE)
	$(MAKE) test
	touch build-stamp

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp
	dh_clean temp-dictionary
	-$(MAKE) -i distclean

binary-indep: build

binary-arch: build
	dh_testdir
	dh_testroot
	dh_clean
	dh_installdirs usr/share/doc/bsdgames \
		usr/share/games/bsdgames/phantasia \
		var/games/bsdgames/hack/save

	$(MAKE) install
	cp atc/BUGS debian/bsdgames/usr/share/doc/bsdgames/BUGS.atc
	cp hunt/README debian/bsdgames/usr/share/doc/bsdgames/README.hunt
	cp phantasia/README debian/bsdgames/usr/share/doc/bsdgames/README.phantasia
	cp boggle/README debian/bsdgames/usr/share/doc/bsdgames/README.boggle
	cp hack/Original_READ_ME debian/bsdgames/usr/share/doc/bsdgames/Original_READ_ME.hack
	cp hack/READ_ME debian/bsdgames/usr/share/doc/bsdgames/READ_ME.hack
	# Move phantasia's binary state files into /usr, they are copied
	# back to /var in the postinst.
	mv debian/bsdgames/var/games/bsdgames/phantasia/monsters \
	   debian/bsdgames/var/games/bsdgames/phantasia/void \
	   debian/bsdgames/usr/share/games/bsdgames/phantasia/

	dh_installdocs ChangeLog ChangeLog.0 TODO README SECURITY THANKS \
		AUTHORS BUGS YEAR2000
	cat COPYING >> debian/bsdgames/usr/share/doc/bsdgames/copyright
	dh_installexamples
	dh_installmenu
	dh_installcron
	dh_installchangelogs -k NEWS
	dh_strip
	dh_compress
	dh_fixperms

	chmod -R u+rw debian/bsdgames/usr/
	# Games with score files need to be set up sgid games.
	chown root:games debian/bsdgames/usr/games/atc \
		debian/bsdgames/usr/games/battlestar \
		debian/bsdgames/usr/games/canfield \
		debian/bsdgames/usr/games/cribbage \
		debian/bsdgames/usr/games/robots \
		debian/bsdgames/usr/games/snake \
		debian/bsdgames/usr/games/tetris-bsd \
		debian/bsdgames/usr/games/phantasia \
		debian/bsdgames/usr/games/sail \
		debian/bsdgames/usr/games/hack
	chmod g+s debian/bsdgames/usr/games/atc \
		debian/bsdgames/usr/games/battlestar \
		debian/bsdgames/usr/games/canfield \
		debian/bsdgames/usr/games/cribbage \
		debian/bsdgames/usr/games/robots \
		debian/bsdgames/usr/games/snake \
		debian/bsdgames/usr/games/tetris-bsd \
		debian/bsdgames/usr/games/phantasia \
		debian/bsdgames/usr/games/sail \
		debian/bsdgames/usr/games/hack
	# The game directories need to be sgid too, and let's
	# remove all the zero byte score files in them.
	chown root:games debian/bsdgames/var/games/bsdgames/ \
		debian/bsdgames/var/games/bsdgames/sail \
		debian/bsdgames/var/games/bsdgames/phantasia \
		debian/bsdgames/var/games/bsdgames/hack \
		debian/bsdgames/var/games/bsdgames/hack/save
	chmod g+rws debian/bsdgames/var/games/bsdgames/ \
		debian/bsdgames/var/games/bsdgames/sail \
		debian/bsdgames/var/games/bsdgames/phantasia \
		debian/bsdgames/var/games/bsdgames/hack \
		debian/bsdgames/var/games/bsdgames/hack/save
	find debian/bsdgames/var/games/bsdgames -size 0 -exec rm {} \;

	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

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