#!/usr/bin/make -f
#
# debian/rules file - for GPM (1.17.6).
#
# Based on sample debian.rules file - for GNU Hello (1.3).
# Copyright 1994,1995 by Ian Jackson.
# Copyright 1997,1998,1999 James Troup.
# Copyright 2004,2005 Guillem Jover.
#
# I hereby give you perpetual unlimited permission to copy,
# modify and relicense this file, provided that you do not remove
# my name from the file itself.  (I assert my moral right of
# paternity under the Copyright, Designs and Patents Act 1988.)
# This file may have to be extensively modified

#export DH_VERBOSE = 1

include debian/vars

DBS = $(MAKE) -f /usr/share/dbs/sys-build.mk

this = make -f debian/rules

BUILD_DIR=$(SOURCE_DIR)/$(TAR_DIR)
export BD=$(BUILD_DIR)

INSTALL=install
INSTALL_PROGRAM=$(INSTALL) -m 755
INSTALL_SCRIPT=$(INSTALL) -m 755
INSTALL_DATA=$(INSTALL) -m 644

CFLAGS = -Wall -g -D_REENTRANT

ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -O0
else
	CFLAGS += -O2
endif
ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
	INSTALL_PROGRAM += -s
endif

# Minor number of the soname

SOMINOR=19.6

unpack:
	$(DBS) source.make

build: unpack build-stamp
build-stamp:
	dh_testdir
	$(MAKE) -C $(BD)/contrib/control
	cd $(BD); autoconf
	cd $(BD); CFLAGS="$(CFLAGS)" ./configure \
				--prefix=/usr --sysconfdir=/etc
	$(MAKE) -C $(BD)
	touch $@

clean:
	dh_testdir
	
	-$(DBS) source.clean
	-rm -f *stamp*
	-rm -rf debian/stampdir
	-dh_clean

checkpo:
	debconf-updatepo
	@for i in debian/po/*.po; do \
	  echo -n "Checking: $$i "; \
	  msgfmt -o /dev/null -c --statistics $$i; \
	done

# Clean for diff with dbs
diff_clean:
	dh_testdir
	
	-cd $(BD); (find -type f -name "*.d" | xargs rm)
	-$(MAKE) -C $(BD)/doc distclean
	-$(MAKE) -C $(BD)/contrib maintainer-clean
	-$(MAKE) -C $(BD)/contrib/control clean
	-$(MAKE) -C $(BD) -i clean
	-rm -rf $(BD)/config.log $(BD)/config.status $(BD)/*~
	-rm -rf $(BD)/libc5
	-rm -f *stamp*

# No binary indep packages
binary-indep:

binary-arch: binary-gpm binary-libgpmg1 binary-libgpmg1-dev

binary-gpm:
	DH_OPTIONS="-pgpm" $(this) binary-gpm-real
binary-gpm-real: build
	dh_testdir
	dh_testroot
	dh_clean -k
	
	dh_installdirs
	
	# First lets install some debian native stuff
	$(INSTALL_PROGRAM) $(BD)/contrib/control/gpm_has_mouse_control \
		debian/gpm/usr/lib/gpm/gpm_has_mouse_control
	$(INSTALL_SCRIPT) debian/gpmconfig debian/gpm/usr/sbin/gpmconfig
	$(INSTALL_SCRIPT) debian/gpm.apm debian/gpm/etc/apm/event.d/gpm
	$(INSTALL_DATA) debian/gpm.lintian-overrides \
		debian/gpm/usr/share/lintian/overrides/gpm
	
	# Now the GPM stuff
	$(INSTALL_PROGRAM) $(BD)/src/gpm debian/gpm/usr/sbin/
	$(INSTALL_PROGRAM) $(BD)/src/mouse-test \
		debian/gpm/usr/sbin/gpm-mouse-test
	$(INSTALL_SCRIPT) $(BD)/contrib/scripts/microtouch-setup \
		debian/gpm/usr/sbin/gpm-microtouch-setup
	$(INSTALL_PROGRAM) $(BD)/src/mev debian/gpm/usr/bin/
	#$(INSTALL_PROGRAM) $(BD)/sample/rmev debian/gpm/usr/bin/
	
	dh_installinfo $(BD)/doc/gpm.info
	
	$(INSTALL_DATA) $(BD)/doc/mev.1 \
			debian/gpm/usr/share/man/man1/
	$(INSTALL_DATA) $(BD)/doc/gpm.8 debian/gpmconfig.8 \
			debian/gpm/usr/share/man/man8/
	$(INSTALL_DATA) $(BD)/doc/gpm-types.7 \
			debian/gpm/usr/share/man/man7/
	$(INSTALL_DATA) $(BD)/doc/gpm-mouse-test.8 \
			debian/gpm/usr/share/man/man8/
	$(INSTALL_DATA) debian/gpm.conf.5 \
			debian/gpm/usr/share/man/man5/
	
	dh_installdocs $(BD)/README $(BD)/MANIFEST $(BD)/TODO \
		       $(BD)/doc/Announce $(BD)/doc/FAQ $(BD)/doc/README* \
		       $(BD)/doc/gpmdoc.txt $(BD)/doc/gpmdoc.ps
	dh_installexamples $(BD)/conf/gpm-syn.conf \
			   $(BD)/conf/gpm-twiddler.conf \
			   $(BD)/contrib/emacs/t-mouse.el
	dh_installchangelogs $(BD)/ChangeLog
	dh_installinit
	dh_installdebconf
	dh_installdeb
	dh_strip
	dh_compress
	dh_fixperms
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary-libgpmg1:
	DH_OPTIONS="-plibgpmg1" $(this) binary-libgpmg1-real
binary-libgpmg1-real: build
	dh_testdir
	dh_testroot
	dh_clean -k
	
	dh_installdirs
	
	$(INSTALL_DATA) $(BD)/src/libgpm.so.1.$(SOMINOR) debian/libgpmg1/usr/lib
	ln -s libgpm.so.1.$(SOMINOR) debian/libgpmg1/usr/lib/libgpm.so.1
	dh_strip
	dh_makeshlibs
	
	dh_installdocs
	dh_installchangelogs $(BD)/ChangeLog
	dh_installdeb
	
	dh_compress
	dh_fixperms
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary-libgpmg1-dev:
	DH_OPTIONS="-plibgpmg1-dev" $(this) binary-libgpmg1-dev-real
binary-libgpmg1-dev-real: build
	dh_testdir
	dh_testroot
	dh_clean -k
	
	dh_installdirs
	
	$(INSTALL_DATA) $(BD)/src/headers/gpm.h debian/libgpmg1-dev/usr/include/
	$(INSTALL_DATA) $(BD)/src/libgpm.a debian/libgpmg1-dev/usr/lib/
	ln -sf libgpm.so.1 debian/libgpmg1-dev/usr/lib/libgpm.so
	
	dh_strip
	dh_installdocs
	dh_installchangelogs $(BD)/ChangeLog
	dh_installdeb
	
	dh_compress
	dh_fixperms
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

# Below here is fairly generic really

binary: binary-indep binary-arch

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

