#!/usr/bin/make -f

# export DH_VERBOSE=1

# Downloaded from https://www.icce.rug.nl/debian/annotations
MD5TRUSTED := 2db245ce038d04b18eecf9b81aa78153

CXX=g++-4.9
CFLAGS = -Wall -g

ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -O0
else
	CFLAGS += -O2
endif

build-stamp:
	dh_testdir
	./build docs
	touch $@

build:
build-arch:
build-indep: build-stamp

clean:
	dh_testdir
	dh_testroot
	rm -rf build-stamp debian/c++-annotations
	./build clean
	dh_clean

install-indep: build-indep
	dh_testdir
	dh_testroot
	dh_installdirs
	dh_installchangelogs -A changelog
	dh_installdocs   -A README README.papersize README.PORTUGUESE README.legalese
	dh_installman
	cp -ra tmp/docs/html                    debian/c++-annotations-html/usr/share/doc/c++-annotations/
	cp -ra contrib                          debian/c++-annotations-html/usr/share/doc/c++-annotations/html/
	cp -a  tmp/docs/txt/cplusplus.txt       debian/c++-annotations-txt/usr/share/doc/c++-annotations/
	cp -a  tmp/docs/latex/cplusplus.latex   debian/c++-annotations-latex/usr/share/doc/c++-annotations/
	cp -a  tmp/docs/latex/cplusplus.sty     debian/c++-annotations-latex/usr/share/doc/c++-annotations/
	cp -a  tmp/docs/latex/cplusplusus.latex debian/c++-annotations-latex/usr/share/doc/c++-annotations/
	cp -a  tmp/docs/latex/cplusplusus.sty   debian/c++-annotations-latex/usr/share/doc/c++-annotations/
	cp -a  tmp/docs/latex/cplusplus.pdf     debian/c++-annotations-pdf/usr/share/doc/c++-annotations/
	cp -a  tmp/docs/latex/cplusplusus.pdf   debian/c++-annotations-pdf/usr/share/doc/c++-annotations/
	cp -a  tmp/docs/latex/cplusplus.ps      debian/c++-annotations-ps/usr/share/doc/c++-annotations/
	cp -a  tmp/docs/latex/cplusplusus.ps    debian/c++-annotations-ps/usr/share/doc/c++-annotations/
	cp -a  tmp/docs/latex/cplusplus.dvi     debian/c++-annotations-dvi/usr/share/doc/c++-annotations/
	cp -a  tmp/docs/latex/cplusplusus.dvi   debian/c++-annotations-dvi/usr/share/doc/c++-annotations/
	cp -ra contributions                    debian/c++-annotations-contrib/usr/share/doc/c++-annotations/
	cp -ra contrib                          debian/c++-annotations-contrib/usr/share/doc/c++-annotations/
	mkdir  debian/c++-annotations/usr/share/doc/c++-annotations/examples
	for e  in `find -type d -name examples | grep -v ^./debian`; \
	do cp  -r --parents   "$$e" debian/c++-annotations/usr/share/doc/c++-annotations/examples; done

# architecture-dependent files here.
binary-indep: build-indep install-indep
	dh_compress -A -X.html -X.gif -X.pdf
	dh_fixperms
	dh_installdeb
	dh_gencontrol
	dh_md5sums
	dh_builddeb

# architecture-independent files here.
binary-arch:

# 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

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 := c++-annotations
UPFILE := $(UPNAME)_$(UPVERSION).tar.gz
DEFILE := $(UPNAME)_$(UPVERSION).orig.tar.gz
URL    := https://www.icce.rug.nl/debian/annotations
MD5CURRENT := `md5sum ../tarballs/$(DEFILE) | sed -e 's/ .*//'`

get-orig-source:
	@@[ -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 -T10 -t3 \
	-O ../tarballs/$(DEFILE) $(URL)/$(UPFILE) ; \
	else \
	echo "Upstream source tarball have been already 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-indep binary-arch
.PHONY: build build-indep build-arch clean binary-indep binary-arch binary install
