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

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

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

	touch build-stamp

build-indep: build-stamp
	./build manual
	# ./build macros MUST be the last step in the build process
	./build macros

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

# 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 := 804703769e7995e25f5f5dd59ca3377c

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/ .*//'`
SVNTRUNK   := https://svn.sourceforge.net/svnroot/yodl/trunk/yodl

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

	-@if [ ! -f ../tarballs/$(DEFILE) ] ; then \
		echo "Downloading $(URL)/$(UPFILE) from $(URL)/$(UPFILE) ..." ; \
		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


get-svn-trunk:
	@@dh_testdir
	@@[ -d ../tarballs/. ]||mkdir -p ../tarballs
	rm -rf ../tarballs/$(DEFILE) ../tarballs/yodl-svn-trunk
	svn --force export $(SVNTRUNK) ../tarballs/yodl-svn-trunk
	@@if [ -d ../tarballs/yodl-svn-trunk/debian ] ; then rm -rf ../tarballs/yodl-svn-trunk/debian; fi
	@@ echo Building the SVN TRUNK tarball in ../tarballs/$(DEFILE)
	@@GZIP=-9 tar -b1 -czf ../tarballs/$(DEFILE) -C ../tarballs yodl-svn-trunk
	rm -rf ../tarballs/yodl-svn-trunk


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

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