#!/usr/bin/make -f
# -*- makefile -*-

#export DH_VERBOSE=1

DEB_HOST_ARCH_CPU ?= $(shell dpkg-architecture -qDEB_HOST_ARCH_CPU)

full_version	:= $(shell dpkg-parsechangelog | grep ^Version: | sed -e 's/^.*: //g')
upstream_version	:= $(shell echo $(full_version) | sed -e 's/~alpha/-ALPHA/' -e 's/~beta/-BETA/' -e 's/~rc/-RC/' -e 's/-[^-]*$$//g' -e 's/^[^-]*$$/&-RELEASE/')
tar_version	:= $(shell echo $(full_version) | sed -e 's/-.*//g')
version		:= $(shell echo $(full_version) | sed -e 's/[~+-].*//g')
major           := $(shell echo $(version) | sed -e 's/\..*//g')

ORIG_DIR	:= kfreebsd-downloader-$(major)-$(tar_version)
ORIG_TAR	:= kfreebsd-downloader-$(major)_$(tar_version).orig.tar.gz

get-orig-source:
	rm -rf $(ORIG_DIR)
	mkdir $(ORIG_DIR)
	for arch in i386 amd64 ; do \
		wget http://ftp.freebsd.org/pub/FreeBSD/releases/$$arch/$$arch/$(upstream_version)/MANIFEST \
			-O $(ORIG_DIR)/MANIFEST.$$arch ; \
	done
	tar --numeric-owner --owner 0 --group 0 -czf ../$(ORIG_TAR) $(ORIG_DIR)
	rm -rf $(ORIG_DIR)

fetch:

%:
	dh $@

debian/%: debian/%.in debian/rules
	sed \
		-e "s/@MAJOR@/$(major)/g" \
		-e "s/@VERSION@/$(version)/g" \
		-e "s/@UPSTREAM_VERSION@/$(upstream_version)/g" \
		-e "s/@ARCH@/$(DEB_HOST_ARCH_CPU)/g" \
	< $< > $@

override_dh_auto_build: debian/postinst debian/postrm debian/install

override_dh_auto_clean:
	rm -f debian/postinst debian/postrm debian/install
