#!/usr/bin/make -f
# debian/rules for samhain
# Copyright (C) 2001 to 2016 by Javier Fernandez-Sanguino

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

# This is the debhelper compatability version to use.
# export DH_COMPAT=5

# Disable dnmalloc for most architectures except for
# those known to work (i386 and amd64).
# For more information see:
# http://www.la-samhna.de/samhain/manual/dnmalloc.html
ifeq (linux,$(DEB_HOST_ARCH_OS))
ifeq (amd64,$(DEB_HOST_ARCH))
DNMALLOC = --enable-dnmalloc
else ifeq (i386,$(DEB_HOST_ARCH))
DNMALLOC = --enable-dnmalloc
else
DNMALLOC = --disable-dnmalloc
endif
else
ifeq (amd64,$(DEB_HOST_ARCH))
DNMALLOC = --enable-dnmalloc
else ifeq (i386,$(DEB_HOST_ARCH))
DNMALLOC = --enable-dnmalloc
else
DNMALLOC = --disable-dnmalloc
endif
endif

ifeq (x86_64,$(DEB_HOST_GNU_CPU))
DISABLE_ASM = --disable-asm
endif

%:
	dh $@

override_dh_auto_configure:
	./configure --prefix=/usr --mandir=\$${prefix}/share/man \
		--with-config-file=/etc/samhain/samhainrc \
		--with-state-dir=/var/lib/samhain  \
		--with-prelude \
		$(DNMALLOC) \
		$(DISABLE_ASM) \
		--enable-network=no  \
		--enable-login-watch \
		--enable-mounts-check \
		--enable-process-check \
		--enable-port-check \
		--enable-suidcheck \
		--with-pid-file=/var/run/samhain/samhain.pid \
		--with-log-file=/var/log/samhain/samhain.log 

override_dh_clean:
	[ ! -f Makefile ] || $(MAKE) distclean
	-rm -f samhainrc.install
	-rm -f build-stamp build-server-stamp build-client-stamp
	dh_clean


override_dh_installdirs:
	dh_installdirs
	# Fix the permissions
	chmod o-rX `pwd`/debian/samhain/var/log/samhain \
		`pwd`/debian/samhain/var/lib/samhain \
		`pwd`/debian/samhain/etc/samhain

override_dh_install:
	$(MAKE) install install-boot DESTDIR=`pwd`/debian/samhain
	# However, remove the rc.d links
	-rm -rf `pwd`/debian/samhain/etc/rc?.d
	# Remove /var/run/samhain from the package, it is created by the init script
	-rm -rf `pwd`/debian/samhain/var/run/samhain


override_dh_installdocs:
	dh_installdocs
	[ -f debian/samhain/usr/share/doc/samhain/MANUAL-2_4.html.tar ] && \
		cd debian/samhain/usr/share/doc/samhain && \
		tar xf MANUAL-2_4.html.tar && mv MANUAL-2_4 manual.html && \
		rm -f MANUAL-2_4.html.tar

override_dh_installinit:
	dh_installinit -- defaults 19

override_dh_installchangelogs:
	dh_installchangelogs docs/Changelog

# TODO: create install targets for client and server
# Builds the server (Yule)
# See http://www.la-samhna.de/samhain/manual/yule.html
build-server: build-server-stamp
build-server-stamp:
	dh_testdir
	./configure --prefix=/usr --mandir=\$${prefix}/share/man \
		--with-config-file=/etc/yule/yulerc \
		--with-state-dir=/var/lib/yule  \
		--with-prelude \
		$(DNMALLOC) \
		--enable-network=server  \
		--enable-login-watch \
		--enable-mounts-check \
		--enable-logfile-monitor \
		--enable-process-check \
		--enable-port-check \
		--enable-suidcheck \
		--with-pid-file=/var/run/samhain/yule.pid \
		--with-log-file=/var/log/samhain/yule.log 
	$(MAKE)
	touch build-server-stamp

# Build the Samhain client 
build-client: build-client-stamp
build-client-stamp:
	dh_testdir
	./configure --prefix=/usr --mandir=\$${prefix}/share/man \
		--with-config-file=/etc/samhain/samhainrc \
		--with-state-dir=/var/lib/samhain  \
		--with-prelude \
		$(DNMALLOC) \
		--enable-network=client \
		--enable-login-watch \
		--enable-mounts-check \
		--enable-logfile-monitor \
		--enable-process-check \
		--enable-port-check \
		--enable-suidcheck \
		--with-pid-file=/var/run/samhain/samhain.pid \
		--with-log-file=/var/log/samhain/samhain.log 
	$(MAKE)
	touch build-client-stamp


get-orig-source:
	@echo "Retrieving source"
	wget -q -O samhain-current.tar.gz  http://la-samhna.de/samhain/samhain-current.tar.gz
	@echo "Extracting source"
	tar -zxf samhain-current.tar.gz
	@echo "Verifying source"
	# Take the latest version
	file=`tar -ztf samhain-current.tar.gz |grep -v asc | head -1`; \
	gpgfile=`tar -ztf samhain-current.tar.gz |grep asc | head -1`; \
	newfile=`echo $$file | sed -e 's/-/_/; s/\.tar/.orig.tar/'`; \
	mv $$file $$newfile; \
	gpg -q --verify $$gpgfile $$newfile 
	rm -f samhain-current.tar.gz
