#!/usr/bin/make -f

include /usr/share/dpkg/pkg-info.mk

DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)
DEB_HOST_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)

# Determine whether we're building with optimization.  This doesn't really
# work at the moment due to upstream problems.
ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
    DEBIAN_OPT_FLAGS = --disable-optimize --disable-optimize-lwp
    DEBIAN_KERN_FLAGS = --disable-optimize-kernel
else ifneq (,$(findstring arm-linux-gnueabi,$(DEB_HOST_GNU_TYPE)))
    DEBIAN_OPT_FLAGS = --disable-optimize --disable-optimize-lwp
    DEBIAN_KERN_FLAGS = --disable-optimize-kernel
else
    DEBIAN_OPT_FLAGS =
    DEBIAN_KERN_FLAGS =
endif

# Skip lengthy parts of the doc build when requested
ifneq (,$(findstring nodoc,$(DEB_BUILD_OPTIONS)))
    DOCMAKE=echo --
    DOXYCMD=
    GENDOCS=
else
    # pdfTeX refuses to fully obey SOURCE_DATE_EPOCH without
    # FORCE_SOURCE_DATE=1.
    DOCMAKE=FORCE_SOURCE_DATE=1 make
    DOXYCMD=dh_doxygen -popenafs-doc
    GENDOCS=AdminGuide QuickStartUnix UserGuide
endif

SYS_NAME  := $(shell sh debian/sysname)

package    = openafs
srcpkg     = openafs-modules-source
dkmspkg    = openafs-modules-dkms
DOCS       = $(CURDIR)/debian/openafs-doc/usr/share/doc/openafs-doc

# The path to the XSLT used to generate HTML from the DocBook source files.
# This path is specific to different Linux distributions, so we have to pass
# it into configure.
HTML_XSL   = /usr/share/xml/docbook/stylesheet/nwalsh/html/chunk.xsl

# The /usr/share/doc directory for these packages should be a symlink to
# /usr/share/doc/openafs-client.  Any package on this list must depend on
# openafs-client.  This variable is used as options to dh_installdocs with
# the --link-doc option.
DOC_PACKAGES := -popenafs-fuse

# These are files that we know we don't want to install.  List them so that we
# can use dh_missing --fail-missing and catch anything new that shows up.
IGNORE = -XAuthLog -Xaklog_dynamic_auth.8 -Xcopyauth -Xdlog -Xdpass	\
	-Xfms.log -Xfs_chgrp.1 -Xfs_chown.1 -Xfs_cscpolicy.1		\
	-Xfs_memdump.1 -Xfs_minidump.1 -Xfs_trace.1	\
	-Xkadb_check -Xkaserver -Xkdb -Xkdump -Xkpwvalid -Xpagsh.krb	\
	-Xsymlink -Xtokens.krb -Xuss -Xvldb_convert -Xvsys		\
	-Xxfs_size_check

# Installed via other means since we have to rename them.
IGNORE += -Xafsd.fuse.8

# These variables are used by get-orig-source, to construct dkms.conf, and
# to set the build version.  You will need to change TAG to package stable
# releases instead of experimental releases.
DEBVERS = $(shell echo '$(DEB_VERSION)' | cut -d- -f1)
VERSION = $(shell echo '$(DEBVERS)' | sed -e 's/[+-].*//' -e 's/~//g')
TAG     = $(shell echo 'openafs-stable-$(VERSION)' | sed 's/\./_/g')
REPO    := git://git.openafs.org/openafs.git

# Upstream does tarball releases for major releases, but not for point
# relesaes, and the tarball releases are split into src and doc and contain
# the WINNT directory.  Dropping WINNT, which is not used on Debian, saves a
# substantial amount of space in the source package, and there's no reason
# to include the files generated by regen.sh when we're going to run it
# again ourselves anyway.
#
# This rule therefore generates an upstream tarball from the upstream Git
# tag, rather than the tarball release, without the generated files that are
# not in Git and without the WINNT directory.  It assumes that git-core is
# installed and there's network connectivity to the upstream repository.
get-orig-source:
	git archive --remote='$(REPO)' --prefix='openafs_$(DEBVERS).orig/' \
	    --format=tar '$(TAG)'					   \
	    | tar --delete 'openafs_$(DEBVERS).orig/src/WINNT'		   \
	    | xz > 'openafs_$(DEBVERS).orig.tar.xz'

# Use dh for most actions, with overrides below.
# openafs provides regen.sh that supersedes autoreconf.
%:
	dh $@ --without-autoreconf

# Handle the renaming of the up man page to afs-up here since the man pages
# are generated from POD source by regen.sh.
override_dh_auto_configure:
	@if test x"$(SYS_NAME)" = x"UNKNOWN" ; then exit 1 ; fi
	echo '$(DEB_VERSION)'-debian > .version
	mv doc/man-pages/pod1/up.pod doc/man-pages/pod1/afs-up.pod
	rm -f doc/man-pages/man1/up.1
	sh regen.sh
	afslogsdir=/var/log/openafs afslocaldir=/var/lib/openafs/local	   \
	    dh_auto_configure --					   \
	    --with-afs-sysname=$(SYS_NAME) --disable-kernel-module	   \
	    --libdir=\$${prefix}/lib --libexecdir=\$${prefix}/lib	   \
	    --localstatedir=/var/lib --enable-supergroups		   \
	    --enable-fuse-client --enable-debug --enable-debug-lwp	   \
	    --with-docbook2pdf=dblatex --with-xslt-processor=xsltproc	   \
	    --with-html-xsl=$(HTML_XSL)					   \
	    --with-krb5-include=/usr/include/mit-krb5			   \
	    --with-krb5-lib=/usr/lib/$(DEB_HOST_MULTIARCH)/mit-krb5	   \
	    --with-roken-include=/usr/include/heimdal			   \
	    --with-roken-lib=/usr/lib/$(DEB_HOST_MULTIARCH)/heimdal	   \
	    --with-hcrypto-include=/usr/include/heimdal			   \
	    --with-hcrypto-lib=/usr/lib/$(DEB_HOST_MULTIARCH)/heimdal	   \
	    --without-ctf-tools						   \
	    $(DEBIAN_OPT_FLAGS)

# Also build a libafs_tree and build the XML documentation if building
# architecture-independent packages.
override_dh_auto_build-indep:
	dh_auto_build
	$(MAKE) libafs_tree
	set -e; for d in $(GENDOCS); do	\
	    $(DOCMAKE) -C doc/xml/$$d all ;				\
	done

# The manipulation of debian/changelog here is unnecessary with a regular
# Debian build.  It is provided solely so that upstream can share the Debian
# packaging files, working around problems caused by upstream's distclean
# removing packaging/Debian/changelog because it's Autoconf-generated.
override_dh_auto_clean:
	[ ! -f doc/man-pages/pod1/afs-up.pod ] \
	    || mv doc/man-pages/pod1/afs-up.pod doc/man-pages/pod1/up.pod
	set -e; for d in $(GENDOCS); do	    \
	    [ ! -f doc/xml/$$d/Makefile ] || $(DOCMAKE) -C doc/xml/$$d clean ; \
	done
	[ ! -f debian/changelog.in ] \
	    || cp debian/changelog debian/changelog.save
	dh_auto_clean -- maintainer-clean
	[ -f debian/changelog ] || mv debian/changelog.save debian/changelog

# We have to use a non-standard install target and then do some fiddling
# with the results.
override_dh_auto_install:
	mkdir -p $(CURDIR)/debian/tmp
	$(MAKE) install_nolibafs DESTDIR=$(CURDIR)/debian/tmp
	rm -f debian/tmp/usr/bin/klog.krb
	rm -f debian/tmp/usr/share/man/man1/klog.krb.1
	rm -f debian/tmp/usr/share/man/man8/kas*.8
	rm -f debian/tmp/usr/share/man/man1/kkpasswd.1

# Override dh_install for architecture-dependent builds to move some files
# around, do some manual installation of things like PAM modules, and ensure
# that scripts we install are executable
override_dh_install-arch:
	chmod +x debian/afs-rootvol debian/afs-newcell
	dh_install $(IGNORE)

	mv debian/openafs-client/usr/bin/pagsh \
	    debian/openafs-client/usr/bin/pagsh.openafs
	mv debian/openafs-client/usr/share/man/man1/pagsh.1 \
	    debian/openafs-client/usr/share/man/man1/pagsh.openafs.1
	mv debian/openafs-client/usr/bin/up \
	    debian/openafs-client/usr/bin/afs-up

	install -d debian/openafs-dbserver/usr/share/man/man8
	( cd debian && pod2man --section 8 --center "Debian GNU/Linux"	\
	    afs-rootvol )						\
	    >debian/openafs-dbserver/usr/share/man/man8/afs-rootvol.8
	( cd debian && pod2man --section 8 --center "Debian GNU/Linux"	\
	    afs-newcell )						\
	    >debian/openafs-dbserver/usr/share/man/man8/afs-newcell.8

# Override dh_install for architecture-independent builds to handle
# installation of all the extra documentation that upstream doesn't install
# by default and install the source required for the kernel source package
# and the DKMS package.
override_dh_install-indep:
	mkdir -p $(DOCS)/html
	-cp doc/xml/*.gif doc/xml/*.html $(DOCS)/html/
	set -e; for d in $(GENDOCS); do	\
	    mkdir $(DOCS)/html/$$d ;					\
	    cp doc/xml/$$d/*.html $(DOCS)/html/$$d/ ;			\
	    cp doc/xml/$$d/*.pdf $(DOCS)/ ;				\
	done
	-cp doc/xml/AdminGuide/*.png $(DOCS)/html/AdminGuide/
	mkdir $(DOCS)/html/arch-overview
	cp doc/txt/arch-overview.h $(DOCS)/html/arch-overview
	cd $(DOCS)/html/arch-overview && doxygen -g && doxygen \
	    && rm Doxyfile *.h && mv html/* . && rm -r latex html
	set -e; for h in doc/protocol/*.h ; do				\
	    mkdir $(DOCS)/html/`basename $$h .h` ;			\
	    cp $$h $(DOCS)/html/`basename $$h .h` ;			\
	    cd $(DOCS)/html/`basename $$h .h` && doxygen -g && doxygen	\
		&& rm Doxyfile *.h && mv html/* . && rm -r latex html	\
		&& cd $(CURDIR) ;					\
	done
	$(DOXYCMD)

	install -d debian/$(srcpkg)/usr/src/modules/$(package)
	cd libafs_tree && find . -print \
	    | cpio -admp $(CURDIR)/debian/$(srcpkg)/usr/src/modules/$(package)
	cp .version debian/$(srcpkg)/usr/src/modules/$(package)/
	mkdir -p debian/$(srcpkg)/usr/src/modules/$(package)/debian
	cp debian/copyright debian/module/* \
	    debian/$(srcpkg)/usr/src/modules/$(package)/debian/
	sed 's/^openafs /openafs-modules /' debian/changelog \
	    > debian/$(srcpkg)/usr/src/modules/$(package)/debian/changelog
	chmod 755 debian/$(srcpkg)/usr/src/modules/$(package)/debian/rules
	chown -R root.src debian/$(srcpkg)
	find debian/$(srcpkg) -type d | xargs chmod 755
	find debian/$(srcpkg) -type f -perm -100 | xargs chmod 755
	find debian/$(srcpkg) -type f -not -perm -100 | xargs chmod 644
	chmod 775 debian/$(srcpkg)/usr/src/modules
	cd debian/$(srcpkg)/usr/src && find modules -print0 | LC_ALL=C sort -z | tar --no-recursion --null -T - $(if $(SOURCE_DATE_EPOCH),--mtime=@$(SOURCE_DATE_EPOCH)) -cf $(package).tar
	bzip2 debian/$(srcpkg)/usr/src/$(package).tar
	chmod 644 debian/$(srcpkg)/usr/src/$(package).tar.bz2
	install -d debian/$(dkmspkg)/usr/src
	mv debian/$(srcpkg)/usr/src/modules/$(package) \
	    debian/$(dkmspkg)/usr/src/openafs-$(VERSION)
	rm -rf debian/$(dkmspkg)/usr/src/openafs-$(VERSION)/debian
	rmdir debian/$(srcpkg)/usr/src/modules
	dh_dkms -V $(VERSION)

	install -d debian/$(srcpkg)/usr/share/doc/$(srcpkg)
	install -m 644 debian/openafs-client.NEWS \
	    debian/$(srcpkg)/usr/share/doc/$(srcpkg)/NEWS.Debian

# We don't want to stop the init scripts on upgrade.
override_dh_installinit:
	dh_installinit -popenafs-client -r
	dh_installinit -popenafs-fileserver -r

override_dh_installsystemd:
	dh_installsystemd -r

# Symlink /usr/share/doc for some packages.
override_dh_installdocs:
	dh_installdocs $(DOC_PACKAGES) --link-doc=openafs-client
	dh_installdocs --remaining-packages

# Install the upstream NEWS file as the upstream changelog.
override_dh_installchangelogs:
	dh_installchangelogs NEWS

# We want --fail-missing behavior, but --list-missing is the default
override_dh_missing-arch:
	dh_missing --fail-missing $(IGNORE)

# Override some of our permissions.
override_dh_fixperms-arch:
	dh_fixperms
	chmod 700 debian/openafs-client/var/cache/openafs
	chmod 700 debian/openafs-dbserver/var/lib/openafs/db
	chmod 700 debian/openafs-fileserver/var/lib/openafs/local

override_dh_strip:
	dh_strip --dbgsym-migration='openafs-dbg (<< 1.6.18.3-3~)'
