#!/usr/bin/make -f
# -*- makefile -*-
# Sample debian/rules that uses debhelper.
# This file was originally written by Joey Hess and Craig Small.
# As a special exception, when this file is copied by dh-make into a
# dh-make output file, you may use that output file without restriction.
# This special exception was added by Craig Small in version 0.37 of dh-make.

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

include /usr/share/dpkg/architecture.mk

TARGET := profile-build ARCH=general-$(DEB_HOST_ARCH_BITS)

ifeq (i386,$(DEB_HOST_ARCH_CPU))
TARGET := profile-build ARCH=x86-32-old
endif

ifneq (,$(findstring amd64,$(DEB_HOST_ARCH)))
TARGET := profile-build ARCH=x86-64
endif

# no compile on the first shot so building without profile
ifeq (hppa,$(DEB_HOST_ARCH_CPU))
TARGET := build ARCH=general-$(DEB_HOST_ARCH_BITS)
endif

# no compile on the first shot
ifneq (,$(findstring powerpc,$(DEB_HOST_ARCH_CPU)))
TARGET := build ARCH=general-$(DEB_HOST_ARCH_BITS)
export DEB_LDFLAGS_MAINT_APPEND=-latomic
endif

ifneq (,$(findstring s390,$(DEB_HOST_ARCH_CPU)))
TARGET := profile-build ARCH=general-$(DEB_HOST_ARCH_BITS)
endif

ifneq (,$(findstring sparc,$(DEB_HOST_ARCH_CPU)))
TARGET := build ARCH=general-$(DEB_HOST_ARCH_BITS)
endif

ifneq (,$(findstring mips,$(DEB_HOST_ARCH_CPU)))
TARGET := profile-build ARCH=general-$(DEB_HOST_ARCH_BITS)
export DEB_LDFLAGS_MAINT_APPEND=-latomic
endif

# Profiling fails due to compiler issues
ifneq (,$(findstring mipsel,$(DEB_HOST_ARCH_CPU)))
TARGET := build ARCH=general-$(DEB_HOST_ARCH_BITS)
export DEB_LDFLAGS_MAINT_APPEND=-latomic
endif

ifeq (alpha,$(DEB_HOST_ARCH_CPU))
TARGET := build ARCH=general-$(DEB_HOST_ARCH_BITS)
endif

ifneq (,$(findstring armel,$(DEB_HOST_ARCH)))
TARGET := build ARCH=general-$(DEB_HOST_ARCH_BITS)
export DEB_LDFLAGS_MAINT_APPEND=-latomic
endif

ifneq (,$(findstring armhf,$(DEB_HOST_ARCH)))
TARGET := build ARCH=general-$(DEB_HOST_ARCH_BITS)
endif

ifeq (ia64,$(DEB_HOST_ARCH_CPU))
TARGET := profile-build ARCH=general-$(DEB_HOST_ARCH_BITS) optimize=no
endif

ifeq (m68k,$(DEB_HOST_ARCH_CPU))
TARGET := build ARCH=general-$(DEB_HOST_ARCH_BITS)
export DEB_LDFLAGS_MAINT_APPEND=-latomic
endif

ifeq (sh4,$(DEB_HOST_ARCH_CPU))
TARGET := build ARCH=general-$(DEB_HOST_ARCH_BITS)
export DEB_LDFLAGS_MAINT_APPEND=-latomic
endif

ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
TARGET2 := $(TARGET:profile-build=build)
else
TARGET2 := $(TARGET)
endif


ifneq (,$(findstring nocheck,$(DEB_BUILD_OPTIONS)))
TESTCMD := 
else
TESTCMD := cd src && ./stockfish bench 32 1 10 default depth
endif


%:
	dh $@ --sourcedirectory=src

override_dh_auto_clean:
	cd src && rm -rf *.gcda && touch .depend && make clean

override_dh_auto_build:
	dh_auto_build --sourcedirectory=src -- $(TARGET2)

override_dh_auto_test:
	$(TESTCMD)

override_dh_installman:
	dh_installman debian/stockfish.6
