#! /usr/bin/make -f

#include /usr/share/quilt/quilt.make

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

CFLAGS = -g -Wall
LDFLAGS = -Wl,--as-needed

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

configure: configure-stamp
configure-stamp:
	dh_testdir
	ln -s /usr/share/misc/config.guess /usr/share/misc/config.sub .
	CFLAGS="$(CFLAGS)" LDFLAGS="$(LDFLAGS)" ./configure \
		--host=$(DEB_HOST_GNU_TYPE) --build=$(DEB_BUILD_GNU_TYPE) \
		--prefix=/usr --bindir=\$${prefix}/games
	touch $@

build: build-stamp
build-stamp: configure
	dh_testdir
	$(MAKE)
	touch $@

clean: 
	dh_testdir
	dh_testroot
	rm -f build-stamp
	rm -f configure-stamp
	[ ! -f Makefile ] || $(MAKE) distclean
	rm -f src/GCint.h config.guess config.sub src/lexpgn.c
	dh_clean

install: build
	dh_testdir
	dh_testroot
	dh_prep
	$(MAKE) install DESTDIR=$(CURDIR)/debian/gnuchess-opt/

	#optify
	rm -rf $(CURDIR)/debian/gnuchess-opt/usr/share/
	mkdir -p $(CURDIR)/debian/gnuchess-opt/opt/gnuchess
	mkdir -p $(CURDIR)/debian/gnuchess-opt/usr/bin
	mv $(CURDIR)/debian/gnuchess-opt/usr/games/* $(CURDIR)/debian/gnuchess-opt/opt/gnuchess
	ln -s /opt/gnuchess/gnuchess $(CURDIR)/debian/gnuchess-opt/usr/bin/gnuchess
	ln -s /opt/gnuchess/gnuchessx $(CURDIR)/debian/gnuchess-opt/usr/bin/gnuchessx

	rmdir $(CURDIR)/debian/gnuchess-opt/usr/games

binary-indep: build install

binary-arch: build install
	dh_testdir
	dh_testroot
	#dh_installdocs
	#dh_installmenu
	#dh_installman debian/gnuchess.6 debian/gnuchessx.6
	#dh_installchangelogs ChangeLog
	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 install
