#!/usr/bin/make -f
# Sample debian/rules that uses debhelper.
# GNU copyright 1997 to 1999 by Joey Hess.
#
# Modified to make a template file for a multi-binary package with separated
# build-arch and build-indep targets  by Bill Allombert 2001

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

export QTDIR=/usr/share/qte2
export OPIEDIR=$(shell pwd)

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

ifeq ($(DEB_BUILD_GNU_ARCH),hppa)
  export CFLAGS_EXTRA = -ftemplate-depth-99 -ffunction-sections
  export CXXFLAGS_EXTRA = -ftemplate-depth-99 -ffunction-sections
endif

ifeq ($(DEB_BUILD_GNU_ARCH),arm)
  export CFLAGS_EXTRA = -march=armv4 -mapcs-32 \
	-fexpensive-optimizations -fomit-frame-pointer
  export CXXFLAGS_EXTRA = -march=armv4 -mapcs-32 \
	-fexpensive-optimizations -fomit-frame-pointer
endif

build: patch build-stamp
build-stamp:
	dh_testdir
	-cp debian/maintain/config.in .
ifeq ($(DEB_BUILD_GNU_ARCH),arm)
	-cp debian/maintain/config-arm .config
	-cp debian/maintain/tmpconfig.h-arm .tmpconfig.h
elsif
	-cp debian/maintain/config .config
	-cp debian/maintain/tmpconfig.h .tmpconfig.h
endif
	-cp debian/maintain/depends.cfgs .depends.cfgs
	$(MAKE)
	$(MAKE) -C core/opiealarm/
	$(MAKE) INSTALL_ROOT=$(CURDIR)/debian/install-tmp/ install
	touch build-stamp

clean: unpatch
	dh_testdir
	dh_testroot
	rm -f build-stamp

	# Add here commands to clean up after the build process.
	-if [ -e qmake/qmake ] ; then \
		$(MAKE) clean ;\
	fi
	-install -d debian/temp-bin
	-cp bin/README debian/temp-bin/
	-cp bin/getprofile debian/temp-bin/
	-cp bin/opie-reorgfiles debian/temp-bin/
	-cp bin/opie-sh-howto.sh debian/temp-bin/
	-cp bin/qpe-reorgfiles debian/temp-bin/
	-rm -f lib/l* bin/* mkspecs/default *.h 
	-rm -f `find ./ -name "*.o"`
	-rm -f `find ./ -name "moc*"`
	-rm -f `find ./ -name "*.moc"`
	-rm -rf include
	-rm `find ./ -name "*.so*"`
	-cp debian/temp-bin/* bin
	-rm -rf debian/temp-bin debian/install-tmp debian/patched
	-$(MAKE) -C scripts/kconfig clean
	-$(MAKE) -C qmake distclean
	dh_clean

patch: patch-stamp
patch-stamp:
	test -d debian/patched || install -d debian/patched
	@echo "Patches applied in the Debian version of ${PACKAGE}:" > $@T
	@for patch in `cat debian/patches/00list`; do \
	stamp=debian/patched/$$patch.dpatch; \
	patch=debian/patches/$$patch.dpatch; \
	author=`sed -n "s,^## *.*dpatch by *,,p" $$patch`; \
	test -x $$patch || chmod +x $$patch; \
	if test -f $$stamp; then \
		echo "$$patch already applied."; \
		echo -e "\n$$patch ($$author):" >> $@T; \
		sed -n 's/^## *DP: */  /p' $$patch >> $@T; \
	else \
		echo -n "applying patch $$patch..."; \
		if $$patch -patch >$$stamp.new 2>&1; then \
			mv $$stamp.new $$stamp; \
			touch $$stamp; \
			echo -e "\n$$patch ($$author):" >> $@T; \
			sed -n 's/^## *DP: */  /p' $$patch >> $@T; \
			echo " ok."; \
		else \
			echo " failed."; \
			exit 1; \
		fi; \
	fi; \
	done
	mv -f $@T $@

unpatch:
	@for patch in `tac debian/patches/00list`; do \
	stamp=debian/patched/$$patch.dpatch; \
	patch=debian/patches/$$patch.dpatch; \
	test -x $$patch || chmod +x $$patch; \
	if test -f $$stamp; then \
		echo -n "reverting patch $$patch..."; \
		if $$patch -unpatch 2>&1 >/dev/null; then \
			rm -f $$stamp; \
			echo " ok."; \
		else \
			echo " failed."; \
			exit 1; \
		fi; \
	fi; \
	done

	rm -f patch-stamp


install: build
	dh_testdir
	dh_testroot
	dh_clean -k 
	dh_installdirs

	find apps sounds pics -path \*/CVS -prune -o -print | cpio -pmd debian/install-tmp/usr/share/opie/
	find etc -path \*/CVS -prune -o -print | cpio -pmd debian/install-tmp/etc/
	-mv debian/install-tmp/etc/etc debian/install-tmp/etc/opie
	find share -path \*/CVS -prune -o -print | cpio -pmd debian/install-tmp/usr/
	find plugins -path \*/CVS -prune -o -print | cpio -pmd debian/install-tmp/usr/lib/opie/
	find root -path \*/CVS -prune -o -print | cpio -pmd debian/install-tmp/
	-mv debian/install-tmp/root/etc/* debian/install-tmp/etc/opie/

	install -d debian/install-tmp/usr/share/lintian/overrides
	-cp debian/overrides/* debian/install-tmp/usr/share/lintian/overrides/
	-cp bin/getprofile debian/install-tmp/usr/bin/
	-cp bin/opiealarm debian/install-tmp/usr/bin/
	-cp bin/opie-reorgfiles debian/install-tmp/usr/bin/
	-cp bin/opie-sh-howto.sh debian/install-tmp/usr/bin/
	-cp bin/qpe-reorgfiles debian/install-tmp/usr/bin/
	-rm -rf `find debian/install-tmp/ -name "CVS"`
	-rm -rf `find debian/install-tmp/ -name ".cvsignore"`
	-chmod 644 `find debian/install-tmp/usr/share/opie/pics/ -type f`
	-chmod 644 `find debian/install-tmp/usr/share/opie/sounds/ -type f`
	dh_install --list-missing --sourcedir=debian/install-tmp

binary-common:
	dh_testdir
	dh_testroot
	dh_installdocs
	dh_installexamples
	dh_installmenu
	dh_installcron
	dh_installman
	dh_installinit -popie-taskbar-fb --init-script=opie -r --no-start
	dh_installchangelogs 
	dh_link
	dh_strip
	dh_compress 
	dh_fixperms
	dh_installdeb
	dh_shlibdeps 
	dh_perl
	dh_gencontrol
	dh_md5sums
	dh_builddeb

# Build architecture independant packages using the common target.
binary-indep: build install
	$(MAKE) -f debian/rules DH_OPTIONS=-i binary-common

# Build architecture dependant packages using the common target.
binary-arch: build install
	$(MAKE) -f debian/rules DH_OPTIONS=-s binary-common

# Any other binary targets build just one binary package at a time.
binary-%: build install
	make -f debian/rules binary-common DH_OPTIONS=-p$*


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