#!/usr/bin/make -f
# -*- makefile -*-
# Sample debian/rules that uses debhelper.
# This file was originally written by Joey Hess and Craig Small.
# As a special exception, when this file is copied by dh-make into a
# dh-make output file, you may use that output file without restriction.
# This special exception was added by Craig Small in version 0.37 of dh-make.

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

# for architecture dependent variables and changelog vars
vafilt = $(subst $(2)=,,$(filter $(2)=%,$(1)))

DPKG_VARS               := $(shell dpkg-architecture)
DEB_BUILD_GNU_TYPE	?= $(call vafilt,$(DPKG_VARS),DEB_BUILD_GNU_TYPE)
DEB_HOST_GNU_TYPE	?= $(call vafilt,$(DPKG_VARS),DEB_HOST_GNU_TYPE)

CFLAGS ?= $(shell dpkg-buildflags --get CFLAGS)
ifneq (,$(filter noopt,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -Wall
endif

ifeq ($(DEB_HOST_GNU_TYPE),$(DEB_BUILD_GNU_TYPE))
CC = gcc
else
CC = $(DEB_HOST_GNU_TYPE)-gcc
endif

%:
	dh $@  --with autoreconf

override_dh_auto_clean:
	if [ -e Makefile ]; then $(MAKE) testclean; fi
	dh_auto_clean
	rm -f simd/jsimdcfg.inc

deb_source := $(shell dpkg-parsechangelog | sed -n 's/^Source: //p')
svn_rev := $(shell dpkg-parsechangelog | sed -rne 's,^Version: .*[+~]svn([0-9]+).*,\1,p')
upstream_version := $(shell dpkg-parsechangelog | sed -rne 's,^Version: ([^-]+).*,\1,p')

.PHONY: get-svn-source
get-svn-source:
	rm -rf $(deb_source)-$(upstream_version).orig
	rm -f $(deb_source)-$(upstream_version).orig.tar.gz
	svn -q export -r $(svn_rev) https://libjpeg-turbo.svn.sourceforge.net/svnroot/libjpeg-turbo/trunk \
		$(deb_source)-$(upstream_version).orig
	GZIP=--best tar -cz --owner root --group root --mode a+rX \
	     -f $(deb_source)_$(upstream_version).orig.tar.gz \
	     $(deb_source)-$(upstream_version).orig
	rm -r $(deb_source)-$(upstream_version).orig

.PHONY: get-orig-source
get-orig-source:
	wget --trust-server-name http://sourceforge.net/projects/$(deb_source)/files/$(upstream_version)/$(deb_source)-$(upstream_version).tar.gz/download
	mv $(deb_source)-$(upstream_version).tar.gz $(upstream_version)/$(deb_source)_$(upstream_version).orig.tar.gz

