#!/usr/bin/make -f
# -*- makefile -*-
# Sample debian/rules that uses debhelper.
# GNU copyright 1997 to 1999 by Joey Hess.

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


# These are used for cross-compiling and for saving the configure script
# from having to guess our platform (since we know it already)
export DEB_HOST_GNU_TYPE   ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
export DEB_BUILD_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
export DEB_HOST_ARCH       ?= $(shell dpkg-architecture -qDEB_HOST_ARCH)

include /usr/share/dpatch/dpatch.make

# (Most of) the files this concerns belong to /usr/lib/util-vserver.
# To keep referencing simple, and because we're not even sure there
# are files belonging to /var/lib/util-vserver, we stay with the default
# so far.
#MAKE_OPTS =  pkglibdir=/var/lib/util-vserver PKGLIBDIR=/var/lib/util-vserver

CFLAGS = -Wall -g 

# Some checks to avoid building on some arches as there is no reserved syscalls
# for the Linux kernel on this arch.
#ifeq ($(DEB_HOST_ARCH),arm)
#	do_not_buid_this_arch
#endif

ifeq ($(DEB_HOST_ARCH),powerpc)
#    # dietlibc's sys/types.h tests for "powerpc", not "__powerpc__"
#    CFLAGS += -Dpowerpc
     confflags += --disable-alternative-syscalls
endif

# Commented out as it do no longer build with this...
ifeq ($(DEB_HOST_ARCH),sparc)
    # Targetting at older sparc's causes compilation errors. Steve Langasek
    # says this works and it is anyway the oldest sparc generation we
    # support in Debian (or kernel/glibc).
	CFLAGS += -mcpu=v8
endif

ifeq ($(DEB_HOST_ARCH),hppa)
    # dietlibc fails to define O_NOFOLLOW and O_LARGEFILE. These values
    # have been copied from glibc's bits/fcntl.h.
    # CFLAGS += -DO_NOFOLLOW=00000200 -DO_LARGEFILE=00004000
    # do_not_buid_this_arch
endif

ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -O0
else
	CFLAGS += -O2
endif
ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
	INSTALL_PROGRAM += -s
endif

# Fix to make it build on hppa, mipsel and s390
# If this is not fixed, gcc is invoked as a cross compiler.
ifeq ($(DEB_BUILD_GNU_TYPE), $(DEB_HOST_GNU_TYPE))
	confflags += --build $(DEB_HOST_GNU_TYPE)
else
	confflags += --build $(DEB_BUILD_GNU_TYPE) --host $(DEB_HOST_GNU_TYPE)
endif

config.status: configure
	dh_testdir
#	$(MAKE) -f Makefile.svn
ifneq "$(wildcard /usr/share/misc/config.sub)" ""
	if [ -f config.sub ];then mv config.sub config.sub.orig;fi
	ln -s /usr/share/misc/config.sub config.sub
endif
ifneq "$(wildcard /usr/share/misc/config.guess)" ""
	if [ -f config.guess ];then mv config.guess config.guess.orig;fi
	ln -s /usr/share/misc/config.guess config.guess
endif
	# Add here commands to configure the package.
	chmod +x ./configure
	CFLAGS="$(CFLAGS)" ./configure $(confflags) \
				--prefix=/usr --enable-release \
				--mandir=\$${prefix}/share/man \
				--infodir=\$${prefix}/share/info \
				--sysconfdir=/etc --enable-dietlibc \
				--localstatedir=/var \
				--with-vrootdir=/var/lib/vservers

build: patch build-stamp

build-stamp: config.status
	dh_testdir
	# Add here commands to compile the package.
	$(MAKE) $(MAKE_OPTS) all

	touch build-stamp

clean: unpatch
	dh_testdir
	dh_testroot
	rm -f build-stamp 

	# Add here commands to clean up after the build process.
	[ ! -f Makefile ] || $(MAKE) clean
	[ ! -f Makefile ] || $(MAKE) distclean
	rm -f config.status config.log config.sub config.guess
	if [ -f config.sub.orig ];then \
		rm -f config.sub; \
		mv config.sub.orig config.sub; \
	fi
	if [ -f config.guess.orig ];then \
		rm -f config.guess; \
		mv config.guess.orig config.guess; \
	fi
	# clean up leftover files...
	rm -rf tests/.libs vserver-start/.libs
	debconf-updatepo
	dh_clean

install: patch build
	dh_testdir
	dh_testroot
	dh_clean -k 
	dh_installdirs

	# Add here commands to install the package into debian/util-vserver.
	$(MAKE) $(MAKE_OPTS) DESTDIR=$(CURDIR)/debian/util-vserver install install-distribution
	# Fixes
	chmod +x $(CURDIR)/debian/util-vserver/usr/lib/util-vserver/distributions/template/initpre
	chmod +x $(CURDIR)/debian/util-vserver/usr/lib/util-vserver/distributions/template/initpost
	rm -f $(CURDIR)/debian/util-vserver/usr/lib/*.la

	rm -f $(CURDIR)/debian/util-vserver/etc/init.d/vservers-legacy
	rm -f $(CURDIR)/debian/util-vserver/etc/init.d/vservers-default
	rm -f $(CURDIR)/debian/util-vserver/etc/init.d/rebootmgr
	rm -f $(CURDIR)/debian/util-vserver/etc/init.d/vprocunhide
	rm -f $(CURDIR)/debian/util-vserver/etc/vservers.conf

	# remove legacy binaries
	rm -rf $(CURDIR)/debian/util-vserver/usr/lib/util-vserver/legacy
	rm -f $(CURDIR)/debian/util-vserver/usr/sbin/vserver-copy

	# legacy man pages
	rm -f $(CURDIR)/debian/util-vserver/usr/share/man/man8/distrib-info.8
	rm -f $(CURDIR)/debian/util-vserver/usr/share/man/man8/vserver-copy.8
	rm -f $(CURDIR)/debian/util-vserver/usr/share/man/man8/rebootmgr.8
	rm -f $(CURDIR)/debian/util-vserver/usr/share/man/man8/vps.*

	# legacy initscripts
	rm -f $(CURDIR)/debian/util-vserver/etc/init.d/v_*

	# Fix for symlinks in /etc/vservers/.defaults
	rm -f $(CURDIR)/debian/util-vserver/etc/vservers/.defaults/vdirbase
	rm -f $(CURDIR)/debian/util-vserver/etc/vservers/.defaults/run.rev
	# Rewrite util-vserver-vars
	perl -pi -e \
	's|^(.*DEFAULT_VSERVERDIR=).*$$|$$1"/etc/vservers/.defaults/vdirbase"|;' \
		debian/util-vserver/usr/lib/util-vserver/util-vserver-vars

	# Install bash completion
	mkdir -p $(CURDIR)/debian/util-vserver/etc/bash_completion.d
	cp $(CURDIR)/debian/vserver.bash_completion $(CURDIR)/debian/util-vserver/etc/bash_completion.d/vserver

	# Lintian overrides
	mkdir -p $(CURDIR)/debian/util-vserver/usr/share/lintian/overrides
	install -m644 debian/util-vserver.override $(CURDIR)/debian/util-vserver/usr/share/lintian/overrides/util-vserver

# Build architecture-independent files here.
binary-indep: build install
# We have nothing to do by default.

# Build architecture-dependent files here.
binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installchangelogs ChangeLog
	dh_installdocs
	dh_installexamples
	dh_installdebconf -n
	dh_installinit -r
	dh_installman
	dh_link
	dh_strip
	dh_compress
	dh_fixperms
	dh_makeshlibs -n
	dh_installdeb
	dh_shlibdeps -L util-vserver -l debian/util-vserver/usr/lib
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary: binary-indep binary-arch
.PHONY: build clean binary-indep binary-arch binary install 
