#!/usr/bin/make -f

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

server:=$(shell pwd)/debian/sendpage-server
client=$(shell pwd)/debian/sendpage-client
common=$(shell pwd)/debian/sendpage-common

ifndef PERL
PERL=/usr/bin/perl
endif
archlib	= `$(PERL) -e 'use Config; print $Config{installarchlib}'`
config	= 	INSTALLDIRS=vendor \
		INSTALLMAN1DIR=$(common)/usr/share/man/man1 \
		INSTALLMAN3DIR=$(common)/usr/share/man/man3 \
		INSTALLVENDORLIB=$(common)/usr/lib/perl5 \
		#INSTALLARCHLIB=$(common)$(archlib)


build:  build-stamp
build-stamp:
	dh_testdir

	$(PERL) Makefile.PL INSTALLDIRS=vendor
	$(MAKE) OPTIMIZE="-O2 -g -Wall"

	touch build-stamp

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp configure-stamp

	if [ -e Makefile ]; then $(MAKE) -i distclean; fi
	dh_clean

install: build
	dh_testdir
	dh_testroot
	dh_installdirs
	$(MAKE) install  PREFIX=$(common)/usr
	find $(shell pwd) -type f -name .packlist | xargs -r rm -f

	cp sendpage.cf $(server)/etc/sendpage/
	cp snpp.conf $(client)/etc/sendpage/
	cp email2page.conf $(client)/etc/sendpage/

	mv $(common)/usr/bin/email2page $(client)/usr/bin/
	mv $(common)/usr/bin/sendmail2snpp debian/sendmail2snpp
	mv $(common)/usr/bin/snpp $(client)/usr/bin/

	mv $(common)/usr/bin/sendpage $(server)/usr/sbin/

	mkdir -p $(server)/usr/share/man/man1
	mkdir -p $(client)/usr/share/man/man1

	mv $(common)/usr/share/man/man1/sendpage.1p \
		$(server)/usr/share/man/man1/
	mv $(common)/usr/share/man/man1/snpp.1p \
		$(client)/usr/share/man/man1/
	mv $(common)/usr/share/man/man1/email2page.1p \
		$(client)/usr/share/man/man1/

	chmod ugo-x $(common)/usr/share/perl5/Sendpage/PagingCentral.pm


# Build architecture-independent files here.
binary-indep: build install
	dh_testdir
	dh_testroot
	dh_perl -i
	dh_installdebconf -i
	dh_installdeb
	dh_installchangelogs -p sendpage-common Changes
	dh_link -p sendpage-client usr/share/doc/sendpage-common usr/share/doc/sendpage-client
	dh_link -p sendpage-server usr/share/doc/sendpage-common usr/share/doc/sendpage-server
	dh_installdocs  -p sendpage-common
	dh_installinit -p sendpage-server debian/sendpage-server.init
	#dh_undocumented -p sendpage-client sendmail2snpp.1
	dh_installexamples -p sendpage-common debian/sendpage.cf.example
	dh_fixperms -i
	dh_compress -i
	dh_gencontrol -i
	dh_builddeb -i

# Build architecture-dependent files here.
binary-arch: build install

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