#!/usr/bin/make -f
# -*- makefile -*-
#
#  # Uncomment this to turn on verbose mode.
export DH_VERBOSE=1

# 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)

ruby_ver = 1.9.1

name_prefix = pe-
pe_prefix = opt/puppet

arch_name = $(subst linux-gnu,linux-,$(patsubst %linux-gnu,%linux,$(DEB_BUILD_GNU_TYPE)))
lib_dir = $(pe_prefix)/lib
ruby_libdir = $(lib_dir)/ruby/$(ruby_ver)
ruby_archdir = $(lib_dir)/ruby/$(ruby_ver)/$(arch_name)
bin_dir = $(pe_prefix)/bin
man_dir = $(pe_prefix)/share/man/man1
include_dir = $(pe_prefix)/include

include /usr/share/cdbs/1/rules/debhelper.mk
#include /usr/share/cdbs/1/class/makefile.mk
#include /usr/share/cdbs/1/class/autotools.mk
#include /usr/share/cdbs/1/rules/patchsys-quilt.mk

DEB_AUTO_UPDATE_AUTOCONF = YES

CFLAGS := -fno-strict-aliasing -g -I/$(include_dir)
CXXFLAGS := -fno-strict-aliasing -g
LDFLAGS+= -Wl,-rpath,/$(lib_dir)

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


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

build:
	autoconf
	./configure --host=$(DEB_HOST_GNU_TYPE) \
							--build=$(DEB_BUILD_GNU_TYPE) \
							--without-X11 \
							--without-tcl \
							--without-tk \
							--without-win32ole \
							--without-fiddle \
							--prefix=/$(pe_prefix) \
							--enable-rpath \
							--enable-shared \
							CFLAGS="$(CFLAGS)" \
							LDFLAGS="$(LDFLAGS)"
	make -j4 COPY="cp -p" Q=

binary-install/pe-ruby:: build
	mkdir -p $(CURDIR)/debian/$(cdbs_curpkg)
	make install DESTDIR=$(CURDIR)/debian/$(cdbs_curpkg)

# Build architecture-dependent files here.
# binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installchangelogs NEWS
	dh_installdocs
	dh_installexamples
	dh_install --sourcedir=debian/tmp
	dh_installman
	dh_lintian
	dh_link
#chrpath -c -r /opt/puppet/lib $(CURDIR)/debian/pe-augeas-tools/opt/puppet/bin/augtool
	dh_compress
	dh_fixperms
	dh_makeshlibs -V
	dh_installdeb
	dh_shlibdeps -l $(CURDIR)/debian/pe-ruby/$(lib_dir)
	dh_gencontrol
	dh_md5sums
	dh_builddeb

install: build
	dh_testdir
	dh_testroot
	dh_clean -k \
    --exclude=./tests/root/etc/group~ \
    --exclude=./tests/root/etc/multipath.conf~ \
    --exclude=./tests/root/etc/passwd~
	dh_installdirs
	make DESTDIR=$(CURDIR)/debian/tmp install

binary-post-install::
	sh $(CURDIR)/debian/fixshebang.sh ruby$(ruby_ver) \
		'$(CURDIR)/debian/$(cdbs_curpkg)/$(bin_dir)'
	sh $(CURDIR)/debian/rmshebang.sh \
		'$(CURDIR)/debian/$(cdbs_curpkg)/$(ruby_libdir)'

clean::
	dh_testdir
	dh_testroot
	rm -f build-stamp
	# Add here commands to clean up after the build process.
	[ ! -f Makefile ] || $(MAKE) distclean
	rm -f config.log
	dh_clean
	rm -rf $(CURDIR)/debian/{pe-ruby,pe-ruby.debhelper.log,pe-ruby.substvars}
	make clean || true
	make distclean || true

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