#!/usr/bin/make -f
# Sample debian/rules that uses debhelper. 
# GNU copyright 1997 by Joey Hess.
#
# This version is for a hypothetical package that builds an
# architecture-dependant package, as well as an architecture-independent
# package.

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

# This is the debhelper compatability version to use, for local overrides.
#export DH_COMPAT=4

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

INSTALL = install
INSTALL_FILE    = $(INSTALL) -p    -o root -g root  -m  644
INSTALL_PROGRAM = $(INSTALL) -p    -o root -g root  -m  755
INSTALL_SCRIPT  = $(INSTALL) -p    -o root -g root  -m  755
INSTALL_DIR     = $(INSTALL) -p -d -o root -g root  -m  755

CPPFLAGS:=$(shell dpkg-buildflags --get CPPFLAGS)
CFLAGS:=$(shell dpkg-buildflags --get CFLAGS) -fgnu89-inline
CXXFLAGS:=$(shell dpkg-buildflags --get CXXFLAGS)
LDFLAGS:=$(shell dpkg-buildflags --get LDFLAGS)

ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
INSTALL_PROGRAM += -s
endif

export DEB_HOST_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
export DEB_BUILD_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
# FOR AUTOCONF 2.52 AND NEWER ONLY
ifeq ($(DEB_BUILD_GNU_TYPE), $(DEB_HOST_GNU_TYPE))
  confflags += --build $(DEB_HOST_GNU_TYPE)
else
  confflags += --build $(DEB_BUILD_GNU_TYPE) --host $(DEB_HOST_GNU_TYPE)
endif

config: config-stamp
config-stamp:
	dh_testdir
	# avoid time skews
	touch configure.ac && 	touch aclocal.m4 && 	touch configure
	touch config.h.in  */Makefile.in Makefile.in
	#https://bugs.debian.org/744550
	dh_autoreconf
	#
	./configure $(confflags) --prefix=/usr --mandir=/usr/share/man \
		--infodir=/usr/share/info --disable-rpath \
		--with-xmorph --with-gtkmorph --with-gtk=2 \
		 --with-waili --without-fftw \
		CFLAGS="$(CFLAGS)" LDFLAGS="$(LDFLAGS)" \
		CPPFLAGS="$(CPPFLAGS)" CXXFLAGS="$(CXXFLAGS)"
	touch config-stamp

build: config build-arch build-indep

build-indep: config build-indep-stamp
build-indep-stamp:
	touch build-indep-stamp

build-arch: config build-arch-stamp
build-arch-stamp:
	# Add here commands to compile the package.
	$(MAKE)
	#
	touch build-arch-stamp

install-indep: DH_OPTIONS=-i
install-indep: build-indep
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs
	# Add here commands to install the package into debian/tmp.
	$(MAKE) install-data-local DESTDIR=`pwd`/debian/tmp
	dh_movefiles
	dh_link -pgtkmorph-example usr/share/xmorph/example usr/share/gtkmorph/example
	dh_link -pgtkmorph-example usr/share/xmorph/example usr/share/doc/gtkmorph-example/examples
	dh_link -pgtkmorph-example usr/share/xmorph/example usr/share/doc/gtkmorph/examples

install-arch: DH_OPTIONS=-a
install-arch: build-arch
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs
	# Add here commands to install the package into debian/tmp.
	$(MAKE) install DESTDIR=`pwd`/debian/tmp
	dh_movefiles
	dh_link -pxmorph usr/share/man/man1/xmorph.1.gz usr/share/man/man1/morph.1.gz
	dh_link -pgtkmorph usr/share/xmorph/pixmaps usr/share/gtkmorph/pixmaps


# 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-indep install-indep
	dh_testdir
	dh_testroot
#	dh_installdebconf
	dh_installdocs
#nope! it was getting installed twice!
#	#dh_installexamples -pgtkmorph-example example
	dh_installmenu
#	dh_installemacsen
#	dh_installpam
#	dh_installinit
	dh_installcron
	dh_installman
#is done by postinst	
#	dh_undocumented
#	dh_installinfo
	dh_installchangelogs
	#ChangeLog
	dh_link
	dh_compress
	dh_fixperms
	dh_installdeb
#	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=-a
binary-arch: build-arch install-arch
	dh_testdir
	dh_testroot
#	dh_installdebconf
	dh_installdocs
#	dh_installexamples
	dh_installmenu
#	dh_installemacsen
#	dh_installpam
#	dh_installinit
#	dh_installcron
	dh_installman
# is done correctly in my postinst
#	dh_installinfo
#	dh_undocumented
	dh_installchangelogs 
	#ChangeLog
	dh_strip
	dh_link
	dh_compress
	dh_fixperms
	dh_makeshlibs
	dh_installdeb
#	dh_perl
	dh_shlibdeps -ldebian/libmorph/usr/lib
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary:  binary-arch binary-indep



clean:
	dh_testdir
	dh_testroot
	rm -f build-arch-stamp build-indep-stamp config-stamp

	# Add here commands to clean up after the build process.
	[ ! -f Makefile ] || $(MAKE) distclean
	#
	#https://bugs.debian.org/744550
	dh_autoreconf_clean
	#
	dh_clean



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