#!/usr/bin/make -f
# debian/rules for the usplash package.
# Copyright © 2006 Canonical Ltd.
# Author: Scott James Remnant <scott@ubuntu.com>

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

export DH_OPTIONS

CFLAGS = -g -Wall

# Disable optimisations if noopt found in $DEB_BUILD_OPTIONS
ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -O0
else
	CFLAGS += -Os
endif

ifeq ($(DEB_HOST_ARCH), $(findstring $(DEB_HOST_ARCH),i386 amd64 lpia))
	BACKEND = "BACKEND=svga"
else
	BACKEND =
endif

# Apply patches to the package
patch: patch-stamp
patch-stamp:
	dh_testdir
	@patches=debian/patches/*.patch; for patch in $$patches; do \
		test -f $$patch || continue; \
		echo "Applying $$patch"; \
		patch -stuN -p1 < $$patch || exit 1; \
	done
	touch $@

# Remove patches from the package
unpatch:
	dh_testdir
	@if test -f patch-stamp; then \
		patches=debian/patches/*.patch; \
		for patch in $$patches; do \
			reversepatches="$$patch $$reversepatches"; \
		done; \
		for patch in $$reversepatches; do \
			test -f $$patch || continue; \
			echo "Reversing $$patch"; \
			patch -suRf -p1 < $$patch || exit 1; \
		done; \
		rm -f patch-stamp; \
	fi

# Build the package
build: patch-stamp build-stamp
build-stamp:
	dh_testdir

	$(MAKE) $(BACKEND)

	touch $@

# Install files
install: build
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs

	$(MAKE) install DESTDIR=$(CURDIR)/debian/tmp

	# Ship initramfs hooks and scripts
	install -d $(CURDIR)/debian/tmp/usr/share/initramfs-tools
	cp -a initramfs/* $(CURDIR)/debian/tmp/usr/share/initramfs-tools 
	chmod a+x $(CURDIR)/debian/tmp/usr/share/initramfs-tools/scripts/init-top/*

binary: binary-indep binary-arch

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

# Build architecture-dependent files here.
binary-arch: DH_OPTIONS=-a
binary-arch: build install
	dh_install --sourcedir=debian/tmp
	dh_testdir
	dh_testroot
	dh_installchangelogs
	dh_installdocs
	dh_installexamples
	dh_installman
	dh_installinit -r --no-start -- start 98 2 3 4 5 . stop 01 0 1 6 .
	dh_link
	dh_strip
	dh_compress -X example
	dh_fixperms
	dh_installdeb
	dh_makeshlibs
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb


# Clean up the mess we made
clean: clean-source unpatch
clean-source:
	dh_testdir
	rm -f build-stamp

	-$(MAKE) clean

	dh_clean


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