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


CXXFLAGS += -O3
CXXFLAGS += -I/usr/include/boinc -I/usr/include/boinc/lib -I/usr/include/boinc/api
# some handwritten rules in client/Makefile.am seems to use CFLAGS only
# [FIXME] remove duplicates
CFLAGS += -O3
CFLAGS += -I/usr/include/boinc -I/usr/include/boinc/lib -I/usr/include/boinc/api


SETIEXTRAFLAGS=


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


UPSTREAM_VERSION = $(shell dpkg-parsechangelog | egrep '^Version:' | \
  cut -d ' ' -f 2 | cut -d '-' -f 1 | cut -d '+' -f 1 | cut -d '~' -f 1)
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.
	BOINCDIR=/usr/share/boinc-dev ./configure \
	  --disable-static-client \
	  --enable-client \
	  --disable-graphics \
	  --disable-dynamic-graphics \
	  --disable-server \
	  --disable-static-linkage \
	  --enable-dependency-tracking \
	  --disable-static \
	  --enable-shared \
	  --with-gnu-ld

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).* \
	  debian/extra/app_info.xml
	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


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


origDir ?= .

get-orig-source:
	if [ ! -d "$(origDir)" ]; then \
	  echo "Destination directory '$(origDir)' does not exist."; \
	  exit; \
	fi; \
	cd $(origDir); \
	DLS=/tmp/download-seti; \
	TODAY=`date +%Y-%m-%d | tr -d '\n'`; \
	svn export svn://svn.debian.org/pkg-boinc/scripts/download-seti $$DLS; \
	echo "Attempting to retrieve today's ($$TODAY) tarball."; \
	$$DLS $$TODAY || ( \
	  YESTERDAY=`date -d yesterday +%Y-%m-%d | tr -d '\n'`; \
	  echo "Today's ($$TODAY) tarball could not be downloaded."; \
	  echo "Now attempting to retrieve yesterday's ($$YESTERDAY) tarball."; \
	  $$DLS $$YESTERDAY; \
	); \
	rm -f $$DLS

.PHONY: get-orig-source
