#!/usr/bin/make -f
# -*- makefile -*- made with the aid of debmake, by Christoph Lameter,

package = proftpd

SHELL = /bin/bash
CC = gcc
CFLAGS := -O2 -Wall -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64
IE := install -m755 -s
ID := install -m644

# Some special build options
ifneq (,$(findstring debug,$(DEB_BUILD_OPTIONS)))
  CFLAGS += -g
  ifneq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
    IE := install -m755
  endif
endif

DH_COMPAT=3
export DH_COMPAT

# the dbs rules
TAR_DIR := proftpd-1.2.5rc1
include debian/scripts/dbs-build.mk

# dpkg-arch rules
ifeq (,$(DEB_BUILD_GNU_TYPE))
  include debian/scripts/dpkg-arch.mk
endif

# This is only compiled if /usr/src/linux/Makefile is 2.2.x or later.
ifeq ("$(shell bash debian/getkernelversion)", "privs")
LINUXPRIVS = mod_linuxprivs:
endif

PAM = mod_pam:
DOC = usr/share/doc
MAN = usr/share/man

# This is the suite of modules maintained in debian/, not upstream CVS.
# To enable mod_pgsql, put it after or in place of mod_mysql.
#EXTRAMODS = mod_mysql:mod_ratio:mod_quota:
EXTRAMODS = mod_ratio:mod_quota:
MYSQLMODS = mod_sql:mod_sql_mysql:
PGSQLMODS = mod_sql:mod_sql_postgres:
LDAPMODS = mod_ldap:

# This should be commented out for the build to succeed.
#DEBUG = -DDEBUG_NOFORK -g3 -O0

configure_args := --prefix=/usr --cache-file=../config.cache		\
	--sysconfdir=/etc --localstatedir=/var/run --enable-autoshadow	\
	--srcdir=../..

ifneq ($(DEB_HOST_GNU_TYPE),$(DEB_BUILD_GNU_TYPE))
  configure_args += --enable-x-compile --build=$(DEB_BUILD_GNU_TYPE)
endif

$(BUILD_TREE)/configure: $(BUILD_TREE)/configure.in
	cd $(BUILD_TREE) && autoconf


proftpd.conf.5: build-tree/$(TAR_DIR)/doc/Configuration.html
	chmod +x debian/html2man.pl
	debian/html2man.pl <build-tree/$(TAR_DIR)/doc/Configuration.html >proftpd.conf.5

stampdir/build: stampdir/configure proftpd.conf.5 $(dh_mak_deps) 
	dh_testdir
	touch build-tree/$(TAR_DIR)/configure # avoid running autoconf
	( cd build-tree/$(TAR_DIR)/contrib/libcap && $(MAKE) )  # Build libcap
	# The -DLINUX bit is cause ./configure doesnt detect alpha
	( cd build-tree/$(TAR_DIR)/build/pam && $(MAKE) all PLATFORM=-DLINUX )
	( cd build-tree/$(TAR_DIR)/build/mysql && $(MAKE) proftpd PLATFORM=-DLINUX )
	( cd build-tree/$(TAR_DIR)/build/pgsql && $(MAKE) proftpd PLATFORM=-DLINUX )
	( cd build-tree/$(TAR_DIR)/build/ldap && $(MAKE) proftpd PLATFORM=-DLINUX )
	touch stampdir/build

build: stampdir/build

stampdir/configure: $(unpacked) $(patched)
	for d in pam mysql pgsql ldap ; do mkdir -p build-tree/$(TAR_DIR)/build/$$d/contrib/dist/rpm ; done
	cd build-tree/$(TAR_DIR)/modules ; for m in mod_{ratio,quota,sql,sqlpw,mysql,pgsql}.c ; do if [ ! -e $m ] ; then ln -s ../contrib/$m . ; fi ; done

	( cd build-tree/$(TAR_DIR)/build/pam && CC="$(CC) -Wall $(CFLAGS) $(DEBUG) -I.. -I../../.." \
		../../configure $(configure_args) --with-modules=$(LINUXPRIVS)$(EXTRAMODS)$(PAM)mod_readme )
	( cd build-tree/$(TAR_DIR)/build/mysql && CC="$(CC) -Wall $(CFLAGS) $(DEBUG) -I ../ -I../../.." \
		../../configure $(configure_args) --with-modules=$(MYSQLMODS)$(LINUXPRIVS)$(EXTRAMODS)$(PAM)mod_readme )
	( cd build-tree/$(TAR_DIR)/build/pgsql && CC="$(CC) -Wall $(CFLAGS) $(DEBUG) -I ../ -I../../.." \
		../../configure $(configure_args) --with-modules=$(PGSQLMODS)$(LINUXPRIVS)$(EXTRAMODS)$(PAM)mod_readme )
	( cd build-tree/$(TAR_DIR)/build/ldap && CC="$(CC) -Wall $(CFLAGS) $(DEBUG) -I ../ -I../../.." \
		../../configure $(configure_args) --with-modules=$(LDAPMODS)$(LINUXPRIVS)$(EXTRAMODS)$(PAM)mod_readme )
	touch stampdir/configure

clean:
	dh_testdir
	dh_clean
	rm -rf stampdir build-tree
	rm -f stamp-build stamp-configure debian/files.saved debian/files
	rm -rf build
	rm -f $$(find . -type l) $$(find . -name "*~" -o -name "*.o")
	rm -f proftpd config.cache config.log lib/*.a ftpshut Make.rules
	rm -rf debian/proftpd debian/proftpd-{doc,common,ldap,pgsql,mysql}
	rm -rf debian/{files*,*substvars*,*.gz} core
	rm -f $$(find * -name "*.orig") modules/mod_ratio.c
	rm -f contrib/dist/rpm/proftpd.spec
	rm -f contrib/libcap/_makenames contrib/libcap/libcap.a
	rm -f proftpd.conf.5
	chmod +x debian/getkernelversion

binary-indep:	checkroot build
	dh_testdir
	dh_clean -k -i
	dh_installdirs -i $(DOC) -pproftpd-doc
	dh_installdocs -i debian/copyright
	dh_installchangelogs -i build-tree/$(TAR_DIR)/ChangeLog
	dh_installdocs -i build-tree/$(TAR_DIR)/README* \
		debian/{READ*,*.txt} build-tree/$(TAR_DIR)/doc/rfc \
		$$(find build-tree/$(TAR_DIR)/doc/* -type f -maxdepth 0 | egrep -v "Get|Show|license")
	dh_installexamples -i build-tree/$(TAR_DIR)/sample-configurations/*
	dh_compress -i
	dh_fixperms -i
	dh_installdeb -i
	dh_gencontrol -i -u-isp
	dh_md5sums -i
	dh_fixperms -i
	dh_builddeb -i

binary-arch:	checkroot build
	dh_testdir
	dh_clean -k -i
	dh_installdirs -a $(DOC) $(MAN)
	dh_installdocs -a debian/copyright
	dh_installchangelogs -a build-tree/$(TAR_DIR)/ChangeLog

	for t in mysql pgsql ldap ; do \
		for d in postinst postrm prerm config init ; do \
			ln -sf proftpd.$$d debian/proftpd-$$t.$$d ; \
		done ; \
		ln -sf proftpd.templates debian/proftpd-$$t.templates ; \
		for d in de es fr nl pt_BR ru ; do \
			ln -sf proftpd.templates.$$d debian/proftpd-$$t.templates.$$d ; \
		done ; \
	done

	for d in pam mysql pgsql ldap ; do \
		case $$d in \
			pam) \
				packagename="proftpd" ;\
			;; \
			*) \
				packagename="proftpd-$$d" ;\
			;; \
		esac ;\
		cwd=`pwd` ; \
		set -e ; \
		dh_installdirs -p$$packagename etc/pam.d etc/init.d etc/default ;\
		( cd build-tree/$(TAR_DIR)/build/$$d && $(MAKE) install-proftpd install-man-proftpd prefix=$$cwd/debian/$$packagename/usr \
			rundir=$$cwd/debian/$$packagename/var/run/proftpd \
			mandir=$$cwd/debian/$$packagename/usr/share/man \
			sysconfdir=$$cwd/debian/$$packagename/etc INSTALL=install ) ;\
		rm debian/$$packagename/usr/sbin/in.proftpd ;\
		cp debian/basic.conf debian/$$packagename/etc/proftpd.conf;\
		mkdir -p debian/$$packagename/usr/share/doc/examples ;\
		cp debian/basic.conf debian/$$packagename/usr/share/doc/examples/proftpd.conf ;\
		cp debian/ftpusers debian/$$packagename/etc ;\
		install -m 755 debian/proftpd.init debian/$$packagename/etc/init.d/proftpd ;\
		install -m 644 debian/proftpd.pam debian/$$packagename/etc/pam.d/proftpd ;\
		install -m 644 debian/default debian/$$packagename/etc/default/proftpd ;\
	done

	dh_installdirs -pproftpd-common usr/bin usr/share/man usr/share/man/man1 usr/share/man/man5 usr/share/man/man8
	cwd=`pwd` ;\
	( cd build-tree/$(TAR_DIR)/build/pam && $(MAKE) install-utils install-man-utils prefix=$$cwd/debian/proftpd-common/usr \
		rundir=$$cwd/debian/proftpd-common/var/run/proftpd \
		mandir=$$cwd/debian/proftpd-common/usr/share/man \
		sysconfdir=$$cwd/debian/proftpd-common/etc INSTALL=install )

	dh_installdirs -pproftpd-common usr/share/man/man5 usr/sbin etc/cron.monthly
	install debian/ftpusers.5 debian/proftpd-common/usr/share/man/man5/ftpusers.5
	install proftpd.conf.5 debian/proftpd-common/usr/share/man/man5/proftpd.conf.5
	install build-tree/$(TAR_DIR)/contrib/xferstats.holger-preiss \
		debian/proftpd-common/usr/sbin/ftpstats

	install -m 755 build-tree/$(TAR_DIR)/contrib/genuser.pl debian/proftpd-common/usr/share/doc/proftpd-common/genuser.pl
	install -m 755 build-tree/$(TAR_DIR)/contrib/ftpasswd debian/proftpd-common/usr/sbin/ftpasswd
	install -m 755 debian/proftpd.cron.monthly debian/proftpd-common/etc/cron.monthly/proftpd
	cp debian/ftpstats.8 debian/proftpd-common/usr/share/man/man8

#	cd debian && uudecode manual.uue && tar -zxvf www.proftpd.org.tar.gz
#	cp -a debian/www.proftpd.org/{images,*.html} \
#		debian/proftpd/$(DOC)/proftpd

	dh_installdebconf -a
	dh_compress -a
	dh_fixperms -a
	dh_installdeb -a
	dh_shlibdeps -a
	dh_perl -a
	dh_gencontrol -a -u-isp
	dh_strip -a
	dh_md5sums -a
	dh_fixperms -a
	dh_builddeb -a
	if egrep "^DEBUG" debian/rules; then false; fi

binary:	binary-indep binary-arch

checkroot:
	dh_testdir
	dh_testroot

.PHONY: binary binary-arch binary-indep clean checkroot

tidy:
	rm -f o *~ debian/*~
	- cd debian && indent -v mod_mysql.c mod_pgsql.c mod_ratio.c
	cd debian && makeinfo --no-headers -o mod_mysql.txt mod_mysql.texi
	cd debian && makeinfo --no-headers -o mod_pgsql.txt mod_pgsql.texi
