#!/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
#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
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.
	CFLAGS="$(CFLAGS)" ./configure $(confflags) \
				--prefix=/usr --enable-release \
				--mandir=\$${prefix}/share/man \
				--infodir=\$${prefix}/share/info \
				--sysconfdir=/etc --enable-dietlibc \
				--localstatedir=/var \
				--enable-apis=NOLEGACY \
				--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
	$(MAKE) -C man

	touch build-stamp

clean: unpatch
	dh_testdir
	dh_testroot
	rm -f build-stamp 

	# Add here commands to clean up after the build process.
	-$(MAKE) clean
	-$(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

	# have to remove v_ init scripts
	rm -f $(CURDIR)/debian/util-vserver/etc/init.d/v_*
	# remove newvserver.defaults (because that is linuxconf and that is not supported in debian).
	#rm -f $(CURDIR)/debian/util-vserver/etc/vservers/newvserver.defaults
#	# Move util-vserver-vars to /etc/vservers as it is needed for legacy support
#	grep -v "Id: util-vserver-vars.pathsubst,v" \
#		$(CURDIR)/debian/util-vserver/usr/lib/util-vserver/util-vserver-vars \
#		> $(CURDIR)/debian/util-vserver/etc/vservers/util-vserver-vars
#	rm -f $(CURDIR)/debian/util-vserver/usr/lib/util-vserver/util-vserver-vars
#	ln -s /etc/vservers/util-vserver-vars $(CURDIR)/debian/util-vserver/usr/lib/util-vserver/util-vserver-vars
	# 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

	# Remove development files as they are not supported upstream in a
	# proper way.
#	rm -rf $(CURDIR)/debian/util-vserver/usr/include/
#	rm -f $(CURDIR)/debian/util-vserver/usr/lib/*.so*
#	rm -f $(CURDIR)/debian/util-vserver/usr/lib/*.a
#	rm -rf $(CURDIR)/debian/util-vserver/usr/lib/pkgconfig

	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	
	dh_installinit
	dh_installman
	dh_link
	dh_strip
	dh_compress
	dh_fixperms
	dh_makeshlibs
	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 
