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

CXXFLAGS += -I/usr/include/boinc
CXXFLAGS += -I/usr/include/boinc/lib

PKG = boinc-app-seti

UPSTREAM_VERSION = $(shell dpkg-parsechangelog | egrep '^Version:' | \
  cut -d ' ' -f 2 | cut -d '+' -f 1 | cut -d '~' -f 1)
APP_INFO_VERSION = $(subst .,,$(UPSTREAM_VERSION))

autoreconf-stamp:
	aclocal-1.9 -I m4 && autoheader && automake-1.9 && autoconf
	touch $@

build: autoreconf-stamp build-stamp
build-stamp:
	dh build --before configure
	
	# 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 \
	  --enable-client \
	  --disable-graphics \
	  --disable-dynamic-graphics \
	  --disable-server \
	  --disable-static-linkage
	
	dh build --after configure
	touch $@

clean:
	[ ! -f Makefile ] ||  $(MAKE) clean
	dh_clean client/setiathome-$(UPSTREAM_VERSION).* \
	  debian/extra/app_info.xml \
	  client/Makefile \
	  client/setiathome-5.13.i686-pc-linux-gnu \
	  client/setiathome-5.13.i686-pc-linux-gnu.debug \
	  Makefile \
	  config.h \
	  config.log \
	  config.status \
	  db/Makefile \
	  db/schema_to_class \
	  Makefile.in \
	  aclocal.m4 \
	  config.h.in \
	  client/Makefile.in \
	  tools/Makefile.in \
	  libtool \
	  stamp-h1 \
	  tools/Makefile \
	  configure



install: build install-stamp
install-stamp:
	dh install --before 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 --remaining
	touch $@

binary-indep: install
	dh binary-indep

binary-arch: install
	dh binary-arch

binary: binary-indep binary-arch

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: build clean install binary-indep binary-arch binary \
 get-orig-source
