#!/usr/bin/make -f
# Sample debian/rules that uses debhelper.
# GNU copyright 1997 to 1999 by Joey Hess.

version = 2.2
patchlevel = $(version).10
date = 20090624

DDIR = $(shell pwd)/debian/tmp
PDIR = usr/lib/tcltk/$(DEB_HOST_MULTIARCH)/snack$(version)

DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/buildflags.mk
include /usr/share/dpkg/architecture.mk

TCLVERSIONS = 8.6 9.0

CONFIGOPTS = --enable-gcc \
	     --enable-shared \
	     --build=$(DEB_BUILD_GNU_TYPE) \
	     --host=$(DEB_HOST_GNU_TYPE) \
	     --prefix=/usr \
	     --with-ogg-include=/usr/include \
	     --with-ogg-lib=/usr/lib

CONFIGOPTS_ALSA = $(CONFIGOPTS) --enable-alsa

BUILD_ALSA := $(shell if [ `uname -s` = Linux ] ; then echo yes ; else echo no ; fi)

ifeq ($(BUILD_ALSA), no)
FLAVORS = oss
else
FLAVORS = oss alsa
endif

%:
	dh $@

override_dh_auto_clean:
	set -e ; \
	for v in $(TCLVERSIONS) ; do \
		rm -rf $(CURDIR)/debian/$$v ; \
	done
	rm -f debian/tcl-snack-dev.links
	rm -f debian/tcl-snack-dev.install
	rm -f debian/tcl-snack.install
	set -e ; \
	for d in $(FLAVORS) ; do \
		rm -f debian/libsnack-$$d.install ; \
	done
	rm -f pkgIndex.tcl
	rm -f unix/tcl.m4
	rm -rf python/build
	dh_auto_clean

override_dh_autoreconf-arch:
	ln -sf ../win/tcl.m4 $(CURDIR)/unix
	dh_autoreconf -D$(CURDIR)/unix

override_dh_auto_configure-arch:
	set -e ; \
	for v in $(TCLVERSIONS) ; do \
		for d in $(FLAVORS) ; do \
			if [ "$$d" = "alsa" ] ; then \
				configopts="$(CONFIGOPTS_ALSA)" ; \
			else \
				configopts="$(CONFIGOPTS)" ; \
			fi ; \
			dh_auto_configure -B$(CURDIR)/debian/$$v/$$d -D$(CURDIR)/unix -- \
				$$configopts \
				--with-tcl=/usr/lib/tcl$$v \
				--with-tk=/usr/lib/tk$$v ; \
		done ; \
	done

override_dh_auto_build-arch:
	set -e ; \
	for v in $(TCLVERSIONS) ; do \
		for d in $(FLAVORS) ; do \
			DEB_CFLAGS="$(CFLAGS)" \
			$(MAKE) -C$(CURDIR)/debian/$$v/$$d ; \
		done ; \
	done

override_dh_auto_install-arch:
	tclsh8.6 debian/prepareInstall.tcl --package $(version) \
					   --versions $(TCLVERSIONS) \
					   --flavors $(FLAVORS) \
					   --multiarch $(DEB_HOST_MULTIARCH)

override_dh_auto_install-indep:
	dh_testdir
	dh_testroot
	dh_prep
	mkdir -p $(DDIR)/tcl
	set -e ; \
	for f in demos/tcl/* ; do \
		sed -e 's/\(wish\|tclsh\)8\.5/\18.6/' $$f >$(DDIR)/tcl/$$(basename $$f) ; \
	done
	cd python ; \
	python3 setup.py install \
		--no-compile \
		--install-layout=deb \
		--root=$(DDIR)

override_dh_fixperms-arch:
	dh_fixperms
	chmod a-x debian/tcl-snack/usr/lib/tcltk/*/snack*/*.tcl

override_dh_fixperms-indep:
	dh_fixperms
	chmod a-x debian/tcl-snack-doc/usr/share/doc/tcl-snack-doc/examples/tcl/*
	chmod a+x debian/tcl-snack-doc/usr/share/doc/tcl-snack-doc/examples/tcl/*.tcl
	chmod a-x debian/tcl-snack-doc/usr/share/doc/tcl-snack-doc/examples/python/*
	chmod a+x debian/tcl-snack-doc/usr/share/doc/tcl-snack-doc/examples/python/*.py
	chmod a-x debian/tcl-snack-doc/usr/share/doc/tcl-snack-doc/examples/python/tkSnack.py
	dh_python3 -ppython3-tksnack

get-orig-source:
	set -e ; \
	CURDIR=`pwd` && \
	TMPDIR=`mktemp -d /tmp/snack.XXXXXX` && \
	cd $$TMPDIR && \
	rsync -r a.cvs.sourceforge.net::cvsroot/wize/wize2/wize/snack$(patchlevel) ./ && \
	cd snack$(patchlevel) && \
	for f in $$(find . -name '*,v') ; do \
	    (cd $$(dirname $$f) ; co $$(basename $$f) ; rm $$(basename $$f)) ; \
	done && \
	for f in $$(find . -name tclkit-linux-x86 -o -name sdx -o -name snack.mcp.sit.hqx -o -name right_back.shape) ; do \
	    echo "REMOVING BINARY WITHOUT A SOURCE $$f" ; rm -f $$f ; \
	done && \
	rm -vf win/Makefile win/config.log win/config.status win/snackConfig.sh && \
	rm -vf unix/Makefile unix/config.log unix/config.status unix/snackConfig.sh && \
	rm -vf generic/SnackMp3.c generic/jkFormatMP3.c generic/jkFormatMP3.h && \
	rm -vf pkgIndex.tcl unix/pkgIndex.tcl && \
	sed -i -e '607,775d' generic/jkFormant.c && \
	cd .. && \
	tar -Jcf $$CURDIR/snack_$(patchlevel).$(date)+dfsg.orig.tar.xz snack$(patchlevel) && \
	rm -rf $$TMPDIR

.PHONY: override_dh_autoreconf-arch override_dh_auto_configure-arch override_dh_auto_build-arch \
	override_dh_auto_install-arch override_dh_auto_install-indep override_dh_auto_clean \
	override_dh_fixperms-indep get-orig-source
