#!/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

VERSION=$(shell dpkg-parsechangelog | grep-dctrl "" -s Version -n)

DEB_HOST_ARCH ?= $(shell dpkg-architecture -qDEB_HOST_ARCH)
CFLAGS := -O2 -W -Wall -g

ifeq ($(DEB_HOST_ARCH),amd64)
FOREIGN_ARCH := i386
PKGPREFIX    := ia32-
else
ifeq ($(DEB_HOST_ARCH),ia64)
FOREIGN_ARCH := i386
PKGPREFIX    := ia32-
else
ifeq ($(DEB_HOST_ARCH),i386)
FOREIGN_ARCH := amd64
PKGPREFIX    := amd64-
else
error $(DEB_HOST_ARCH) unknown
endif
endif
endif


build: ia32-libs-tools/mangle ia32-apt-get/apt-get
build: ia32-apt-get/dpkg-deb debian/ia32-apt-get.postinst

ia32-libs-tools/mangle:
	g++ $(CFLAGS) -DARCH='"'$(DEB_HOST_ARCH)'"' -DPKGPREFIX='"'$(PKGPREFIX)'"' -DVERSION='"'~$(VERSION)'"' -o ia32-libs-tools/mangle ia32-libs-tools/mangle.cc

ia32-apt-get/apt-get: 
	sed < ia32-apt-get/apt-get.in "s/@NATIVE_ARCH@/$(DEB_HOST_ARCH)/g" | sed "s/@FOREIGN_ARCH@/$(FOREIGN_ARCH)/g" > ia32-apt-get/apt-get

ia32-apt-get/dpkg-deb:
	sed < ia32-apt-get/dpkg-deb.in "s/@NATIVE_ARCH@/$(DEB_HOST_ARCH)/g" | sed "s/@FOREIGN_ARCH@/$(FOREIGN_ARCH)/g" > ia32-apt-get/dpkg-deb

debian/ia32-apt-get.postinst:
	sed < debian/ia32-apt-get.postinst.in "s/@FOREIGN_ARCH@/$(FOREIGN_ARCH)/g" > debian/ia32-apt-get.postinst

clean:
	dh_testdir
	dh_testroot
	dh_clean
	rm -rf ia32-libs-tools/mangle
	rm -rf ia32-apt-get/apt-get ia32-apt-get/dpkg-deb
	rm -rf debian/ia32-apt-get.postinst

install: build
	dh_testdir
	dh_testroot
	dh_clean -k 
	dh_installdirs
	cp -a ia32-libs-tools/hooks debian/ia32-libs-tools/usr/lib/ia32-libs-tools/
	rm -rf debian/ia32-libs-tools/usr/lib/ia32-libs-tools/hooks/.svn
	sed "s/@VERSION@/$(VERSION)/g" < ia32-libs-tools/create > debian/ia32-libs-tools/usr/lib/ia32-libs-tools/create
	sed "s/@VERSION@/$(VERSION)/g" < ia32-libs-tools/convert > debian/ia32-libs-tools/usr/lib/ia32-libs-tools/convert
	chmod a+x debian/ia32-libs-tools/usr/lib/ia32-libs-tools/create debian/ia32-libs-tools/usr/lib/ia32-libs-tools/convert

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

# Build architecture-dependent files here.
binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installchangelogs 
	dh_installdocs
	dh_installexamples
	dh_install
	dh_installman
	dh_link
	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 configure
