#!/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


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 += -O3
CFLAGS_APPEND += -I/usr/include/boinc -I/usr/include/boinc/lib -I/usr/include/boinc/api

# 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 += --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-1.9 -I m4 && autoheader && automake-1.9 && 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
	# 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
	# 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 splitter/Makefile splitter/Makefile.in stamp-h1 tools/Makefile tools/Makefile.in validate/Makefile
	rm -f sah_config.h

override_dh_installchangelogs:
	dh_installchangelogs -k checkin_notes

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
