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

UPSTREAM_VERSION := $(shell dpkg-parsechangelog | egrep '^Version:' | cut -f 2 -d ' ' | cut -d - -f 1)
POSTINST_VERSION := $(shell egrep ^VERSION= debian/postinst | cut -d = -f 2)

# 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)

export CFLAGS = $(shell dpkg-buildflags --get CFLAGS)
export CPPFLAGS = $(shell dpkg-buildflags --get CPPFLAGS)
export CXXFLAGS = $(shell dpkg-buildflags --get CXXFLAGS)
export LDFLAGS = $(shell dpkg-buildflags --get LDFLAGS)
CFLAGS += -Wall -g
CPPFLAGS += -D__STDC_CONSTANT_MACROS
CXXFLAGS += -DZM_FFMPEG_CVS -DHAVE_LIBCRYPTO

%:
	dh $@ --with autoreconf

override_dh_auto_configure:
	./configure --host=$(DEB_HOST_GNU_TYPE) --build=$(DEB_BUILD_GNU_TYPE) --sysconfdir=/etc/zm --prefix=/usr --mandir=\$${prefix}/share/man --infodir=\$${prefix}/share/info --with-mysql=/usr  --with-webdir=/usr/share/zoneminder --with-ffmpeg=/usr --with-cgidir=/usr/lib/cgi-bin --with-webuser=www-data --with-webgroup=www-data --enable-crashtrace=no --enable-mmap=yes

override_dh_clean:
	# check to make sure that postinst contains the correct upstream version
	[ $(UPSTREAM_VERSION) = $(POSTINST_VERSION) ]

	# Add here commands to clean up after the build process.
	[ ! -f Makefile ] || $(MAKE) distclean
	dh_clean 

override_dh_install:
	# Add here commands to install the package into debian/zm.
	$(MAKE) install DESTDIR=$(CURDIR)/debian/zoneminder RUNDIR=$(CURDIR)/debian/zoneminder/var/run ZM_RUNDIR=$(CURDIR)/debian/zoneminder/var/run
	install -D -m 0644 db/zm_create.sql        $(CURDIR)/debian/zoneminder/usr/share/zoneminder/db
	install -D -m 0644 db/zm_update-*.sql $(CURDIR)/debian/zoneminder/usr/share/zoneminder/db
	install -D -m 0644 debian/apache.conf  $(CURDIR)/debian/zoneminder/etc/zm
	#
	# 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
	ln -s /var/cache/zoneminder/events debian/zoneminder/usr/share/zoneminder/
	ln -s /var/cache/zoneminder/images debian/zoneminder/usr/share/zoneminder/
	ln -s /var/cache/zoneminder/temp debian/zoneminder/usr/share/zoneminder/

	# remove zoneminder-shipped mootools, as it is now patched to use mootools
	# shipped from the libjs-mootools package. http://bugs.debian.org/585590
	rm -rf debian/zoneminder/usr/share/zoneminder/tools/mootools
	rm -f debian/zoneminder/usr/share/zoneminder/js/mootools.ext.js

	#
	# 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.
	#
	ln -s /usr/lib/cgi-bin debian/zoneminder/usr/share/zoneminder/

override_dh_fixperms:
	dh_fixperms
	chown root:root debian/zoneminder/etc/zm/zm.conf

override_dh_auto_test:
	# do not run tests...
