#!/usr/bin/make -f
# Copyright © 2003, 2006  Recai Oktaş <roktas@omu.edu.tr>
# Copyright (c) 2014 Roger Kalt <roger.kalt@gmail.com>
# Portions are based on the sample debian/rules of debhelper.
#
# Licensed under the GNU General Public License, version 2.
# See the file `http://www.gnu.org/copyleft/gpl.txt'.

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


# A few convenience variables.
NAME=elog
SRCDIR=$(shell pwd)
DESTDIR=$(shell pwd)/debian/$(NAME)
CONFDIR=etc
DATADIR=usr/share/$(NAME)
VARDIR=var/lib/$(NAME)
DOCDIR=usr/share/doc/$(NAME)
SRVDIR=$(VARDIR)


CFLAGS = `dpkg-buildflags --get CFLAGS`
CFLAGS += -funroll-loops -fomit-frame-pointer -W -Wall -Wl,-z,relro -D_FORTIFY_SOURCE=2 
LDFLAGS = `dpkg-buildflags --get LDFLAGS`
LDFLAGS += defs

# Enable by default OpenSSL and KRB5 support in the Debian binaries
LIBS= -lkrb5 -lssl

# User CFLAGS
CFLAGS += -DHAVE_KRB5 \
	  -DHAVE_SSL \
	  -DUSE_CRYPT \
	  -DDEFAULT_PORT=80 \
	  -DCFGFILE=\"elog.conf\" \
          -DCONFIG_PATH=\"/$(CONFDIR)\" \
	  -DLOGBOOK_DIR=\"/$(SRVDIR)/logbooks\" \
	  -DRESOURCE_DIR=\"/$(DATADIR)\"


build: build-arch build-indep
build-arch: build-stamp
build-indep: build-stamp
build-stamp:
	dh_testdir
	# Compile with the standard crypt support.
	cd $(SRCDIR); $(MAKE) CFLAGS='$(CFLAGS)' LIBS='$(LIBS)'
	touch build-stamp

clean:
	dh_testdir
	dh_testroot
	-cd $(SRCDIR); $(MAKE) clean
	dh_clean

install: build
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs
	$(MAKE) DESTDIR=$(CURDIR)/debian/elog install

# Build architecture-independent files here.
binary-indep: install
# We have nothing to do by default.

# Build architecture-dependent files here.
binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installdocs -X doc/ChangeLog $(SRCDIR)/doc-html-latest/
	# not installing the very old html pages located here: $(SRCDIR)/doc/
	# Fix 'doc/doc-html-latest'; 'doc/html' is more usual.
	-cd $(DESTDIR)/$(DOCDIR); mv doc-html-latest html
	# Remove contrib content and install new README with download location of external contributions
	# Reason: Too many different authors with too many different licenses: Should stay external of Debian
	mkdir -p $(DESTDIR)/$(DOCDIR)/contrib
	echo "Download external elog contributions here: https://midas.psi.ch/elogs/Contributions/" > $(DESTDIR)/$(DOCDIR)/contrib/README
	dh_installchangelogs $(SRCDIR)/doc/ChangeLog
	dh_installexamples debian/examples/*
	dh_installman $(SRCDIR)/man/*
	# install empty ssl directoy and skip the server.crt / server.key from orig sources
	mkdir -p $(DESTDIR)/usr/share/elog/ssl
	echo "In case SSL is enabled in '/etc/elog.conf' make here manually the following symbolic links:\n   ./server.key -> /etc/ssl/private/my.key\n   ./server.crt -> /etc/ssl/certs/my.pem\n\nGeneration of self-signed SSL certificates can e.g. be done easily with command /usr/sbin/make-ssl-cert from package ssl-cert." > $(DESTDIR)/usr/share/elog/ssl/README
	dh_lintian
	# install the init script but do not modify the postinst, prerm and postrm scripts
	dh_installinit --noscripts
	dh_install
	dh_link
	dh_strip
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary: binary-indep binary-arch

# Get most current orig source from git repos and do the repacking automatically.
# The NEW_ORIG_VERSION must be set manually.
NEW_ORIG_VERSION=2.9.2+2014.05.11git44800a7
get-orig-source:
	mkdir get-orig-source
	git clone https://bitbucket.org/ritt/elog.git ./get-orig-source/elog
	git --git-dir=./get-orig-source/elog/.git reset --hard
	git --git-dir=./get-orig-source/elog/.git checkout 44800a7
	git clone https://bitbucket.org/tmidas/mxml.git  ./get-orig-source/mxml
	# move mxml/* into elog/src
	mv ./get-orig-source/mxml/* ./get-orig-source/elog/src
	# automatically generate ./src/git-revision.h from git repo log
	echo "/********************************************************************\n\n  Name:         git-revision.h\n  Created by:   Stefan Ritt\n  Copyright 2000 + Stefan Ritt\n\n  ELOG is free software: you can redistribute it and/or modify\n  it under the terms of the GNU General Public License as published by\n  the Free Software Foundation, either version 3 of the License, or\n  (at your option) any later version.\n\n  ELOG is distributed in the hope that it will be useful,\n  but WITHOUT ANY WARRANTY; without even the implied warranty of\n  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n  GNU General Public License for more details.\n\n  You should have received a copy of the GNU General Public License\n  along with ELOG.  If not, see <http://www.gnu.org/licenses/>.\n\n********************************************************************/\n" \#define GIT_REVISION \"`git --git-dir=./get-orig-source/elog/.git log -n 1 --pretty=format:"%ad - %h"`\" > ./get-orig-source/elog/src/git-revision.h
	# delete embedded source of FCKeditor coming from upstream
	rm -rf ./get-orig-source/elog/scripts/fckeditor
	rm -rf ./get-orig-source/elog/scripts/fckeditor.zip
	# fetch latest version of ELOG homepage (HTML) for documentation purpose
	mkdir ./get-orig-source/elog/doc-html-latest
	wget -q -p -r -l 2 -nd -A css,png,gif,html -P ./get-orig-source/elog/doc-html-latest http://midas.psi.ch/elog	
	rm -rf ./get-orig-source/elog/doc-html-latest/robots.txt ./get-orig-source/elog/doc-html-latest/robots.txt.1
	# rename elog to elog-<new version>
	mv ./get-orig-source/elog ./get-orig-source/elog-$(NEW_ORIG_VERSION)
	# pack it
	tar czf ../../$(shell date "+%Y%m%d-%H%M%S")-REPACKED-elog_$(NEW_ORIG_VERSION).orig.tar.gz --exclude-vcs -C get-orig-source elog-$(NEW_ORIG_VERSION)
	rm -rf ./get-orig-source

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