#!/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 -rf linux debian/linux debian/kernel-source-*

	: # 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
	find . -type l \
	    \( -name install-sh -o -name mkinstalldirs -o -name missing \
	       -o -name compile -o -name depcomp \
	    \) \
		| xargs rm -f

	dh clean

	dh_clean

install: DH_OPTIONS=
install: build
	dh_testdir
	dh_testroot
	dh_prep
	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=$(CURDIR)/debian/tmp

	# empty dependency_libs field in *.la files
	sed -i "/dependency_libs/ s/'.*'/''/" "$(CURDIR)/debian/tmp/usr/lib/libcapi20.la"

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

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

	dh_install

# 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_installdocs
	dh_installexamples
	dh_installchangelogs 
	dh_installman
	dh_link
	dh_compress
	dh_fixperms
	dh_installdeb
	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
	dh_installman
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

	dh_lintian
	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_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
	dh_gencontrol
	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
