#!/usr/bin/make -f

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

SRC_VERSION = $(shell dpkg-parsechangelog | sed -ne 's/^Version: \(\([0-9]\+\):\)\?\(.*\)-.*/\3/p')
ORIG_VERSION = $(shell echo $(SRC_VERSION) | sed -ne 's/+ds1//p')
ORIGTAR = swftools_$(ORIG_VERSION).orig.tar.gz
ORIGDIR = swftools_$(ORIG_VERSION).orig
DEBTAR = swftools_$(SRC_VERSION).orig.tar.gz

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

# --enable-warnings sets CFLAGS to
# "-Wall -Wno-unused -Wno-format -Wno-redundant-decls"
# --enable-debug sets CFLAGS to "-O2 -g"
# in the configure script
#ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
#	CFLAGS = -g
#	CFGFLAGS = --enable-warnings
#else
#	CFGFLAGS = --enable-warnings --enable-debug
#endif

%:
	dh $@

clean:
	dh_testdir
	dh_testroot
	[ ! -f Makefile ] || $(MAKE) distclean
	rm -f core
	rm -f python*-stamp
	rm -f aclocal.m4 config.h config.log lib/action/Makefile swfs/Makefile lib/pdf/Makefile lib/swf/Makefile
	rm -f _pypath.py _pypath.txt
	rm -rf lib/python/usr
#	[ ! -f configure.dist ] || mv configure.dist configure
	dh_autotools-dev_restoreconfig
	dh_clean

override_dh_auto_configure:
	dh_autotools-dev_updateconfig
#	mv configure configure.dist
	aclocal -I m4
	autoconf
	LDFLAGS="-Wl,-z,defs" ./configure --enable-debug --host=$(DEB_HOST_GNU_TYPE) --build=$(DEB_BUILD_GNU_TYPE) --prefix=/usr --mandir=\$${prefix}/share/man --infodir=\$${prefix}/share/info --disable-poppler $(CFGFLAGS)

#override_dh_auto_build:
#	# Add libart stuff into the Makefile.common
#	cp Makefile.common Makefile.common.bak
#	echo "C += $(LIBART_CFLAGS)" >>Makefile.common
#	echo "CC += $(LIBART_CFLAGS)" >>Makefile.common
#	echo "LIBS += $(LIBART_LIBS)" >>Makefile.common
#	$(MAKE)
#	$(MAKE) -C lib

#	mv Makefile.common.bak Makefile.common

#	$(MAKE) -C src
#	$(MAKE) -C swfs

#	cp Makefile.common Makefile.common.bak
#	$(foreach pp, $(python_versions), $(call build_python,$(pp),$(pp)/$(call py_sitename, $(pp))))
#	mv Makefile.common.bak Makefile.common

override_dh_auto_install:
	dh_auto_install -- prefix=$(CURDIR)/debian/tmp/usr
	mkdir -p debian/tmp/usr/lib
	mkdir -p debian/tmp/usr/include/rfxswf
	/usr/bin/install -m 644 lib/librfxswf.a debian/tmp/usr/lib/
	/usr/bin/install -m 644 config.h lib/rfxswf.h lib/bitio.h lib/drawer.h lib/mem.h lib/types.h debian/tmp/usr/include/rfxswf/
	sed -i s/"\(#include \"\.\)\.\(\/config\.h\"\)"/"\1\2"/ debian/tmp/usr/include/rfxswf/rfxswf.h
#	cp -r lib/python/usr debian/tmp/

get-orig-source:
	uscan --no-conf --force-download --rename --download-current-version --destdir=.
	mkdir -p _unpack
	tar -xzf $(ORIGTAR) -C _unpack
	find _unpack  -maxdepth 1 -name "swftools*" -type d -exec mv \{} _unpack/swftools_$(ORIG_VERSION).orig \;
	rm -rf _unpack/$(ORIGDIR)/lib/pdf/xpdf
	rm -rf _unpack/$(ORIGDIR)/avi2swf
	tar -czf $(DEBTAR) -C _unpack $(ORIGDIR)
	rm -rf _unpack
