#!/usr/bin/make -f

-include /usr/share/dpkg/buildtools.mk

CFLAGS=-O2 -DUSE_IPV6 -fno-strength-reduce -Wall -fsigned-char -I/usr/include -L/usr/lib
DESTDIR=$(CURDIR)/debian/tetrinetx/
OWNDIRS= $(DESTDIR)var/games/tetrinetx \
	 $(DESTDIR)var/log/tetrinetx \
	 $(DESTDIR)etc/tetrinetx

ifneq (,$(findstring debug,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -g
endif
ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
	INSTALL_PROGRAM += -s
endif

configure: configure-stamp
configure-stamp:
	dh_testdir

	cp debian/config.h src/

	touch configure-stamp

build: build-stamp

build-stamp: configure-stamp bin/tetrinetx
	dh_testdir
	touch build-stamp

bin/tetrinetx: $(wildcard src/*.[ch])
	$(CC) $(CFLAGS) src/main.c -o $@ -ladns

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp configure-stamp

	-rm -f src/.deps/main.P bin/tetrinetx

	dh_clean

install: build-stamp
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs
	install -o games -g games -m 0755 bin/tetrinetx $(DESTDIR)/usr/games/tetrinetx
	install -o games -g games -m 0644 bin/game.motd bin/game.pmotd bin/game.conf $(DESTDIR)etc/tetrinetx
	install -o games -g games -m 0600 bin/game.secure $(DESTDIR)etc/tetrinetx
	chown -R games:games $(OWNDIRS)

binary-indep:

binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installdocs
	dh_installexamples
	chmod 0644 $(DESTDIR)usr/share/doc/tetrinetx/examples/*
	dh_installinit
	dh_installlogrotate
	dh_installman
	dh_installchangelogs ChangeLog
	dh_link
	dh_strip
	dh_compress
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

build-arch: build
build-indep: build

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