#!/usr/bin/make -f
SHELL+= -e

D := $(CURDIR)/debian/inn2
B := $(CURDIR)/build

include /usr/share/dpkg/architecture.mk
ifeq ($(DEB_HOST_ARCH_BITS),32)
configure_flags = --enable-largefiles
endif

# the upstream source needs to be copied in the flavor-specific build dirs
src_files := $(shell find . -maxdepth 1 \
	-not -name . -and -not -name debian -and -not -name .pc \
	-and -not -name .git \
	-and -not -name 'build-*' -and -not -name '.stamp-*')

##############################################################################
DPKG_EXPORT_BUILDFLAGS = 1
DEB_BUILD_MAINT_OPTIONS=hardening=+pie,+bindnow
-include /usr/share/dpkg/buildflags.mk

DEB_HOST_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
ifeq ($(DEB_BUILD_GNU_TYPE),$(DEB_HOST_GNU_TYPE))
  configure_flags += --build $(DEB_HOST_GNU_TYPE)
else
  configure_flags += --build $(DEB_BUILD_GNU_TYPE) --host $(DEB_HOST_GNU_TYPE)
endif

clean:
	rm -rf .stamp-*
	dh_clean build/

$B/configure:
	dh_testdir
	mkdir -p $B
	for dir in $(src_files); do cp -ldpR $$dir $B; done

configure: $B/config.status
$B/config.status: $B/configure
	dh_update_autotools_config
	cd $B && \
	CONFIG_SHELL=/bin/sh \
	BZIP2=/bin/bzip2 \
	GZIP=/bin/gzip \
	PERL=/usr/bin/perl \
	ac_cv_path_AWK=awk \
	ac_cv_prog_AWK=awk \
	ac_cv_path_CAT=cat \
	ac_cv_path_EGREP=egrep \
	ac_cv_path_OPENSSL=openssl \
	ac_cv_path_SED=sed \
	ac_cv_path_SORT=sort \
	ac_cv_path_UUSTAT=uustat \
	ac_cv_path_UUX=uux \
	ac_cv_path_GPGV=/usr/bin/gpgv \
	ac_cv_path_GPG=/usr/bin/gpg1 \
	ac_cv_path_PATH_GETFTP=wget \
	LDFLAGS="-Wl,--as-needed $(LDFLAGS)" \
	./configure \
		--enable-uucp-rnews \
		--with-perl \
		--with-openssl \
		--prefix=/usr/lib/news \
		--libdir=/usr/lib/news \
		--mandir=/usr/share/man \
		--sysconfdir=/etc/news \
		--includedir=/usr/include \
		--with-db-dir=/var/lib/news \
		--with-filter-dir=/etc/news/filter \
		--with-log-dir=/var/log/news \
		--with-run-dir=/run/news \
		--with-spool-dir=/var/spool/news \
		--with-tmp-dir=/var/spool/news/incoming/tmp \
		--with-http-dir=/var/www/inn \
		--with-libperl-dir=/usr/share/perl5 \
		--with-bdb=/usr \
		--with-krb5=/usr \
		--with-sasl=/usr \
		--with-sendmail=/usr/sbin/sendmail \
		--with-zlib=/usr \
		$(configure_flags)
	touch $@

build: build-arch build-indep
build-arch: .stamp-build
build-indep: .stamp-build
.stamp-build: $B/config.status
	dh_testdir
	cd $B && $(MAKE)
	touch $@

install: .stamp-build
	dh_testdir
	dh_testroot
	dh_prep

	cd $B && $(MAKE) install DESTDIR=$D

	dh_movefiles --sourcedir=$(subst $(CURDIR)/,,$D)

#	move back to the main package these man pages
	mv $D-dev/usr/share/man/man3/uwildmat.3 \
		$D-dev/usr/share/man/man3/*.3pm \
		$D/usr/share/man/man3/

#	remove assorted crap and
#	make sure we don't ship active, active.times, newsgroups in place!
	cd $D/etc/news/filter && rm -f *.py *.tcl
	rm -rf $D/usr/lib/news/bin/simpleftp $D/usr/share/man/man1/simpleftp.1\
		$D/usr/lib/news/doc/ $D/usr/include/ $D/etc/news/motd.* \
		$D/usr/lib/news/share/ \
		$D/usr/lib/news/*.la \
		$D/var/lib/news/* $D/run/news/ $D/var/www/

#	compatibility symlink for nnrpd-ssl which was removed in 2.5.2-1
	ln -s nnrpd $D/usr/lib/news/bin/nnrpd-ssl
	install -m 755 extra/buildinnkeyring extra/ginpaths2 \
		$D/usr/lib/news/bin/

	mkdir $D/usr/lib/news/examples/
	cp extra/active extra/newsgroups $D/usr/lib/news/examples/

	mkdir $D/var/log/news/path

	dh_link
	dh_installchangelogs ChangeLog
	dh_installdocs
	dh_installexamples
	dh_installinit --noscripts --init-script=inn2
	dh_installcron
	dh_compress
	dh_fixperms -Xusr/lib/news/bin/innbind
	dh_strip

	# these files may contain passwords
	chown root:news $D-inews/etc/news/passwd.nntp $D/etc/news/incoming.conf $D/etc/news/innfeed.conf
	chmod 640 $D-inews/etc/news/passwd.nntp $D/etc/news/incoming.conf $D/etc/news/innfeed.conf

	perl -i -pe 's/^(pathhost:\s+).+/$${1}server.example.net/' \
		$D-inews/etc/news/inn.conf

	chmod -x $D/usr/lib/news/bin/control/*.pl $D/etc/news/*.local

	chmod u+w,o+r $D/usr/lib/news/bin/innbind
	chown news:uucp $D-inews/usr/lib/news/bin/rnews
	chmod 4754 $D-inews/usr/lib/news/bin/rnews

	chown -R news:news $D/var/spool/news/ $D/var/lib/news/ \
		$D/var/log/news/
	chmod -R g+w $D/var/spool/news/ $D/var/lib/news/ \
		$D/var/log/news/

	dh_makeshlibs --noscripts
	dh_installdeb
	dh_md5sums
	dh_shlibdeps --exclude=/usr/lib/news/bin/auth/passwd/auth_krb5 -- \
		-dSuggests $D/usr/lib/news/bin/auth/passwd/auth_krb5 \
		-dDepends

	dh_gencontrol $(no_package) -- \
	    -VPERLAPI=$$(perl -MConfig -e 'printf("perlapi-%s", $$Config{debian_abi} || $$Config{version})')
	dh_builddeb $(no_package)

binary-arch: install

binary-indep:

binary: binary-arch

get-orig-source:
	cd $(dir $(firstword $(MAKEFILE_LIST)))../ && \
	uscan --rename --force-download --destdir $(CURDIR)

.PHONY: clean configure build binary-arch binary install
