#!/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
#export DEB_BUILD_MAINT_OPTIONS=hardening=+all
DPKG_EXPORT_BUILDFLAGS=1
include /usr/share/dpkg/buildflags.mk

# 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)
DEB_HOST_ARCH_OS    ?= $(shell dpkg-architecture -qDEB_HOST_ARCH_OS)


# The following fails on ia64, because of alignment stuff
# CFLAGS = -Wall -g
CFLAGS = -g

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

ifeq ($(DEB_HOST_ARCH_OS), linux)
CONFFLAGS += --enable-watchdog
endif

configure: configure-stamp
configure-stamp:
	dh_testdir
	mkdir -p m4
	dh_autoreconf
	# Add here commands to configure the package.
	# The following Debian defaults don't work...need to figure out what's
	# wrong:
	# LDFLAGS="-Wl,-z,defs"
	chmod +x ltmain.sh configure
	./configure --host=$(DEB_HOST_GNU_TYPE) --build=$(DEB_BUILD_GNU_TYPE) --prefix=/usr --sysconfdir=/etc --with-varpath=/var/lib/openhpi --mandir=\$${prefix}/share/man --infodir=\$${prefix}/share/info --enable-daemon --enable-clients --enable-ipmi --enable-cpp_wrappers --enable-oa_soap --enable-ilo2_ribcl --enable-snmp_bc --enable-ipmidirect --enable-simulator --enable-dynamic_simulator $(CONFFLAGS) CFLAGS="$(CFLAGS)"
	touch configure-stamp


build: build-stamp

build-arch: build-arch-stamp
build-arch-stamp: configure-stamp

build-indep: build-indep-stamp
build-indep-stamp: configure-stamp

build-stamp:  configure-stamp
	dh_testdir

	# Add here commands to compile the package.
	$(MAKE)

	touch $@

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp configure-stamp

	# Add here commands to clean up after the build process.
	# chmod -x ltmain.sh configure
	[ ! -f Makefile ] || $(MAKE) clean
	find . -name Makefile -exec rm -f "{}" ";"
	find . -name Makefile -exec rm -f "{}" ";"
	find . -depth -type d -name \$\(top_srcdir\) -exec rm -rf "{}" ";"
	find . -depth -type d -name \.deps -exec rm -rf "{}" ";"
	find . -depth -type d -name \$\(top_srcdir\) -exec rm -rf "{}" ";"
	# Should we remove the baselibs/csharp from the distro?

	rm -f config.log config.guess libtool config.status config.h rt-env.sh stamp-h1
	rm -f openhpi.spec openhpi.pc openhpid/openhpid.sh
	rm -f utils/openhpiutils.pc clients/.dirstamp
	dh_autoreconf_clean
	dh_clean 

install: build
	dh_testdir
	dh_testroot
	dh_prep 
	dh_installdirs

	# Add here commands to install the package into debian/openhpi.
	# Installing into debian/tmp, then using dh_install and the
	# package.install file to break up the files into packages.
	$(MAKE) install DESTDIR=$(CURDIR)/debian/tmp

	# openhpi is an empty package, but an empty .install file can't be
	# represented in a diff
	touch debian/openhpi.install


# Build architecture-independent files here.
binary-indep: build install
	dh_testdir
	dh_testroot
	dh_installchangelogs -i
	dh_installdocs -i --all debian/README.Debian
	dh_installexamples -i
#	dh_install --sourcedir=debian/tmp -i
#	dh_installmenu -i
#	dh_installdebconf -i	
#	dh_installlogrotate -i
#	dh_installemacsen -i
#	dh_installpam -i
#	dh_installmime -i
#	dh_python -i
#	dh_installinit -i
#	dh_installcron -i
#	dh_installinfo -i
#	dh_installwm -i
#	dh_installudev -i
	dh_lintian -i
#	dh_undocumented -i
	dh_installman -i
	dh_link -i
	dh_compress -i
	dh_fixperms -i
#	dh_perl -i
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

# Build architecture-dependent files here.
binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installchangelogs -a
	dh_installdocs -a --all debian/README.Debian
	dh_installexamples -a
	# Using the upstream init script...better way to do this?
	rm -f debian/openhpid.init
	mv debian/tmp/etc/init.d/openhpid debian/openhpid.init
	# For now, not shipping the .la files because they are a support
	# liability as far as version dependencies go
	rm -f debian/tmp/usr/lib/*.la
	rm -f debian/tmp/usr/lib/openhpi/*.la
	# The following are for specific documentation files that upstream
	# puts in a nonstandard place:
	mv debian/tmp/usr/share/doc/openhpi*/README debian/libopenhpi3/usr/share/doc/libopenhpi3/
	mv debian/tmp/usr/share/doc/openhpi*/README.daemon debian/libopenhpi3/usr/share/doc/libopenhpi3/
	# Our debian/copyright file includes the contents of this one
	rm debian/tmp/usr/share/doc/openhpi*/COPYING
	# Upstream doesn't "install" these files anywhere and they're useful:
	cp plugins/ilo2_ribcl/OpenHPI_ProLiant_DevGuide.pdf debian/openhpi-plugin-ilo2-ribcl/usr/share/doc/openhpi-plugin-ilo2-ribcl/
	cp plugins/oa_soap/OpenHPI_Blade_DevGuide.pdf debian/openhpi-plugin-oa-soap/usr/share/doc/openhpi-plugin-oa-soap/
	cp plugins/snmp_bc/bc2hpi.pdf debian/openhpi-plugin-snmp-bc/usr/share/doc/openhpi-plugin-snmp-bc/
	dh_install --sourcedir=debian/tmp --fail-missing -a
#	dh_installmenu -a
#	dh_installdebconf -a	
#	dh_installlogrotate -a
#	dh_installemacsen -a
#	dh_installpam -a
#	dh_installmime -a
#	dh_python -a
	dh_installinit -a
#	dh_installcron -a
#	dh_installinfo -a
#	dh_installwm -a
#	dh_installudev -a
	dh_lintian -a
#	dh_undocumented -a
	dh_installman -a
	dh_link -a
	dh_strip -a
	dh_compress -a
	dh_fixperms -a
#	dh_perl -a
	dh_makeshlibs -a -X/usr/lib/openhpi	# Could add -V here
	dh_installdeb -a
	dh_shlibdeps -a -- --warnings=1
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a
binary: binary-indep binary-arch
.PHONY: build clean binary-indep binary-arch binary install 
