#!/usr/bin/make -f

# Uncomment this to compile with gcc-snapshot.
#export  CC=/usr/lib/gcc-snapshot/bin/gcc
#export CXX=/usr/lib/gcc-snapshot/bin/g++
#export CPP=/usr/lib/gcc-snapshot/bin/cpp

# Uncomment for particular version of gcc
#export  CC := gcc-4.7
#export CXX := g++-4.7
#export CPP := cpp-4.7

# Uncomment for clang (llvm)
#export CC  = clang
#export CXX = clang

# Set default flags with dpkg-buildflags
export DEB_BUILD_MAINT_OPTIONS = hardening=+all
DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/buildflags.mk

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


# PKG = boinc-app-seti

export CPPFLAGS := $(shell dpkg-buildflags --get CPPFLAGS)
export CFLAGS   := $(shell dpkg-buildflags --get CFLAGS)
export CXXFLAGS := $(shell dpkg-buildflags --get CXXFLAGS)
export LDFLAGS  := $(shell dpkg-buildflags --get LDFLAGS)

# Uncomment this to enable compilation with UltraSPARC specific compiler
# flags. This will most probably increase setiathome's performance or if
# your are unlucky will break it.
#
#CPUTYPE = $(shell head -1 /proc/cpuinfo | cut -f 2 -d ':' | cut -f 3 -d '\' )
#ifeq (UltraSparc, $(CPUTYPE))
#  CPUSUBTYPE = $(shell head -1 /proc/cpuinfo | cut -f 2 -d ':' | \
#    cut -f 4 -d '\' | cut -f 1 -d '+')
#  CFLAGS += -mcpu=v9
#  CXXFLAGS += -mcpu=v9
#  ifeq (III, $(CPUSUBTYPE))
#    CFLAGS += -mcpu=ultrasparc3
#    CXXFLAGS += -mcpu=ultrasparc3
#  else
#    CFLAGS += -mcpu=ultrasparc
#    CXXFLAGS += -mcpu=ultrasparc
#  endif
#  CFLAGS += -m64
#  CXXFLAGS += -m64
#  DEB_CONFIGURE_EXTRA_FLAGS += --enable-bitness=64
#endif


# Append this to CFLAGS and CXXFLAGS.
CFLAGS_APPEND += -I/usr/include/boinc -I/usr/include/boinc/lib -I/usr/include/boinc/api

ifneq (clang,$(CXX))
CFLAGS_APPEND += -O3
#CFLAGS_APPEND += -mnative
#CFLAGS_APPEND += -mno-avx
#CFLAGS_APPEND += -msse4.2
GCCVERSION=$(shell $(CXX) --version | tr " " "\n" | egrep '^[0-9]+\.[0-9]+\.[0-9]+$$' | cut -f1,2 -d.)
# gcc 4.6 on precise has issues while gcc 4.7 and 4.8 on sid are just fine with -mno-avx set on non bulldozer/SandyBridge machines
# CFLAGS_APPEND += -flto
# LDFLAGS += -flto
endif

ifeq (clang,$(CXX))
GCCVERSION=$(shell $(CXX) --version | tr " " "\n" | egrep '^[0-9]+\.[0-9]+-[0-9]+$$' | cut -f1 -d-)
CFLAGS_APPEND += -O3
# -O4 implies link-time optimisation with clang, fails
endif

# Although all source file are C++, some handwritten rules in
# client/Makefile.am seem to use CFLAGS only.
CFLAGS   += $(CFLAGS_APPEND)
CXXFLAGS += $(CFLAGS_APPEND)

# This is a shame, but there is yet no AltiVec for Linux PowerPC with SETI
SETIEXTRAFLAGS= --disable-altivec

ifeq ($(DEB_BUILD_GNU_CPU),x86_64)
  SETIEXTRAFLAGS += --enable-sse3
  SETIEXTRAFLAGS += --disable-avx
#  SETIEXTRAFLAGS += --enable-sse2 --enable-sse --enable-mmx
endif

ifeq ($(DEB_BUILD_GNU_CPU),i686)
  SETIEXTRAFLAGS += --enable-sse2 --enable-sse --enable-mmx
endif


# Get upstream version from debian version.
UPSTREAM_VERSION = $(shell dpkg-parsechangelog | egrep '^Version:' | \
  cut -d ' ' -f 2 | cut -d '-' -f 1 | cut -d '+' -f 1 | cut -d '~' -f 1 | perl -pe 's/^.*?://')
# Remove dot "." in upstream version for use in app_info.xml
APP_INFO_VERSION = $(subst .,,$(UPSTREAM_VERSION))


%:
	dh $@ --parallel

override_dh_auto_configure:
	aclocal -I m4 && autoheader && automake && autoconf
	# Remember to not configure with --disable-dependency-tracking, because
	# it breaks some handwritten rules in client/Makefile.am which require
	# that DEPDIR exists.
	CFLAGS="$(CFLAGS)" CXXFLAGS="$(CXXFLAGS)" CPPFLAGS="$(CPPFLAGS)" LDFLAGS="$(LDFLAGS)" \
	BOINCDIR=/usr/include/boinc ./configure \
	  --disable-static-client \
	  --disable-static \
	  --enable-shared \
	  --enable-client \
	  --disable-graphics \
	  --disable-server \
	  --enable-dependency-tracking \
	  --with-gnu-ld $(SETIEXTRAFLAGS)

override_dh_auto_clean:
	if [ -r Makefile ]; then make clean; fi
	find . -name "*.o" -delete
	find . -name ".deps" -a -type d | xargs -r rm -r
	rm -f client/seti_boinc client/hires_timer_test tools/fakedata tools/workunit_resample tools/workunit_to_ascii
	dh_clean \
	  client/setiathome-$(UPSTREAM_VERSION).* \
	  client/setiathome_enhanced \
	  debian/extra/app_info.xml \
	  client/setiathome-7.0.x86_64-pc-linux-gnu*
	# may be seen after invoking binary
	rm -f client/boinc_finish_called client/result.sah client/state.sah client/stderr.txt client/wisdom.sah client/work_unit.sah client/boinc_lockfile client/project_specific_defines.h
	# generated by autoconf
	rm -f  Makefile Makefile.in aclocal.m4 assimilator/Makefile assimilator/Makefile.in client/Makefile client/Makefile.in config.log config.status configure db/Makefile db/schema_to_class db/tools/Makefile jpeglib/Makefile jpeglib/Makefile.in libtool sah_config.h.in stamp-h1 tools/Makefile tools/Makefile.in validate/Makefile splitter_fft/Makefile splitter_fft/Makefile.in splitter_pfb/Makefile splitter_pfb/Makefile.in
	rm -f sah_config.h
	rm -f time_stats_log boinc_lockfile lockfile gui_rpc_auth.cfg stderr.txt

override_dh_install: 
	cp -f client/seti_boinc client/setiathome_enhanced
	cat debian/extra/app_info.xml.in | \
	  sed -e 's/VERSION/$(APP_INFO_VERSION)/' > debian/extra/app_info.xml
	dh_install


override_dh_strip:
	dh_strip --dbg-package=boinc-app-seti-dbg


origDir ?= .

get-orig-source:
	if [ ! -d "$(origDir)" ]; then \
	  echo "Destination directory '$(origDir)' does not exist."; \
	  exit; \
	fi; \
	cd $(origDir); \
	EXP=/tmp/download-seti; \
	TODAY=`date +%Y-%m-%d | tr -d '\n'`; \
	nice="nice -5"; if [ -x /usr/bin/ionice ]; then nice="$$nice ionice -c 3"; fi ; \
	$$nice wget -O$$EXP "http://git.debian.org/?p=pkg-boinc/scripts.git;a=blob_plain;f=export-seti;hb=HEAD" --quiet && \
	chmod +x $$EXP && $$EXP && rm -f $$EXP

.PHONY: get-orig-source
