#! /usr/bin/make -f
# Based on a sample debian/rules that uses debhelper.
# This file is public domain software, originally written by Joey Hess. 

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

# This is the debhelper compatibility version to use.
export DH_COMPAT=4

# These are used for cross-compiling and for saving the configure script
# from having to guess our platform (since we know it already)
DEB_HOST_GNU_TYPE   ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

CC      := gcc
CFLAGS  := -g -Wall -W
LDFLAGS :=

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

# Use soft-float and thumb mode if it enabled.
ifneq (,$(findstring thumb,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -mthumb
endif


INSTALL := install
INSTALL_FILE    := $(INSTALL) -p    -o root -g root  -m  644
INSTALL_PROGRAM := $(INSTALL) -p    -o root -g root  -m  755
INSTALL_SCRIPT  := $(INSTALL) -p    -o root -g root  -m  755
INSTALL_DIR     := $(INSTALL) -p -d -o root -g root  -m  755

export CC CFLAGS LDFLAGS 
export INSTALL INSTALL_FILE INSTALL_PROGRAM INSTALL_SCRIPT INSTALL_DIR

build: build-stamp
build-stamp: config.status
	dh_testdir
	# Commands to compile the package.
	$(MAKE)
	touch build-stamp

config.status: configure
	dh_testdir
	ln -s /usr/share/misc/config.{guess,sub} .
	./configure \
	  --host=$(DEB_HOST_GNU_TYPE) \
	  --build=$(DEB_BUILD_GNU_TYPE) \
	  --prefix=/usr \
	  --bindir=\$${prefix}/games \
    --without-readline

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp
	-$(MAKE) distclean
	rm -f src/GCint.h config.{guess,sub} src/lexpgn.c
	dh_clean

install: build
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs

	# Commands to install the package into debian/<packagename>

	$(MAKE) install prefix=$(CURDIR)/debian/gnuchess/usr
#	$(INSTALL_SCRIPT) debian/gnuchessx $(CURDIR)/debian/gnuchess/usr/games

# Build architecture-independent files here.
binary-indep: build install
# We have nothing to do by default.

# Build architecture-dependent files here.
binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installdocs
	$(INSTALL_FILE) doc/README $(CURDIR)/debian/gnuchess/usr/share/doc/gnuchess/MANUAL
	dh_installmenu
	dh_installman debian/gnuchess.6 debian/gnuchessx.6
	dh_installchangelogs ChangeLog
	dh_strip --dbg-package=gnuchess
	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
