#!/usr/bin/make -f
# Made with the aid of debhelper by by Joey Hess,
# based on the sample debian/rules file for GNU hello by Ian Jackson.
#
# This is free software; see the GNU General Public Licence
# version 2 or later for copying conditions.  There is NO warranty.
#
# Modified to suit yodl-2.00 by Frank B. Brokken <f.b.brokken@rc.rug.nl>

SHELL = /bin/sh

export DEB_BUILD_HARDENING=1

# The following is for internal development usage only
# Update that sum when new _upstream_ releases occur, this
# catches silent file content forges at the upstream server side
# Archive downloaded from https://www.icce.rug.nl/debian/yodl
#   file: yodl_x.y.z.tar.gz

MD5TRUSTED := 7df95988902055c98b2d784dd701efa8

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


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

unexport NAME

configure: configure-stamp
configure-stamp:
	dh_testdir
	# Add here commands to configure the package.
	touch configure-stamp

clean:
	dh_testdir
	dh_testroot

	# Cleanup old stuff:
	./build clean

	dh_clean build-stamp install-stamp debian/substvars

build: build-arch build-indep
build-arch: build-stamp
build-stamp: configure-stamp
	dh_testdir

	# Compile the package:
	./build programs
	./build man
	./build macros

	touch build-stamp

build-indep: build-stamp
	./build manual

install: install-stamp

install-stamp: build-stamp
	dh_testdir
	dh_testroot
	dh_prep 
	dh_installdirs

	# Install the package below debian/yodl:
	./build install programs    debian/yodl
	./build install macros      debian/yodl
	./build install man         debian/yodl
	./build install docs        debian/yodl

	touch $@

install-indep:
	./build install manual debian/yodl-doc

# Build architecture-independent files here.
binary-indep: build-indep install-indep
	dh_testdir -i
	dh_testroot -i
	dh_installdocs -i
	dh_installinfo -i
	dh_installchangelogs -i
	dh_link -i
	dh_compress -i
	dh_fixperms -i
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

# Build architecture-dependent files here.
binary-arch: build-arch install
	dh_testdir -pyodl
	dh_testroot -pyodl
	dh_installdocs -pyodl
	dh_installinfo -pyodl
	dh_installchangelogs -pyodl
	dh_strip -pyodl
	dh_compress -pyodl
	dh_fixperms -pyodl
	dh_installdeb -pyodl
	dh_shlibdeps -pyodl
	dh_gencontrol -pyodl
	dh_md5sums -pyodl
	dh_builddeb -pyodl

DEBVERSION:=$(shell head -n 1 debian/changelog | sed -e 's/^[^(]*(\([^)]*\)).*/\1/')
UPVERSION:=$(shell echo $(DEBVERSION) | sed -e 's/^.*://' -e 's/-[0-9.]*$$//' -e 's/.dfsg$$//')
UPNAME := yodl
UPFILE := $(UPNAME)_$(UPVERSION).tar.gz
DEFILE := $(UPNAME)_$(UPVERSION).orig.tar.gz
URL        := https://www.icce.rug.nl/debian/yodl
MD5CURRENT := `md5sum ../tarballs/$(DEFILE) | sed -e 's/ .*//'`

get-orig-source:
	@@dh_testdir
	@@[ -d ../tarballs/. ]||mkdir -p ../tarballs

	-@if [ ! -f ../tarballs/$(DEFILE) ] ; then \
	        echo -e "Downloading $(URL)/$(UPFILE)\nSaving into ../tarballs/$(DEFILE) ..." ; \
	        wget --no-check-certificate --quiet -nv -T20 -t3 -O \
	                 ../tarballs/$(DEFILE) $(URL)/$(UPFILE) ; \
	else \
	        echo "Upstream source tarball has already been downloaded" ; \
	fi

	-@if [ "$(MD5CURRENT)" != "$(MD5TRUSTED)" ] ; then \
	        echo "Expecting upstream filename md5sum $(MD5TRUSTED), but $(MD5CURRENT) found" ; \
	        echo "Upstream filename md5sum is NOT trusted! Possible upstream filename forge!" ; \
	        false ; \
	else \
	        echo "Upstream filename md5sum is trusted!" ; \
	fi

print-version:
	@@echo "Debian version:                  $(DEBVERSION)"
	@@echo "Upstream version:                $(UPVERSION)"

binary: binary-arch binary-indep
.PHONY: build clean binary-indep binary-arch binary install
