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

override_dh_installexamples:
	dh_installexamples -X.in

override_dh_shlibdeps:
	dh_shlibdeps -L libcapi20-3 -l debian/libcapi20-3/usr/lib

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-arch build-indep
build-arch: build-stamp
build-indep: 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

	$(MAKE) subconfig

	$(MAKE)

	cd isdnlog/tools/zone; $(MAKE) zonefiles
	cd Mini-FAQ; make

	touch build-stamp

# XXX this is the standard dh call
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

	chmod +x debian/finish-install-first.sh
	debian/finish-install-first.sh
	mv debian/tmp/usr/bin/capifaxrcvd debian/tmp/usr/sbin/
	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

	# remove some unwanted stuff
	set -e
	if [ -s debian/isdneurofile/etc/init.d/eftd.sh ]; then mv debian/isdneurofile/etc/init.d/eftd.sh debian/isdneurofile/etc/init.d/isdneurofile; fi
	rm -f debian/pppdcapiplugin/usr/share/doc/pppdcapiplugin/examples/adsl.conf	# already in /etc/drsdl
	rm -f usr/share/doc/isdnutils-doc/isdn-faq.txt.gz				# already in ./Mini-FAQ/
	rm -vf debian/tmp/etc/isdn/isdnlog.isdnctrl0.options
	rm -vf debian/tmp/usr/share/man/man1/xmonisdn.1x*

	dh install

binary-indep: build install
	dh binary-indep

# 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
	ln -s ../man7/isdn_cause.7.gz \
	    debian/isdnutils-base/usr/share/man/man1/isdncause.1.gz
	rm -rf debian/libcapi20-dev/usr/share/doc/libcapi20-dev
	ln -s libcapi20-3 debian/libcapi20-dev/usr/share/doc/libcapi20-dev

	# checking that nothing is left behind...
	rm -f debian/tmp/etc/services # this should be ignored
	dh binary-arch

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