#!/usr/bin/make -f
# Sample debian/rules that uses debhelper.
# GNU copyright 1997 to 1999 by Joey Hess.

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

# This is the debhelper compatibility version to use.
export DH_COMPAT=10
DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/buildflags.mk

NAME=ezmlm-idx
PREFIX=/usr
BINDIR=$(PREFIX)/bin
SBINDIR=$(PREFIX)/sbin
LIBDIR=$(PREFIX)/lib/indimail/ezmlm
LIBEXECDIR=$(PREFIX)/libexec
MANDIR=$(PREFIX)/share/man
DOCDIR=$(PREFIX)/share/doc/ezmlm-idx
SYSCONFDIR=/etc/indimail
RCDIR=$(SYSCONFDIR)/ezmlm
CGIDIR=/var/www/cgi-bin
DESTDIR=$(CURDIR)/debian/ezmlm-idx.tmp
version=$(shell cat conf-version)
email=$(shell cat conf-email)

build: build-stamp
build-stamp:
	dh_testdir
	# Add here commands to compile the package.
	echo $(SYSCONFDIR)          >conf-sysconfdir
	echo $(RCDIR)               >conf-etc
	echo $(PREFIX)              >conf-prefix
	echo $(BINDIR)              >conf-bin
	echo $(SBINDIR)             >conf-sbin
	echo $(MANDIR)              >conf-man
	echo $(LIBDIR)              >conf-lib
	echo $(LIBEXECDIR)          >conf-libexec
	echo "$(CC) $(CFLAGS) -I/usr/include/mysql -I/usr/include/postgresql -I/usr/include/qmail" >conf-cc
	echo "$(CC) $(LDFLAGS) -L/usr/lib/mysql" >conf-ld
	make clean
	make SYSTEM=LINUX all
	# --- end custom part for compiling
	( \
	echo NAME=ezmlm-idx; \
	echo Description=\"Easy Mailing List Manager for indimail-mta\"; \
	echo ezmlm-idx_version=$(version); \
	echo ID=ezmlm-idx; \
	echo HOME_URL="https://github.com/indimail/ezmlm-idx"; \
	echo PACKAGE_BUGREPORT=\"'$(email)'\"; \
	) > ezmlm-idx-release
	touch build-stamp

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp

	# Add here commands to clean up after the build process.
	rm -rf $(DESTDIR)
	# --- end custom part for cleaning up

	dh_clean

install: build
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs

	# Add here commands to install the package
	# The DESTDIR Has To Be Exactly  /usr/src/packages/BUILD/debian/<nameOfPackage>
	mkdir -p "$(DESTDIR)$(BINDIR)"
	mkdir -p "$(DESTDIR)$(SBINDIR)"
	mkdir -p "$(DESTDIR)$(DOCDIR)"
	mkdir -p "$(DESTDIR)$(LIBDIR)"
	mkdir -p "$(DESTDIR)$(LIBEXECDIR)/ezmlm"
	mkdir -p "$(DESTDIR)$(RCDIR)"
	mkdir -p "$(DESTDIR)$(CGIDIR)"
	mkdir -p "$(DESTDIR)$(PREFIX)/sbin"
	./installer "$(DESTDIR)$(RCDIR)"      <ETC
	./installer "$(DESTDIR)$(BINDIR)"     <BIN
	./installer "$(DESTDIR)$(SBINDIR)"    <SBIN
	./installer "$(DESTDIR)$(MANDIR)"     <MAN
	./installer "$(DESTDIR)$(LIBDIR)"     <LIB
	./installer "$(DESTDIR)$(LIBEXECDIR)" <LIBEXEC
	ln -s `head -n 1 conf-lang` "$(DESTDIR)$(RCDIR)/default"
	install -m 0644 README.mysql README.pgsql INSTALL.cgi ezcgirc ezcgi.css "$(DESTDIR)$(DOCDIR)"
	mv $(DESTDIR)$(BINDIR)/ezmlm-cgi   $(DESTDIR)$(CGIDIR)
	# --- end custom part for installing
	install -m 0644 ezmlm-idx-release "$(DESTDIR)$(RCDIR)"/ezmlm-idx-release
	/bin/rm -f ezmlm-idx-release 

# Build architecture-independent files here.
binary-indep: build install
	# We have nothing to do by default.

# Build architecture-dependent files here.
binary-arch: build install
	dh_testdir
	dh_testroot
	find $(DESTDIR)$(MANDIR) -name '*'.? -type f -exec gzip -q {} \;
	dh_install --sourcedir=$(DESTDIR) -pezmlm-idx
	dh_install --sourcedir=$(DESTDIR) -pezmlm-idx-cgi
	dh_install --sourcedir=$(DESTDIR) -pezmlm-idx-mysql
	dh_install --sourcedir=$(DESTDIR) -pezmlm-idx-pgsql
	dh_install --sourcedir=$(DESTDIR) -pezmlm-idx-sqlite
	dh_installdocs BLURB CHANGES* FAQ INSTALL.source README README.std ezmlm-idx.changes THANKS TODO UPGRADE DOWNGRADE ezmlmrc.template LICENSE
	dh_installexamples
	dh_installcron
	dh_installman
	dh_installchangelogs --exclude ChangeLog
	dh_link
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

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