#!/usr/bin/make -f
# Sample debian/rules that uses debhelper. 
# GNU copyright 1997 by Joey Hess.

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

# This has to be exported to make some magic below work.
export DH_OPTIONS

# brace expansion
export SHELL=/bin/bash

DEB_HOST_ARCH		?= $(shell dpkg-architecture -qDEB_HOST_ARCH)
DEB_HOST_GNU_TYPE	?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)

some_arch_match := no
ifeq ($(DEB_HOST_ARCH),$(findstring $(DEB_HOST_ARCH),hurd sh))
  some_arch_match := yes
endif

distribution := $(shell lsb_release -is)

auto: auto-update-stamp
auto-update-stamp:
	: # update autotools files
	for d in capi20 isdnlog/client vbox3; do \
	  cp -p /usr/share/misc/config.* $$d/; \
	done

	@set -e; \
	for d in capi20; do \
	  echo "Regenerating autotools files in $$d ..."; \
	  cd $$d; \
	  echo "  libtool"; libtoolize -c -f ; \
	  echo "  aclocal-1.9"; aclocal-1.9; \
	  echo "  automake-1.9"; automake-1.9 --add-missing; \
	  cd ..; \
	done

	@set -e; \
	for d in capifax capiinfo capiinit rcapid; do \
	  echo "Regenerating autotools files in $$d ..."; \
	  cd $$d; \
	  echo "  aclocal"; aclocal; \
	  echo "  autoconf"; autoconf; \
	  case "$$d" in rcapid);; *) echo "  autoheader"; autoheader; esac; \
	  echo "  automake"; automake --add-missing; \
	  touch configure.in; \
	  touch aclocal.m4; \
	  touch configure; \
	  cd ..; \
	done

ifeq (0,1)
	: # does not work, automake-1.3 was used ...
	@set -e; \
	for d in isdnlog/client; do \
	  echo "Regenerating autotools files in $$d ..."; \
	  cd $$d; \
	  echo "  aclocal"; aclocal; \
	  echo "  autoconf"; autoconf; \
	  echo "  autoheader"; autoheader; \
	  echo "  automake"; automake -c --add-missing; \
	  touch configure.in; \
	  touch aclocal.m4; \
	  touch configure; \
	  cd ..; \
	done
endif
	@set -e; \
	for d in vbox; do \
	  echo "Regenerating autotools files in $$d ..."; \
	  cd $$d; \
	  echo "  aclocal-1.7"; aclocal-1.7; \
	  echo "  autoconf"; autoconf; \
	  echo "  automake-1.7"; automake-1.7 -c --add-missing; \
	  touch configure.in; \
	  touch aclocal.m4; \
	  touch configure; \
	  cd ..; \
	done

	@set -e; \
	for d in eicon; do \
	  echo "Regenerating autotools files in $$d ..."; \
	  cd $$d; \
	  echo "  autoconf2.13"; autoconf2.13; \
	  cd ..; \
	done

# autoheader doesn't work in vbox ... is it needed?
#	  echo "  autoheader"; autoheader; \

	cd ipppd && autoconf

	touch auto-update-stamp

build: build-stamp
build-stamp: auto-update-stamp
	dh_testdir

	# we need a path to the kernel sources.
	# Link to top dir so that local choice can be preserved. That link must
	# be made first thing, otherwise it will try to unpack
	# /usr/src/kernel-source-2.4.*.tar.bz2 .
	#  This used to be "parent dir", but that doesn't always work for
	#  build daemons...
	# This has moved to an external script, as large chunks of shell
	# script in makefiles is an ugly solution.
	# sh debian/prepare-kernel-source $(DEB_HOST_ARCH)
	-rm -f ../linux

	# cp debian/isdnactivecards.files.all     debian/isdnactivecards.files
	# we need a .config file. Try isdnutils.config in parent directory first
	# so that local choice can be preserved.
	test -f ../isdnutils.config && cp ../isdnutils.config .config || \
	    sed -e "s#@KERNELDIR@#`pwd`/linux#" \
		< debian/dotconfig > .config
ifeq ($(some_arch_match),yes)
	: # build only isdnvboxclient on the "excluded" arches
	sed -e "s#@KERNELDIR@#`pwd`/linux#" \
		< debian/dotconfig.vboxclientonly > .config
endif

	# : # ugh
	# touch eurofile/configure

	$(MAKE) subconfig

	$(MAKE)


ifneq ($(some_arch_match),yes)
	cd isdnlog/tools/zone; $(MAKE) zonefiles
	cd Mini-FAQ; make
endif

	touch build-stamp

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp auto-update-stamp

	[ ! -f Makefile ] || $(MAKE) distclean
	rm -f isdnlog/client/config.cache \
		isdnlog/client/config.status isdnlog/client/config.log
	#-rm -f vbox/examples/beep.msg.example
	#-rm -f `find . -name "*~"`
	#-rm -rf debian/tmp* `find debian/* -type d ! -name CVS` debian/files* core
	-rm -rf linux debian/linux debian/kernel-source-*
	# -rm -f  debian/isdnactivecards.files debian/isdnactivecards.conffiles

	: # remove updated/added autotools files
	find . -name config.sub -o -name config.guess -o \
		-name autom4te.cache -o -name config.cache -o \
		-name config.log -o -name config.status -o -name aclocal.m4 -o \
		-name config.h -o -o -name ltmain.sh | xargs rm -f
	rm -f capifax/configure capiinfo/configure capiinit/configure \
		rcapid/configure vbox/configure eicon/configure
	rm -f capi20/Makefile.in capifax/Makefile.in capiinfo/Makefile.in \
		capiinit/Makefile.in rcapid/Makefile.in vbox/doc/Makefile.in \
		vbox/doc/de/Makefile.in vbox/utils/Makefile.in
	find . -type l \
	    \( -name install-sh -o -name mkinstalldirs -o -name missing \
	       -o -name compile -o -name depcomp \
	    \) \
		| xargs rm -f

	[ ! -f Makefile ] || $(MAKE) distclean

	rm -f capiinit/stamp-h.in
	rm -f vbox/src/stamp-h1

	debconf-updatepo

	dh_clean

install: DH_OPTIONS=
install: build
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs
	# Install ALL dirs into tmp also. Unfortunately there's no easy way...
	mkdir -p debian/tmp/sbin
	sort -u debian/*.dirs > debian/tmp.dirs
	dh_installdirs -Pdebian/tmp -pisdnutils `cat debian/tmp.dirs`
	rm debian/tmp.dirs

	# Add here commands to install the package into debian/tmp
	# (the files get moved out by dh_movefiles later)
	$(MAKE) install DESTDIR=`pwd`/debian/tmp

	cp -p isdnlog/holiday-??.dat debian/tmp/usr/share/isdn/

ifneq ($(some_arch_match),yes)
	chmod +x debian/finish-install-first.sh
	debian/finish-install-first.sh
	mv debian/tmp/usr/bin/capifaxrcvd debian/tmp/usr/sbin/
	dh_movefiles -i
endif
	: # These apply to all arch'es:
	mv debian/tmp/usr/share/man/man5/vbox_file.5 \
	    debian/tmp/usr/share/man/man5/vbox.5
	install -m 0644 vbox/examples/vboxrc.example \
	    debian/isdnvboxclient/usr/share/doc/isdnvboxclient/examples/vboxrc

	dh_movefiles -s

ifneq ($(some_arch_match),yes)
	chmod +x debian/finish-install-last.sh
	debian/finish-install-last.sh
endif


	# These apply to all arch'es:
	# the binary is called vboxcnvt, not vboxconvert
	mv debian/isdnvboxclient/usr/share/man/man1/vboxconvert.1 \
	    debian/isdnvboxclient/usr/share/man/man1/vboxcnvt.1

	mkdir -p debian/isdnutils-xtools/usr/share/man/man1/
	mv debian/isdnutils-xtools/usr/man/man1/xisdnload.1x \
	    debian/isdnutils-xtools/usr/share/man/man1/xisdnload.1x
	rmdir -p --ignore-fail-on-non-empty debian/isdnutils-xtools/usr/man/man1

# Build architecture-independent files here.
# Pass -i to all debhelper commands in this target to reduce clutter.
binary-indep: DH_OPTIONS=-i
binary-indep: build install
	dh_testdir
	dh_testroot
#	dh_installdebconf
	dh_installdocs
	dh_installexamples
#	dh_installmenu
#	dh_installemacsen
#	dh_installpam
#	dh_installinit
#	dh_installcron
#	dh_installinfo
	dh_installchangelogs 
	dh_link
	dh_compress
	dh_fixperms
	dh_installdeb
	for i in debian/*/DEBIAN/conffiles; do \
	  [ -f $$i ] || continue; \
	  sort -u $$i > $$i.sorted; mv $$i.sorted $$i; \
	done
#	dh_perl
	dh_gencontrol
	dh_md5sums
	dh_builddeb

# Build architecture-dependent files here.
# Pass -a to all debhelper commands in this target to reduce clutter.
binary-arch: DH_OPTIONS=-s
binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installdebconf
	dh_installdocs
ifneq ($(some_arch_match),yes)
	install -m 0644 debian/README.MPPP debian/README.multiple-ipppd \
	    debian/ipppd/usr/share/doc/ipppd/
	#install -m 0644 debian/isdneurofile.README.Debian \
	#    debian/isdneurofile/usr/share/doc/isdneurofile/README.Debian
endif
	dh_installexamples
	dh_installmenu
#	dh_installpam
	# override the "global" DH_OPTIONS=-a setting for one command
ifneq ($(some_arch_match),yes)
	DH_OPTIONS='' dh_installinit -pcapiutils \
		--no-restart-on-upgrade --update-rcd-params='defaults 13 21'
	mkdir -p debian/capiutils/etc/modprobe.d
	cp -p debian/capiutils.blacklist \
		debian/capiutils/etc/modprobe.d/blacklist-capiutils.conf
endif
	# Ok, for two commands
	# No, for one command after all. Do this one by hand.
	# env DH_OPTIONS='' dh_installinit --no-restart-on-upgrade -pisdnutils-base
	# the postinst stuff normally done by dh is now statically in there.
ifneq ($(some_arch_match),yes)
	install -m 0755 debian/isdnutils-base.init \
		debian/isdnutils-base/etc/init.d/isdnutils
endif
	dh_installcron
ifneq ($(some_arch_match),yes)
	ln -s ../man7/isdn_cause.7.gz \
	    debian/isdnutils-base/usr/share/man/man1/isdncause.1.gz
endif
	# lintian junk
	# - duh, it's an example, why should it be executable
	install -d debian/isdnutils-base/usr/share/lintian/overrides
	install -m 644 debian/isdnutils-base.lintian-overrides debian/isdnutils-base/usr/share/lintian/overrides/isdnutils-base
	# - the upstream source contains junk, how am I supposed to change that?!
	install -d debian/isdnutils/usr/share/lintian/overrides
	install -m 644 debian/isdnutils.lintian-overrides debian/isdnutils/usr/share/lintian/overrides/isdnutils
	# # - INSTALL is not a compilation guide, it's a real usage guide.
	# install -d debian/isdneurofile/usr/share/lintian/overrides
	# install -m 644 debian/isdneurofile.lintian-overrides debian/isdneurofile/usr/share/lintian/overrides/isdneurofile
	# - again, examples scripts that shouldn't be executable
	# - and I *do* want a different default depending on the language!
	install -d debian/isdnlog/usr/share/lintian/overrides
	install -m 644 debian/isdnlog.lintian-overrides debian/isdnlog/usr/share/lintian/overrides/isdnlog
	# - these shared libraries are only used by a single binary that knows all about them
	install -d debian/pppdcapiplugin/usr/share/lintian/overrides
	install -m 644 debian/pppdcapiplugin.lintian-overrides debian/pppdcapiplugin/usr/share/lintian/overrides/pppdcapiplugin
	# - there's no -dev package, so shut up about the shlib symlink
	# - No, I don't call ldconfig in an unsafe manner
	install -d debian/libcapi20-3/usr/share/lintian/overrides
	install -m 644 debian/libcapi20-3.lintian-overrides debian/libcapi20-3/usr/share/lintian/overrides/libcapi20-3
	# - not much chance of more processed with the name "vboxgetty", and start-stop-daemon won't work
	# - db_input in postinst only in extreme situations
	install -d debian/isdnvboxserver/usr/share/lintian/overrides
	install -m 644 debian/isdnvboxserver.lintian-overrides debian/isdnvboxserver/usr/share/lintian/overrides/isdnvboxserver
	# - yet again, examples scripts that shouldn't be executable
	# - db_input in postinst only in extreme situations
	install -d debian/ipppd/usr/share/lintian/overrides
	install -m 644 debian/ipppd.lintian-overrides debian/ipppd/usr/share/lintian/overrides/ipppd

	dh_installchangelogs
	rm -rf debian/libcapi20-dev/usr/share/doc/libcapi20-dev
	ln -s libcapi20-3 debian/libcapi20-dev/usr/share/doc/libcapi20-dev
	dh_strip
	dh_link
	dh_compress
	dh_fixperms
	dh_installdeb
#	dh_makeshlibs
#	dh_perl
	dh_shlibdeps -L libcapi20-3 -l debian/libcapi20-3/usr/lib
	# there's already a versioned depends in the control file for libcapi20-3
	perl -i -pe 's/, libcapi20-3//' debian/capiutils.substvars
	for i in debian/*/DEBIAN/conffiles; do \
	  [ -f $$i ] || continue; \
	  sort -u $$i > $$i.sorted; mv $$i.sorted $$i; \
	done
	dh_gencontrol
##	# dont build isdnvboxclient on all architectures.
##	for pkg in isdnutils isdnutils-xtools ipppd isdnlog isdnvbox isdnvboxserver libcapi20-3 isdnactivecards pppdcapiplugin isdneurofile; do \
##	    dpkg-gencontrol -ldebian/changelog -isp -p$pkg -Tdebian/$pkg.substvars -Pdebian/$pkg; \
##	done
##	case $(DEB_HOST_ARCH) in \
##	    hurd)	;; \
##	    *)		dpkg-gencontrol -ldebian/changelog -isp -pisdnvboxclient -Tdebian/isdnvboxclient.substvars -Pdebian/isdnvboxclient; \
##	esac
##	chmod 644 debian/*/DEBIAN/control
##	chown 0.0 debian/*/DEBIAN/control
	dh_md5sums
	dh_builddeb
	# checking that nothing is left behind...
	rm -f debian/tmp/etc/services # this should be ignored
x:
ifeq ($(some_arch_match),yes)
	echo "Ignore any of the 'still existing files' listed below on this architecture."
endif
	if [ ! -z "`find debian/tmp ! -type d`" ]; then echo "The following files still exist in debian/tmp/:"; find debian/tmp ! -type d -ls; fi;

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