#!/usr/bin/make -f
# -*- makefile -*-
# Sample debian/rules that uses debhelper.
# This file was originally written by Joey Hess and Craig Small.
# As a special exception, when this file is copied by dh-make into a
# dh-make output file, you may use that output file without restriction.
# This special exception was added by Craig Small in version 0.37 of dh-make.

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

# These are used for cross-compiling and for saving the configure script
# from having to guess our platform (since we know it already)
DEB_HOST_GNU_TYPE   ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

CFLAGS = -Wall
CPPFLAGS = -D__STDC_CONSTANT_MACROS
CXXFLAGS = -DHAVE_LIBCRYPTO

ifneq (,$(findstring debug,$(DEB_BUILD_OPTIONS)))
DEBOPT = --enable-debug
CFLAGS += -g
CXXFLAGS += -g
else
DEBOPT =
endif

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

INSTDIR = debian/tmp

# These are used to get the most recent version of the original sources from github
UURL = $(shell git config --get remote.origin.url)
BRANCH = $(shell git rev-parse --abbrev-ref HEAD)
HEAD = $(shell git rev-parse HEAD)
PKD  = $(abspath $(dir $(MAKEFILE_LIST)))
PKG  = $(word 2,$(shell dpkg-parsechangelog -l$(PKD)/changelog | grep ^Source))
VER ?= $(shell dpkg-parsechangelog -l$(PKD)/changelog | perl -ne 'print $$1 if m{^Version:\s+(?:\d+:)?(\d.*)(?:\-|\+nmu\d+.*)};')
DTYPE =
TARBALL = ../$(PKG)_$(VER)$(DTYPE).orig.tar.xz

%:
	dh $@ --buildsystem=cmake --parallel

override_dh_auto_configure:
	CFLAGS="$(CFLAGS)" CXXFLAGS="$(CXXFLAGS)" dh_auto_configure -- \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCMAKE_SKIP_RPATH=ON \
		-DCMAKE_VERBOSE_MAKEFILE=OFF \
		-DCMAKE_COLOR_MAKEFILE=ON \
		-DZM_RUNDIR=/var/run/zm \
		-DZM_SOCKDIR=/var/run/zm \
		-DZM_TMPDIR=/var/tmp/zm \
		-DZM_LOGDIR=/var/log/zm \
		-DZM_WEBDIR=/usr/share/zoneminder \
		-DZM_CONTENTDIR=/var/cache/zoneminder \
		-DZM_CGIDIR=/usr/lib/cgi-bin \
		-DZM_WEB_USER=www-data \
		-DZM_WEB_GROUP=www-data \
		-DCMAKE_INSTALL_SYSCONFDIR=etc/zm

override_dh_auto_test:
	# do not run tests...


override_dh_clean:
	# Add here commands to clean up after the build process.
	[ ! -f Makefile ] || $(MAKE) distclean
	dh_clean src/zm_config_defines.h
	#
	# Delete remaining auto-generated Makefile if Makefile.in exists
	find $(CURDIR)/ -type f -name "Makefile" | while read file; do \
	  [ -f $$file.in ] && rm -f $$file; \
	done || true
	#
	# Delete remaining auto-generated Makefile.in if Makefile.am exists
	find $(CURDIR)/ -type f -name "Makefile.in" | while read filein; do \
	  fileam=`echo $$filein | sed 's/\(.*\)\.in/\1\.am/'`; \
	  [ -f $$fileam ] && rm -f $$filein; \
	done || true

override_dh_install:
	dh_install --fail-missing
	#
	# NOTE: This is a short-term kludge; hopefully changes in the next
	# upstream version will render this unnecessary.
	rm -rf debian/zoneminder/usr/share/zoneminder/events
	rm -rf debian/zoneminder/usr/share/zoneminder/images
	rm -rf debian/zoneminder/usr/share/zoneminder/temp
	# The link stuff for these folders has been moved to
	# zoneminder-core.links file
	#
	# This is a slightly lesser kludge; moving the cgi stuff to
	# /usr/share/zoneminder/cgi-bin breaks one set of behavior,
	# having it just in /usr/lib/cgi-bin breaks another bit of
	# behavior.
	# The link stuff for /usr/share/zoneminder/cgi-bin has been moved to
	# zoneminder-ui-base.links file

override_dh_installinit:
	dh_installinit --package=zoneminder-core --name=zoneminder

override_dh_systemd_start:
	dh_systemd_start --package=zoneminder-core --name=zoneminder \
	  --restart-after-upgrade

override_dh_systemd_enable:
	dh_systemd_enable --package=zoneminder-core --name=zoneminder

override_dh_fixperms:
	dh_fixperms
	#
	# As requested by the Debian Webapps Policy Manual §3.2.1
	chown root:www-data debian/zoneminder-core/etc/zm/zm.conf
	chmod 640 debian/zoneminder-core/etc/zm/zm.conf

.PHONY: override_dh_strip
override_dh_strip:
	dh_strip --dbg-package=zoneminder-core-dbg

# Inspired by https://wiki.debian.org/onlyjob/get-orig-source
.PHONY: get-orig-source
get-orig-source: $(TARBALL) $(info I: $(PKG)_$(VER)$(DTYPE))
	@

$(TARBALL):
	$(if $(wildcard $(PKG)-$(VER)),$(error folder '$(PKG)-$(VER)' exists, aborting...))
	@echo "# Cloning origin repository..."; \
	if ! git clone $(UURL) $(PKG)-$(VER); then \
	  $(RM) -r $(PKG)-$(VER); \
	  echo "failed to clone repository, aborting..."; \
	  false; \
	fi
	@if [ $(BRANCH) != "master" ]; then \
	  cd $(PKG)-$(VER); \
	  echo "# Not on master branch, fetching origin branch '$(BRANCH)'..."; \
	  git fetch origin $(BRANCH):$(BRANCH) || false; \
	  echo "# Switching to branch '$(BRANCH)'..."; \
	  git checkout $(BRANCH) || false; \
	fi
	@echo "# Checking local source..."
	@if [ $$(cd $(PKG)-$(VER) && git rev-parse HEAD) = $(HEAD) ]; then \
	  echo "even with origin, ok"; \
	  true; \
	else \
	  echo "not even with origin, aborting..."; \
	  false; \
	fi
	@echo "# Setting times..."
	@cd $(PKG)-$(VER) \
	&& for F in $$(git ls-tree -r --name-only HEAD | sed -e "s/\s/\*/g"); do \
	  touch --no-dereference -d "$$(git log -1 --format="%ai" -- $$F)" "$$F"; \
	done
	@echo "# Cleaning-up..."
	cd $(PKG)-$(VER) && $(RM) -r .git
	@echo "# Packing file '$(TARBALL)'..."
	@find -L "$(PKG)-$(VER)" -xdev -type f -print | sort \
	| XZ_OPT="-6v" tar -caf "$(TARBALL)" -T- --owner=root --group=root --mode=a+rX \
	&& $(RM) -r "$(PKG)-$(VER)"
