#!/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
export DEB_BUILD_MAINT_OPTIONS = hardening=+all


# 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/
	# not installing the very old html pages located here: $(SRCDIR)/doc/
	# Fix 'doc/doc'; 'doc/html' is more usual.
	-cd $(DESTDIR)/$(DOCDIR); mv doc 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
	printf "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
	printf "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 PSI ELOG FTP server and do the repacking automatically.
# The ORIG_VERSION must be set manually.
ORIG_VERSION=3.1.2-1
get-orig-source:
	wget -q http://midas.psi.ch/elog/download/tar/elog-$(ORIG_VERSION).tar.gz
	tar xzf ./elog-$(ORIG_VERSION).tar.gz
	# rename elog folder
	mv ./elog-3.1.2 ./elog-$(ORIG_VERSION)
	# move mxml/* into elog/src
	mv ./mxml/* ./elog-$(ORIG_VERSION)/src
	# delete embedded source of CKeditor coming from upstream
	rm -rf ./elog-$(ORIG_VERSION)/scripts/ckeditor
	rm -rf ./elog-$(ORIG_VERSION)/scripts/ckeditor.zip
	# delete embedded jquery coming from upstream
	rm -rf ./elog-$(ORIG_VERSION)/scripts/jquery-1.11.1.min.js
	# remove compressed javacript of progress bar, fetch progress bar javascript sources and embed it
	rm -rf ./elog-$(ORIG_VERSION)/scripts/progress/*
	wget -q https://github.com/usablica/progress.js/archive/v0.1.0.tar.gz
	tar xzf v0.1.0.tar.gz
	cp progress.js-0.1.0/README.md ./elog-$(ORIG_VERSION)/scripts/progress/
	cp progress.js-0.1.0/src/* ./elog-$(ORIG_VERSION)/scripts/progress/
	# pack it
	cd ..
	tar czf ../../$(shell date "+%Y%m%d-%H%M%S")-REPACKED-elog_$(ORIG_VERSION).orig.tar.gz --exclude-vcs -C ./ elog-$(ORIG_VERSION)
	rm -rf ./elog-$(ORIG_VERSION).tar.gz ./mxml ./v0.1.0.tar.gz ./progress.js-0.1.0 ./elog-$(ORIG_VERSION)

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