#!/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

PREFIX=/usr

# 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)

# version=$(shell dpkg-parsechangelog | grep '^Version: ' | sed -e 's/^Version: //' | sed -e 's/-.*//')
# major=$(shell echo ${version} | cut -d. -f1-2)
# micro=$(shell echo ${version} | cut -d. -f3 | cut -d+ -f1)
# nextmicro=$(shell expr ${micro} + 1)
# launcher_check=$(shell dpkg --compare-versions "$$(apt-cache policy maemo-release | grep Installed | awk '{print $$2;}')" "lt" "5.0.0" && echo "maemo-launcher" || echo "nolauncher")

CFLAGS = -Wall -g

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

# Use soft-float and thumb mode if enabled. 
ifneq (,$(findstring thumb,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -mthumb
endif

ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
	INSTALL_PROGRAM += -s
endif

CONFIGURE_SWITCHES =     \
	--prefix="$(PREFIX)"   \
	--enable-maintainer-mode  \
	$(NULL)

# ifeq ($(launcher_check),maemo-launcher)
# 	ifneq (,$(findstring maemo-launcher,$(DEB_BUILD_OPTIONS)))
# 		CONFIGURE_SWITCHES += --enable-maemo-launcher
# 		USE_MAEMO_LAUNCHER = true
# 	endif
# endif
# ifeq ($(launcher_check),nolauncher)
# 	ifeq (,$(findstring nolauncher,$(DEB_BUILD_OPTIONS)))
# 		CONFIGURE_SWITCHES += --enable-maemo-launcher
# 		USE_MAEMO_LAUNCHER = true
# 	endif
# endif

config.status: configure
	CFLAGS="$(CFLAGS)" ./configure $(CONFIGURE_SWITCHES)

configure:
	echo -e '\033[7mtarget: configure\033[0m'
	dh_testdir

	SBOX_DEFAULT_AUTOMAKE="1.9" autoreconf -s -i && glib-gettextize -f && aclocal

build: build-stamp
	echo -e '\033[7mtarget: build\033[0m'

build-stamp: config.status
	echo -e '\033[7mtarget: build-stamp\033[0m'
	dh_testdir

	# Add here commands to compile the package.
	$(MAKE)

	touch build-stamp

clean:
	echo -e '\033[7mtarget: clean\033[0m'
	dh_testdir
	dh_testroot
	rm -f *-stamp 

	# Add here commands to clean up after the build process.
	dh_clean
	-$(MAKE) maintainer-clean

install: build install-stamp

install-stamp:
	echo -e '\033[7mtarget: install\033[0m'
	dh_testdir
	dh_testroot
	dh_clean -k 
	dh_installdirs

	echo -e '\033[7mMaking install\033[0m'
	# Add here commands to install the package into debian/tmp
	$(MAKE) install DESTDIR=$(CURDIR)/debian/tmp

	# install icon
	mkdir -p $(CURDIR)/debian/tmp/usr/share/icons/hicolor/32x32/hildon
	cp $(CURDIR)/debian/icon/pogacsa-32x32.png $(CURDIR)/debian/tmp/usr/share/icons/hicolor/32x32/hildon/pogacsa.png

	# install icon
	mkdir -p $(CURDIR)/debian/tmp/usr/share/icons/hicolor/48x48/hildon
	cp $(CURDIR)/debian/icon/pogacsa-48x48.png $(CURDIR)/debian/tmp/usr/share/icons/hicolor/48x48/hildon/pogacsa.png

	# install icon
	mkdir -p $(CURDIR)/debian/tmp/usr/share/icons/hicolor/128x128/hildon
	cp $(CURDIR)/debian/icon/pogacsa-128x128.png $(CURDIR)/debian/tmp/usr/share/icons/hicolor/128x128/hildon/pogacsa.png

	# Remove the dreaded .la files
	find $(CURDIR)/debian/tmp | grep '\.la$$' | xargs rm -f

	# Add osso-backup script
	mkdir -p $(CURDIR)/debian/tmp/etc/osso-backup/applications
	cp $(CURDIR)/debian/pogacsa.conf $(CURDIR)/debian/tmp/etc/osso-backup/applications

	#Install CUD/RFS script
	mkdir -p $(CURDIR)/debian/tmp/etc/osso-cud-scripts
	cp $(CURDIR)/debian/pogacsa-cud.sh $(CURDIR)/debian/tmp/etc/osso-cud-scripts
	mkdir -p $(CURDIR)/debian/tmp/etc/osso-rfs-scripts
	ln -s ../osso-cud-scripts/pogacsa-cud.sh $(CURDIR)/debian/tmp/etc/osso-rfs-scripts/pogacsa-rfs.sh

	touch install-stamp

# Build architecture-independent files here.
binary-indep: build install
	echo -e '\033[7mtarget: binary-indep\033[0m'
# We have nothing to do by default.

clean-debian:
	echo -e '\033[7mtarget: clean-debian\033[0m'
	rm -rf \
		`ls -ld debian/* | grep '^d' | grep -vE 'override-files|scripts|tmp|icon' | awk '{print $$$$NF;}'` \
		debian/{*.{debhelper,substvars},files,copyright}

# Build architecture-dependent files here.
binary-arch: build install clean-debian
	echo -e '\033[7mtarget: binary-arch\033[0m'

	dh_maemo_package_icons
	dh_testdir
	dh_testroot
#	dh_installchangelogs ChangeLog
	dh_installdocs
	dh_installexamples
	dh_install --sourcedir=debian/tmp --fail-missing -v
	dh_gconf
#	dh_installmenu
#	dh_installdebconf
#	dh_installlogrotate
#	dh_installemacsen
#	dh_installpam
#	dh_installmime
#	dh_installinit
#	dh_installcron
#	dh_installinfo
	dh_installman
	dh_link
#	if test "x$(USE_MAEMO_LAUNCHER)" = "xtrue"; then \
#	  dh_maemolauncher -v; \
#	fi
	for dbg in `cat $(CURDIR)/debian/control | grep '^Package:' | grep --regex='-dbg$$' | awk '{print $$2;}' | sed 's/-dbg$$//'`; do \
		echo dh_strip --dbg-package=$$dbg; \
		dh_strip --dbg-package=$$dbg; \
	done
	dh_compress
	dh_fixperms
#	dh_perl
#	dh_python
#	dh_makeshlibs -V "libpurple0 (>= $(major).0)"

	# some dh_*-generated pre- and post- -inst and -rm segments may pass args unknown to some busybox coreutils
	for script in `ls $(CURDIR)/debian/*.p{re,ost}{inst,rm}.* 2> /dev/null`; do \
		cat $$script | sed -r -e 's/-p --ignore-fail-on-non-empty (.*$$)/\1 || true/' > $$script.new && \
			mv $$script.new $$script; \
	done

	dh_installdeb
	dh_shlibdeps
	dh_gencontrol -- $(LAUNCHER_DEPENDS)
	dh_md5sums
	dh_builddeb
	dh_maemo_package_icons -r

binary: binary-indep binary-arch
	echo -e '\033[7mtarget: binary\033[0m'

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