#!/usr/bin/make -f
# Sample debian/rules that uses debhelper. 
# GNU copyright 1997 by Joey Hess.
#
# This version is for a hypothetical package that builds an
# architecture-dependant package, as well as an architecture-independent
# package.

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

# This has to be exported to make some magic below work.
export DH_OPTIONS

ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
    DEBIAN_OPT_FLAGS = --disable-optimize --disable-lwp-optimize
    DEBIAN_KERN_FLAGS = --disable-kernel-optimize
else
    DEBIAN_OPT_FLAGS =
    DEBIAN_KERN_FLAGS =
endif

ifndef KSRC
    KSRC = /usr/src/linux
endif
ifndef KPKG_DEST_DIR
    KPKG_DEST_DIR = ..
endif
KVERS := $(shell perl debian/kernel-version $(KSRC))
export KSRC 
export KVERS
export KPKG_DEST_DIR

SYS_NAME  := $(shell KSRC="$(KSRC)" KVERS="$(KVERS)" sh debian/sysname)

package    = openafs
srcpkg     = openafs-modules-source
modulepkg := openafs-modules-$(KVERS)${INT_SUBARCH}
moduledir := debian/$(modulepkg)/lib/modules/$(KVERS)/fs

# These packages have lintian overrides.
LINTIAN_PACKAGES = openafs-dbserver openafs-client openafs-fileserver

# 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.
DOC_PACKAGES = libpam-openafs-kaserver openafs-dbserver openafs-fileserver \
	openafs-kpasswd

# These variable is used only by get-orig-source, which will normally only be
# run by maintainers.
VERSION   = 1.4.0
UPSTREAM  = /afs/grand.central.org/software/openafs/$(VERSION)

# Download the upstream source and do the repackaging that we have to do for
# DFSG reasons.  This assumes AFS is mounted, as it's generally only used by
# the package maintainers.
get-orig-source:
	cp $(UPSTREAM)/openafs-$(VERSION)-src.tar.bz2 .
	tar xjf openafs-$(VERSION)-src.tar.bz2
	rm openafs-$(VERSION)-src.tar.bz2
	rm -r openafs-$(VERSION)/src/packaging/MacOS
	rm -r openafs-$(VERSION)/src/WINNT
	tar cf openafs_$(VERSION).orig.tar openafs-$(VERSION)
	rm -r openafs-$(VERSION)
	gzip -9 openafs_$(VERSION).orig.tar

configure: configure-stamp
configure-stamp:
	@if test x"$(SYS_NAME)" = x"UNKNOWN" ; then exit 1 ; fi
	dh_testdir
	afslogsdir=/var/log/openafs afslocaldir=/etc/openafs/server-local \
	    sh configure \
	    --with-afs-sysname=$(SYS_NAME) --disable-kernel-module \
	    --prefix=/usr --sysconfdir=/etc --libexecdir=/usr/lib \
	    --localstatedir=/var/lib --enable-supergroups \
	    --enable-largefile-fileserver --enable-debug --enable-lwp-debug \
	    $(DEBIAN_OPT_FLAGS)
	chmod a+x src/libafs/make_kbuild_makefile.pl         
	touch configure-stamp

build: configure-stamp build-stamp
build-stamp:
	dh_testdir
	mkdir -p $(CURDIR)/debian/tmp
	$(MAKE) install_nolibafs DESTDIR=$(CURDIR)/debian/tmp
	chmod +x debian/afs-rootvol debian/afs-newcell debian/doc/build-man
	cd debian/doc && ./build-man
	touch build-stamp

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp configure-stamp
	rm -f build-modules-stamp configure-modules-stamp
ifeq (Makefile,$(wildcard Makefile))
	$(MAKE) distclean
endif
	rm -rf debian/doc/man1 debian/doc/man5 debian/doc/man8
	dh_clean

install: DH_OPTIONS=
install: build
	dh_testdir
	dh_testroot
	dh_installdirs
	dh_install

	set -e; for pkg in $(LINTIAN_PACKAGES) ; do \
	    install -d debian/$$pkg/usr/share/lintian/overrides; \
	    install -m 644 -c debian/$$pkg.lintian \
	        debian/$$pkg/usr/share/lintian/overrides/$$pkg; \
	done
	set -e; for pkg in $(DOC_PACKAGES) ; do \
	    ln -s openafs-client debian/$$pkg/usr/share/doc/$$pkg; \
	done

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

	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
	/usr/bin/docbook-to-man debian/pt_util.sgml \
	    >debian/openafs-dbserver/usr/share/man/man8/pt_util.8
	( 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

# Build architecture-independent files here.  Pass -i to all debhelper
# commands in this target to reduce clutter.
binary-indep: DH_OPTIONS=-i
binary-indep: build install binary-source
	dh_testdir
	dh_testroot
	dh_installchangelogs NEWS
	dh_installdocs
	dh_link
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_gencontrol
	dh_md5sums
	dh_builddeb

# Build architecture-dependent files here.  Pass -a to all debhelper commands
# in this target to reduce clutter.
binary-arch: DH_OPTIONS=-a
binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installchangelogs NEWS
	dh_installdocs
	dh_installdebconf
	DH_OPTIONS= dh_installinit -popenafs-client -r -- defaults 25 18
	DH_OPTIONS= dh_installinit -popenafs-fileserver -r
	dh_link
	dh_strip
	dh_compress
	dh_fixperms
	chmod 700 debian/openafs-client/var/cache/openafs
	chmod 700 debian/openafs-dbserver/var/lib/openafs/db
	chmod 700 debian/openafs-fileserver/etc/openafs/server
	chmod 700 debian/openafs-fileserver/var/lib/openafs/cores
	dh_perl
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary: binary-indep binary-arch

#####################  Building openafs-modules-source  ######################

binary-source:
	dh_testdir
	dh_testroot
	-rm -rf debian/$(srcpkg) debian/substvars
	install -d debian/$(srcpkg)/usr/src/modules/$(package)
	find . \( -name \*.o -o -path ./debian/$(srcpkg) -o -path \*/CVS \
	    -o -path \*/.svn -o -path ./src/WINNT  \
	    -o -path ./obj -o -path ./$(SYS_NAME)  \
	    -o \( -path ./debian/\* -type d \) \
	    -o -path ./debian/\*debhelper \) -prune -o -print | \
	        cpio -admp debian/$(srcpkg)/usr/src/modules/$(package)
	cd debian/$(srcpkg)/usr/src/modules/$(package) && \
	    $(MAKE) -f debian/rules clean && \
	    mv debian/kern-sysname debian/sysname
	-cd debian/$(srcpkg)/usr/src/modules/$(package) && \
	    rm -rf src/libafs/rx src/libafs/afs src/libafs/afsint
	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 && \
	    rm -r modules
	gzip -9 debian/$(srcpkg)/usr/src/$(package).tar
	chmod 644 debian/$(srcpkg)/usr/src/$(package).tar.gz

####################  Module package support (make-kpkg)  ####################

kdist_configure: configure-modules-stamp

kdist_image: build-modules-stamp
	$(ROOT_CMD) $(MAKE) $(MFLAGS) -f debian/rules binary-modules
	$(ROOT_CMD) $(MAKE) $(MFLAGS) -f debian/rules clean

kdist: build-modules-stamp
	$(ROOT_CMD) $(MAKE) $(MFLAGS) -f debian/rules binary-modules
	KSRC="$(KSRC)" KMAINT="$(KMAINT)" KEMAIL="$(KEMAIL)" \
	    sh -v debian/genchanges.sh
	$(ROOT_CMD) $(MAKE) $(MFLAGS) -f debian/rules clean

kdist_clean:
	$(ROOT_CMD) $(MAKE) $(MFLAGS) -f debian/rules clean

########################  Kernel module build rules  #########################

configure-modules: configure-modules-stamp
configure-modules-stamp:
	@if test x"$(SYS_NAME)" = x"UNKNOWN" ; then exit 1 ; fi
	@if test x"$(KVERS)" = x ; then \
	    echo 'No version in $(KSRC)/include/linux/version.h' >&2 ; \
	    exit 1 ; \
	fi
	sh debian/prep-modules $(KSRC)
	sh configure --with-afs-sysname=$(SYS_NAME) \
	    --with-linux-kernel-headers=$(KSRC) --prefix=`pwd`/debian/tmp \
	    --enable-debug --enable-lwp-debug --enable-kernel-debug \
	    $(DEBIAN_OPT_FLAGS) $(DEBIAN_KERN_FLAGS)
	touch configure-modules-stamp

build-modules: build-modules-stamp
build-modules-stamp: configure-modules-stamp
	make only_libafs
	touch build-modules-stamp

install-modules: build-modules-stamp
	dh_testdir
	dh_testroot
	dh_clean -k
	cd src/libafs && make install
	install -d -g root -o root -m 755 $(moduledir)
	install -g root -o root -m 755 debian/tmp/lib/openafs/*o $(moduledir)/
	if test -f $(moduledir)/openafs.o \
	        -a ! -f $(moduledir)/openafs.mp.o ; then \
	    ln $(moduledir)/openafs.o $(moduledir)/openafs.mp.o || exit 1 ; \
	fi

binary-modules: install-modules
	dh_testdir
	dh_testroot
	dh_installchangelogs NEWS
	dh_installdocs
	dh_installmodules
	dh_strip
	dh_link
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_gencontrol -- -v`cat debian/VERSION`
	dh_md5sums
	dh_builddeb --destdir="$(KPKG_DEST_DIR)"

.PHONY: build clean binary-indep binary-arch binary install configure
.PHONY: kdist_configure kdist_image kdist kdist_clean binary-source
