#!/usr/bin/make -f
# 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

NAME = systemimager-ssh
ARCH = $(shell dpkg --print-architecture)
PKG = $(NAME)-$(ARCH)
VERSION = $(shell dpkg-parsechangelog | grep ^Version: | \
                  cut -d " " -f 2 | cut -d "-" -f 1)
BUILD_DIR = $(NAME)-$(VERSION)
UPSTREAM_TARBALL = $(NAME)-$(VERSION).tar.bz2


PATCHES=$(sort $(shell find $(CURDIR)/patches -name "*.patch"))

configure: configure-stamp
configure-stamp:
	dh_testdir
	# Add here commands to configure the package.
	@echo -n "Extracting upstream tarball..."
	@tar xfj $(UPSTREAM_TARBALL)
	@echo "done."
	@cd $(BUILD_DIR) && \
          $(foreach patch, $(PATCHES), \
            echo -n Applying `basename $(patch)`... && cat $(patch) | \
	    patch -s -p1 && echo done. &&) /bin/true
	touch configure-stamp

copy_changelog:	$(BUILD_DIR)/changelog

$(BUILD_DIR)/changelog:	$(BUILD_DIR)/CHANGE.LOG
	cd $(BUILD_DIR) && cp CHANGE.LOG changelog

build:	build-arch build-indep

build-indep:	cleandeb configure-stamp build-indep-stamp copy_changelog

build-indep-stamp:
# nothing to do
	touch build-indep-stamp

build-arch:	cleandeb configure-stamp build-arch-stamp copy_changelog

build-arch-stamp:
	dh_testdir

	$(MAKE) -C $(BUILD_DIR) WITH_SSH=1 ssh_tarball

	touch build-arch-stamp

debian/control:
	sed -e 's/$${misc\:Package\-Arch}/$(ARCH)/g' \
	    -e 's/$${misc\:Real\-Arch}/all/;' \
	       debian/control.in > debian/control

clean:	debian/control
	dh_testdir
	dh_testroot
	rm -f build-indep-stamp build-arch-stamp configure-stamp

	# Add here commands to clean up after the build process.
	rm -rf $(BUILD_DIR)
	cd $(CURDIR)/debian && rm -rf $(PKG)
	dh_clean

install-arch:	build-arch
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs

	cd $(CURDIR)/debian && mkdir -p $(PKG)

	# Add here commands to install the packages
	$(MAKE) -C $(BUILD_DIR) install_ssh_tarball \
	  DESTDIR=$(CURDIR)/debian/$(PKG) PREFIX=/usr WITH_SSH=1

install-indep:	build-indep
# We have nothing to do by default

# Build architecture-dependent files here.
binary-arch: build-arch install-arch
	dh_testdir
	dh_testroot
	# generate control file suitable for the boot package
	# substvars don't seem to work, so we hack around it with sed.
	cp debian/control debian/control.orig
	dh_installdocs
	dh_installchangelogs
	dh_link
	dh_strip
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_perl
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums 
	dh_builddeb

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

binary: binary-indep binary-arch
.PHONY: build cleandeb clean binary-indep binary-arch binary install configure debian/control
