#!/usr/bin/make -f

PKG_NAME=fusecompress
PKG_DIR=$(PKG_NAME)-$(PKG_VERS)
ORIG_FILE=$(PKG_NAME)_$(PKG_VERS).orig.tar
PKG_VERS=$(shell dpkg-parsechangelog | sed -rne 's,^Version: ([^-]+).*,\1,p')

get-orig-source:
	[ ! -f $(ORIG_FILE).gz ] || rm -f $(ORIG_FILE).gz
	uscan --force-download --rename --repack --download-version=$(PKG_VERS) --destdir=.
	test -f $(ORIG_FILE).gz || exit 1
	mkdir $(PKG_NAME)__TEMP
	tar -xzf $(ORIG_FILE).gz -C $(PKG_NAME)__TEMP
	mv $(PKG_NAME)__TEMP/tex-fusecompress-* $(PKG_DIR)

	rm -f $(ORIG_FILE).gz
	rm -rf $(PKG_NAME)__TEMP
	
	tar -cf $(ORIG_FILE) $(PKG_DIR)
	gzip --best $(ORIG_FILE)
	rm -rf $(PKG_DIR)
	@echo "Re-packaged source file: $(ORIG_FILE).gz"
	@ls -l $(ORIG_FILE).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)
ifneq ($(DEB_HOST_GNU_TYPE),$(DEB_BUILD_GNU_TYPE))
CROSS= --build $(DEB_BUILD_GNU_TYPE) --host $(DEB_HOST_GNU_TYPE)
else
CROSS= --build $(DEB_BUILD_GNU_TYPE)
endif



config.status: configure
	dh_testdir
	# Add here commands to configure the package.
ifneq "$(wildcard /usr/share/misc/config.sub)" ""
	[ ! -f config.sub ] || mv -f config.sub config.sub.ups
	cp -f /usr/share/misc/config.sub config.sub
endif
ifneq "$(wildcard /usr/share/misc/config.guess)" ""
	[ ! -f config.guess ] || mv -f config.guess config.guess.ups
	cp -f /usr/share/misc/config.guess config.guess
endif
	./configure $(CROSS) --prefix=/usr --mandir=\$${prefix}/share/man --infodir=\$${prefix}/share/info CFLAGS="$(CFLAGS)" LDFLAGS="-Wl,-z,defs"


build: build-stamp
	$(MAKE) DESTDIR=$(CURDIR)/debian/fusecompress

build-stamp: config.status 
	dh_testdir

	touch $@

clean: 
	dh_testdir
	dh_testroot
	rm -f build-stamp 

	# Add here commands to clean up after the build process.
	[ ! -f Makefile ] || $(MAKE) distclean

	[ ! -f config.guess.ups ] || mv -f config.guess.ups config.guess
	[ ! -f config.sub.ups ] || mv -f config.sub.ups config.sub

	dh_clean build-stamp
	rm -f config.status config.log

install: build
	dh_testdir
	dh_testroot
	dh_prep  
	dh_installdirs

	# Add here commands to install the package into debian/fusecompress.
	$(MAKE) DESTDIR=$(CURDIR)/debian/fusecompress install

	rm -f $(CURDIR)/debian/fusecompress/usr/bin/print_compress


# Build architecture-independent files here.
binary-indep: install
# We have nothing to do by default.

# Build architecture-dependent files here.
binary-arch: install
	dh_testdir
	dh_testroot
	dh_link /usr/share/man/man1/fusecompress.1 \
		/usr/share/man/man8/mount.fusecompress.8
	dh_installchangelogs debian/changelog
	dh_installdocs
	dh_installman
	dh_strip --dbg-package=fusecompress-dbg
	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 get-orig-source
