#!/usr/bin/make -f

# Tell Autoconf the correct system types.
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))
    SYSTEM = --build $(DEB_HOST_GNU_TYPE)
else
    SYSTEM = --build $(DEB_BUILD_GNU_TYPE) --host $(DEB_HOST_GNU_TYPE)
endif

# Set compiler flags.
CFLAGS   := $(shell dpkg-buildflags --get CFLAGS)
CPPFLAGS := $(shell dpkg-buildflags --get CPPFLAGS)
LDFLAGS  := $(shell dpkg-buildflags --get LDFLAGS)

# 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

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 = -plibpam-openafs-kaserver -popenafs-dbserver -popenafs-fuse \
	-popenafs-kpasswd

# These are files that we know we don't want to install.  List them so that we
# can use dh_install --fail-missing and catch anything new that shows up.
IGNORE = -XAuthLog -Xcopyauth -Xdlog -Xdpass -Xfms.log -Xfs_chgrp.1	   \
	-Xfs_chown.1 -Xfs_cscpolicy.1 -Xfs_flushall.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 -Xpam_afs

# 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.
DEBIAN  := $(shell dpkg-parsechangelog | grep ^Version: | cut -d' ' -f2)
DEBVERS := $(shell echo '$(DEBIAN)' | 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 xf -
	rm -r openafs_$(DEBVERS).orig/src/WINNT
	tar cf openafs_$(DEBVERS).orig.tar openafs_$(DEBVERS).orig
	rm -r openafs_$(DEBVERS).orig
	gzip -9 openafs_$(DEBVERS).orig.tar

# Use dh for most actions, with overrides below.
%:
	dh $@

# 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 '$(DEBIAN)'-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 \
	    sh configure \
	    CFLAGS='$(CFLAGS)' CPPFLAGS='$(CPPFLAGS)' LDFLAGS='$(LDFLAGS)' \
	    --with-afs-sysname=$(SYS_NAME) --disable-kernel-module \
	    --prefix=/usr --mandir=\$${prefix}/share/man \
	    --sysconfdir=/etc --libexecdir=/usr/lib --with-krb5 \
	    --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) \
	    $(DEBIAN_OPT_FLAGS) $(SYSTEM)

# Also build a libafs_tree and build the XML documentation.
override_dh_auto_build:
	chmod a+x src/libuafs/make_h_tree.pl
	dh_auto_build
	$(MAKE) libafs_tree
	set -e; for d in AdminGuide QuickStartUnix UserGuide ; do \
	    $(MAKE) -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 AdminGuide QuickStartUnix UserGuide ; do \
	    [ ! -f doc/xml/$$d/Makefile ] || $(MAKE) -C doc/xml/$$d clean ; \
	done
	[ ! -f debian/changelog.in ] \
	    || cp debian/changelog debian/changelog.save
	dh_auto_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

# Override dh_install to handle installation of all the extra documentation
# that upstream doesn't install by default, install the source required for
# the kernel source package and the DKMS package, move some files about and
# do some manually installation of additional things like PAM modules, and
# to ensure that the extra scripts we install are executable.
override_dh_install:
	chmod +x debian/afs-rootvol debian/afs-newcell
	dh_install --fail-missing $(IGNORE)

	mkdir -p $(DOCS)/html
	cp doc/xml/*.gif doc/xml/*.html $(DOCS)/html/
	set -e; for d in AdminGuide QuickStartUnix UserGuide ; 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/arch/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

	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 && tar cf $(package).tar modules
	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)

	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/klog \
	    debian/openafs-client/usr/bin/klog.afs
	mv debian/openafs-client/usr/share/man/man1/klog.1 \
	    debian/openafs-client/usr/share/man/man1/klog.afs.1
	mv debian/openafs-client/usr/bin/up \
	    debian/openafs-client/usr/bin/afs-up

	install -m 644 -c debian/tmp/usr/lib/pam_afs.so.1 \
	    debian/libpam-openafs-kaserver/lib/security/pam_afs.so
	install -m 644 -c debian/tmp/usr/lib/pam_afs.krb.so.1 \
	    debian/libpam-openafs-kaserver/lib/security/pam_afs.krb.so

	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

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

# We have multiple init scripts with some non-standard defaults.
override_dh_installinit:
	dh_installinit -popenafs-client -r -- defaults 25 20
	dh_installinit -popenafs-fileserver -r

# Create the documentation symlinks.
override_dh_installdocs:
	dh_installdocs $(DOC_PACKAGES) --link-doc=openafs-client
	dh_installdocs --remaining-packages
	set -e; if [ -d "$(DOCS)" ] ; then \
	    for d in arch-overview bos-spec fs-cm-spec rx-spec \
		     vldb-vol-spec ; do \
		if [ -f "$(DOCS)/html/$$d/jquery.js" ] ; then \
		    rm $(DOCS)/html/$$d/jquery.js ; \
		    ln -s /usr/share/javascript/jquery/jquery.min.js \
			$(DOCS)/html/$$d/jquery.js ; \
		fi ; \
	    done ; \
	fi

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

# Override some of our permissions.
override_dh_fixperms:
	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

# Only include debugging information for the servers installed into
# /usr/lib/openafs rather than for all binaries.  The servers are where
# debugging backtraces really matter, and a 10MB debugging package is
# overkill.
override_dh_strip:
	dh_strip --dbg-package=openafs-dbg
	rm -rf debian/openafs-dbg/usr/lib/debug/lib
	rm -rf debian/openafs-dbg/usr/lib/debug/sbin
	rm -rf debian/openafs-dbg/usr/lib/debug/usr/bin
	rm -rf debian/openafs-dbg/usr/lib/debug/usr/sbin
