#!/usr/bin/make -f

DEB_HOST_GNU_TYPE   ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
CFLAGS = -Wall -g

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

get-orig-source:
	set -e; \
		PKG=mtpfs; VER=0.9; \
		svn co http://mtpfs.googlecode.com/svn/trunk/ $$PKG; \
		REVISION=`svnversion $$PKG`; \
		find $$PKG -depth -type d -name ".svn" | xargs rm -rf; \
		tar cfz $${PKG}_$${VER}+svn$${REVISION}.orig.tar.gz $$PKG; \
		rm -rf $$PKG

build: build-stamp
build-stamp:
	dh_testdir
	cp -f /usr/share/misc/config.sub config.sub
	cp -f /usr/share/misc/config.guess config.guess
	./configure \
		--host=$(DEB_HOST_GNU_TYPE) \
		--build=$(DEB_BUILD_GNU_TYPE) \
		--prefix=/usr \
		--mandir=\$${prefix}/share/man \
		CFLAGS="$(CFLAGS)" LDFLAGS="-Wl,-z,defs,--as-needed"
	$(MAKE)
	touch $@

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp
	test -f Makefile && $(MAKE) distclean || true
	rm -f config.guess config.sub
	dh_clean

install: build
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs
	$(MAKE) install DESTDIR=$(CURDIR)/debian/mtpfs

binary-indep: build install

binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installdocs NEWS README
	dh_installchangelogs
	dh_installman debian/manpages/mtpfs.1
	dh_link usr/bin/mtpfs sbin/mount.mtpfs
	dh_link usr/share/man/man1/mtpfs.1 usr/share/man/man1/mount.mtpfs.1
	dh_strip
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

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