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

# We use some bashisms
SHELL=/bin/bash

# mega rule -- Joey knows how to do the rest
%:
	dh $@ --with autotools_dev

override_dh_clean:
	dh_clean

	-rm debian/freeipmi-ipmidetectd.debhelper.log

	: # Remove pulled in default file for watchdog
	-rm $(CURDIR)/debian/freeipmi-bcm-watchdog.bmc-watchdog.default

	: # Remove some other generated files
	-rm  doc/freeipmi-faq.info doc/stamp-vti doc/version-faq.texi

override_dh_auto_configure:
	dh_auto_configure -- $(shell dpkg-buildflags --export=configure | grep CFLAGS )

override_dh_auto_install:
	dh_auto_install
	### chmod 644 $(CURDIR)/debian/tmp/etc/ipmi_monitoring_sensors.conf
	: # since it containts sensitive information
	chmod 640 $(CURDIR)/debian/tmp/etc/freeipmi/freeipmi.conf
	chown root.adm $(CURDIR)/debian/tmp/etc/freeipmi/freeipmi.conf
	: # Clean up duplicate/unneeded copyright/installation files
	rm -f $(CURDIR)/debian/tmp/usr/share/doc/freeipmi/COPYING*
	rm -f $(CURDIR)/debian/tmp/usr/share/doc/freeipmi/DISCLAIMER*
	rm -f $(CURDIR)/debian/tmp/usr/share/doc/freeipmi/INSTALL*
	rm -f $(CURDIR)/debian/tmp/usr/share/doc/freeipmi/ChangeLog*

	: # chrpath -- operate only on files (not on still dangling symlinks)
	/usr/bin/find  $(CURDIR)/debian/*/usr/sbin -type f \
	| xargs grep -L '#!/bin/sh' \
	| xargs chrpath -d
	chrpath -d $(CURDIR)/debian/*/usr/lib/*.so.*

	: # assure no dir.gz
	rm -f $(CURDIR)/debian/tmp/usr/share/info/dir


	: # Copy default file for bmc-watchdog
	cp $(CURDIR)/debian/tmp/etc/sysconfig/bmc-watchdog \
		$(CURDIR)/debian/freeipmi-bcm-watchdog.bmc-watchdog.default

	: # Use Debian specific init files with "matching" names
	dh_installinit -pfreeipmi-bmc-watchdog \
	 --name=bmc-watchdog
	dh_installinit -pfreeipmi-ipmidetect \
	 --name=ipmidetectd

# Before isntalldeb check that all pieces were placed into
# corresponding binary packages.  Increasing number of bugreports
# pointed that I was missing pieces -- thus explicit check now.
# Such check will be ran only if both arch-dep and -indep pkgs
# are built which will be checked by presence of corresponding
# directories (patch is welcome for a proper way)
override_dh_installdeb:
	@[ -e debian/freeipmi-common ] && [ -e debian/freeipmi-tools ] && \
	echo "I: Verifying that everything was installed" && \
	find debian/tmp -type f \
	| grep -v '\.la$$' \
	| sed -e 's,etc/sysconfig/bmc-watchdog,etc/default/bmc-watchdog,g' \
	| while read f; do \
		cf=$$(basename $$f); \
		installations=$$(/usr/bin/find debian/ -name $$cf -o -name $$cf.gz | grep -v debian/tmp | wc -l); \
		[ "$$installations" -gt 0 ] || echo "$$installations of $$cf"; \
	done | grep . && exit 1 || :
	dh_installdeb

override_dh_installdocs:
	rm -f debian/freeipmi/usr/share/doc/freeipmi
	mkdir -p debian/freeipmi/usr/share/doc
	ln -s freeipmi-common debian/freeipmi/usr/share/doc/freeipmi
	dh_installdocs NEWS README.* TODO debian/tmp/usr/share/doc/freeipmi/freeipmi-*

override_dh_installchangelogs:
	dh_installchangelogs ChangeLog ChangeLog.0

	#dh_installdocs --link-doc=freeipmi-common

# Readily usable examples
# conflicts with .conf manpages -- apparently matches anywhere in the filename
#override_dh_compress:
#	dh_compress -X.c
