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

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

export LIBTOOLIZE=true
#export DEB_BUILD_MAINT_OPTIONS="hardening=+all"
#export DEB_CFLAGS_MAINT_APPEND="-Wall -pedantic"
#export DEB_LDFLAGS_MAINT_APPEND=-Wl,--as-needed
CPPFLAGS := $(shell dpkg-buildflags --get CPPFLAGS)
CFLAGS   := -O2 -Wall -fcommon -fpermissive $(shell dpkg-buildflags --get CFLAGS)
CXXFLAGS := -O2 -Wall $(shell dpkg-buildflags --get CXXFLAGS)
LDFLAGS  := $(shell dpkg-buildflags --get LDFLAGS)
ifneq (,$(findstring debug,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -g
endif

configure: configure-stamp
configure-stamp:
	# Change section for some man files
	mv man/fr/gatos.1 man/fr/gatos.7
	perl -pi -e 's/gatos\.1/gatos.7/' man/fr/Makefile.am
	perl -pi -e 's/TH GATOS 1/TH GATOS 7/' man/fr/gatos.7
	mv man/en/gatos.1 man/en/gatos.7
	perl -pi -e 's/gatos\.1/gatos.7/' man/en/Makefile.am
	perl -pi -e 's/TH GATOS 1/TH GATOS 7/' man/en/gatos.7
	#
	mv man/en/gatos.conf.1.in man/en/gatos.conf.5.in
	rm man/en/gatos.conf.1
	perl -pi -e 's/gatos\.conf\.1/gatos.conf.5/g' man/en/Makefile.am
	perl -pi -e 's/TH gatos.conf 1/TH gatos.conf 5/' man/en/gatos.conf.5.in
	#
	mv man/fr/gatos.conf.1.in man/fr/gatos.conf.5.in
	rm man/fr/gatos.conf.1
	perl -pi -e 's/gatos\.conf\.1/gatos.conf.5/g' man/fr/Makefile.am
	perl -pi -e 's/TH gatos.conf 1/TH gatos.conf 5/' man/fr/gatos.conf.5.in
	#
	dh_testdir
	./autogen.sh
	CC="g++" dh_auto_configure -- --without-kernel-delay --enable-shared --prefix=/usr --mandir=\$${prefix}/share/man --infodir=\$${prefix}/share/info
	touch configure-stamp

build: build-arch build-stamp
build-arch: build-stamp
build-indep: build-stamp
build-stamp: configure-stamp 
	dh_testdir
	# Add here commands to compile the package.
	$(MAKE) CC=gcc CFLAGS="$(CFLAGS)"
	#/usr/bin/docbook-to-man debian/gatos.sgml > gatos.1
	touch build-stamp

clean:
	dh_testdir
	rm -f build-stamp configure-stamp
	# Add here commands to clean up after the build process.
	[ ! -f Makefile ] || $(MAKE) clean
	[ ! -f Makefile ] || $(MAKE) distclean
	rm -f config.log compile missing configure \
		m4/ltoptions.m4 \
		m4/ltsugar.m4 \
		m4/ltversion.m4 \
		m4/lt~obsolete.m4 \
		m4/libtool.m4 \
		aclocal.m4 \
		acinclude.m4
	# Automake generated
	rm -f config.guess config.sub config.status INSTALL install-sh ltmain.sh \
		Makefile.in \
		docs/Makefile.in \
		gfxdump/Makefile.in \
		m4/Makefile.in \
		man/Makefile.in \
		po/Makefile.in \
		src/Makefile.in \
		tech-docs/Makefile.in \
		*/*/Makefile.in
	# PO generated
	#rm -f po/fr/atisplit.gmo \
	#	po/fr/atitogif.gmo \
	#	po/fr/atitojpg.gmo \
	#	po/fr/atitoppm.gmo \
	#	po/fr/gatos.gmo \
	#	po/fr/yuvsum.gmo
	# Other generated
	rm -f	gatos.spec intl/po2tbl.sed po/POTFILES
	dh_clean

install: build
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs
	# Add here commands to install the package into debian/gatos.
	$(MAKE) install prefix=$(CURDIR)/debian/tmp/usr docdir=$(CURDIR)/debian/tmp/usr/share/doc/gatos
	mkdir -p $(CURDIR)/debian/gatos/etc
	install -m 644 debian/gatos.conf $(CURDIR)/debian/gatos/etc/gatos.conf
	mkdir -p $(CURDIR)/debian/gatos/usr/share/applications
	install -m 644 $(CURDIR)/debian/xatitv.desktop $(CURDIR)/debian/gatos/usr/share/applications/xatitv.desktop

# Build architecture-independent files here.
binary-indep: build install
# We have nothing to do by default.

# Build architecture-dependent files here.
binary-arch: build install
	dh_testdir
	dh_testroot
	dh_install
#	dh_installdebconf	
	dh_installdocs
	dh_installexamples
	dh_installmenu
#	dh_installlogrotate
#	dh_installemacsen
#	dh_installpam
#	dh_installmime
#	dh_installinit
	dh_installcron
	dh_installman
	dh_installinfo
#	dh_undocumented
	dh_installchangelogs ChangeLog
	dh_link
	dh_strip
	dh_compress
	dh_fixperms
	#chmod 4755 $(CURDIR)/debian/gatos/usr/bin/xatitv
	#chmod 4755 $(CURDIR)/debian/gatos/usr/bin/atitv
	mv debian/gatos/usr/bin/scanpci debian/gatos/usr/bin/scanpci.gatos
	mv debian/gatos/usr/share/man/man1/scanpci.1.gz debian/gatos/usr/share/man/man1/scanpci.gatos.1.gz
	mv debian/gatos/usr/share/man/fr/man1/scanpci.1.gz debian/gatos/usr/share/man/fr/man1/scanpci.gatos.1.gz
	dh_makeshlibs
	dh_installdeb
#	dh_perl
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

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