#!/usr/bin/make -f
# debian/rules file that uses debhelper. GNU copyright 1997 by Joey Hess.
# Modified for aumix by Tom Lees, 1998 and Bas Zoetekouw, 2002, 2003

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

# This is the debhelper compatibility version to use.
export DH_COMPAT=4

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

# this package
me=aumix-2.8

# directories
topdir := $(shell echo `pwd`)
debdir := $(topdir)/debian
patchdir := $(topdir)/patches
builddir := $(topdir)/build
sourcedir := $(builddir)/$(me)
stampdir := $(topdir)/stamp

# handle DEB_BUILD_OPTIONS=debug
#CFLAGS += -Wall -Wshadow -g
CFLAGS += -Wall -g
ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
  CFLAGS += -O0
else
  CFLAGS += -O2
endif
export CFLAGS

config_flags := --host=$(DEB_HOST_GNU_TYPE) \
                --build=$(DEB_BUILD_GNU_TYPE) \
                --prefix=/usr \
                --mandir=\$${prefix}/share/man \
                --without-alsa \
                --without-gtk1


# all new/updated translations we have
NEWPOOS:=$(sort $(shell cd $(patchdir) && find -maxdepth 1 -name \*.po))

# all patches
PATCHES:=$(sort $(shell cd $(patchdir) && find -maxdepth 1 -name \*.patch))



# unpack upstream source
unpack: $(stampdir)/unpack
$(stampdir)/unpack:
	dh_testdir
	
	# make builddir
	[ -d "$(builddir)" ] || mkdir $(builddir)
	# make stampdir
	[ -d "$(stampdir)" ] || mkdir $(stampdir)
	
	# this will unpack to $(builddir)/$(me)
	bzip2 -dc $(topdir)/$(me).tar.bz2 | tar xC $(builddir)
	
	touch $(stampdir)/unpack


# apply patches in debian/diffs to source
patch: $(stampdir)/patch
$(stampdir)/patch: $(stampdir)/unpack
	dh_testdir

	# install new/updated *.po files
	@cd $(sourcedir) && \
	for PO in $(NEWPOOS); do \
	  echo "# Installing new $$PO"; \
	  cp $(patchdir)/$$PO po/; \
	done

	# apply patches
	@cd $(sourcedir) && \
	for PATCH in $(PATCHES); do \
	  echo "# Applying patch $$PATCH"; \
	  patch -s -p1 < $(patchdir)/$$PATCH; \
	done

	# run aclocal, autoconf, automake
	@cd $(sourcedir) && \
	aclocal-1.7 && \
	autoconf && \
	automake-1.7

	touch $(stampdir)/patch


# Configure, build and install the GTK aumix binary in $(builddir)/gtk
build-gtk: $(stampdir)/build-gtk
$(stampdir)/build-gtk: $(builddir)/gtk/config.status
	dh_testdir
	$(MAKE) -C $(builddir)/gtk
	touch $(stampdir)/build-gtk


config-gtk: $(builddir)/gtk/config.status
$(builddir)/gtk/config.status: $(stampdir)/patch
	dh_testdir

	[ -d "$(builddir)/gtk" ] || mkdir -p -m 0755 $(builddir)/gtk
	cd $(builddir)/gtk && \
	$(sourcedir)/configure $(config_flags)

install-gtk: $(stampdir)/install-gtk
$(stampdir)/install-gtk: $(stampdir)/build-gtk
	dh_testdir
	dh_testroot
	#dh_clean -k -paumix-gtk
	dh_installdirs -paumix-gtk

	# install the main programs and files
	cd $(builddir)/gtk && \
	$(MAKE) install DESTDIR=$(debdir)/aumix-gtk/ \
		        pkgdatadir=\$${datadir}/aumix-gtk/
	# move the icon to the right directory
	mv debian/aumix-gtk/usr/share/aumix-gtk/aumix.xpm \
	   debian/aumix-gtk/usr/share/pixmaps/icon_aumix.xpm
	# generate the right README.Debian file
	sh ./debian/make-README.sh aumix-gtk > debian/aumix-gtk.README.Debian
	cp debian/default debian/aumix-gtk.default
	cp debian/docs debian/aumix-gtk.docs
	# we provide our own version of xaumix
	rm debian/aumix-gtk/usr/bin/xaumix
	install -m 0755 debian/xaumix debian/aumix-gtk/usr/bin/
	# install the docs, menu, init.d
	dh_installdocs -paumix-gtk 
	dh_installman -paumix-gtk $(debdir)/mute.1
	dh_installchangelogs -paumix-gtk $(sourcedir)/ChangeLog
	dh_installmenu -paumix-gtk
	## handle installinit manually because of complications of sharing the 
    ## /etc/init.d/aumix config file between aumix and aumix-gtk.  See postrm.
	##dh_installinit -paumix-gtk --no-restart-on-upgrade --no-start \
	##   --init-script=aumix -u "start 21 2 3 4 5 . stop 19 0 1 6 ."
	install -m 0755 $(debdir)/init $(debdir)/aumix-gtk/etc/init.d/aumix
	# create lock directory
	mkdir -m 0755 -p debian/aumix-gtk/var/lock/aumix
	# strip binaries, if needed
	dh_strip -paumix-gtk
	# misc policy stuff
	dh_compress -paumix-gtk
	dh_fixperms -paumix-gtk

	touch $(stampdir)/install-gtk



# Configure and build the non-GTK aumix binary in build/nogtk
build-nogtk: $(stampdir)/build-nogtk
$(stampdir)/build-nogtk: $(builddir)/nogtk/config.status
	dh_testdir
	$(MAKE) -C build/nogtk
	touch $(stampdir)/build-nogtk

config-nogtk: $(builddir)/nogtk/config.status
$(builddir)/nogtk/config.status: $(stampdir)/patch
	dh_testdir

	mkdir -p -m 0755 ./build/nogtk
	cd build/nogtk && \
	$(sourcedir)/configure $(config_flags) --without-gtk

install-nogtk: $(stampdir)/install-nogtk
$(stampdir)/install-nogtk: $(stampdir)/build-nogtk
	dh_testdir
	dh_testroot
	#dh_clean -k -paumix
	dh_installdirs -paumix

	# install the main programs and files
	cd $(builddir)/nogtk && \
	$(MAKE) install DESTDIR=$(debdir)/aumix/ \
		        pkgdatadir=\$${datadir}/aumix/
	# move the icon to the right directory
	mv debian/aumix/usr/share/aumix/aumix.xpm \
	   debian/aumix/usr/share/pixmaps/icon_aumix.xpm
	# we provide our own version of xaumix
	rm debian/aumix/usr/bin/xaumix
	install -m 0755 debian/xaumix debian/aumix/usr/bin/
	# generate README.Debian file for aumix
	sh ./debian/make-README.sh aumix > debian/aumix.README.Debian
	cp debian/default debian/aumix.default
	cp debian/docs debian/aumix.docs
	# install the docs, menu, init.d
	dh_installdocs -paumix
	dh_installman -paumix $(debdir)/mute.1
	dh_installchangelogs -paumix $(sourcedir)/ChangeLog
	dh_installmenu -paumix
	## handle installinit manually because of complications of sharing the 
    ## /etc/init.d/aumix config file between aumix and aumix-gtk.  See postrm.
	##dh_installinit -paumix --no-restart-on-upgrade --no-start \
	##   --init-script=aumix -u "start 21 2 3 4 5 . stop 19 0 1 6 ."
	install -m 0755 $(debdir)/init $(debdir)/aumix/etc/init.d/aumix
	# create lock directory
	mkdir -m 0755 -p debian/aumix/var/lock/aumix
	# strip binaries, if needed
	dh_strip -paumix
	# misc policy stuff
	dh_compress -paumix
	dh_fixperms -paumix

	touch $(stampdir)/install-nogtk


config: config-gtk config-nogtk
build: build-gtk build-nogtk
install: install-gtk install-nogtk


clean:
	dh_testdir
	dh_testroot
	-$(MAKE) distclean
	rm -rf $(builddir) $(stampdir)

	# these are dynamically generated 
	rm -f debian/aumix{,-gtk}.README.[dD]ebian \
	      debian/aumix{,-gtk}.default \
	      debian/aumix{,-gtk}.docs

	dh_clean

# there are no architecture independent packages to be made
binary-indep: 

# build both binary packages
binary-arch: build install
	dh_testdir -a
	dh_testroot -a
	dh_shlibdeps -a
	dh_installdeb -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

binary: binary-indep binary-arch
.PHONY: build build-gtk build-nogtk clean binary-gtk binary-nogtk \
        binary install install-gtk install-nogtk patch \
	config-gtk config-nogtk config
