#!/usr/bin/make -f
# Made with the aid of dh_make, by Craig Small
# Sample debian/rules that uses debhelper. GNU copyright 1997 by Joey Hess.
# Some lines taken from debmake, by Cristoph Lameter.

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

# This is the debhelper compatibility version to use.
export DH_COMPAT=1

# Set CFLAGS according to the Debian Policy:
CFLAGS = -g -Wall
ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -O0
else
	CFLAGS += -O2
endif
CXXFLAGS = $(CFLAGS)


# Compile the beast!:
build: build-stamp
build-stamp: debianbackup.tar
	dh_testdir
	CFLAGS="$(CFLAGS)" CXXFLAGS="$(CXXFLAGS)" ./configure \
		--prefix=$(shell pwd)/debian/tmp/usr --fhs --no-comp-exe
	CFLAGS="$(CFLAGS)" CXXFLAGS="$(CXXFLAGS)" $(MAKE)
	CFLAGS="$(CFLAGS)" CXXFLAGS="$(CXXFLAGS)" $(MAKE) -C makes libset
	touch build-stamp


# Clean everything as if nothing happened :) :
clean: clean-debian debianbackup.tar
# Heh, for cleaning we need to run the configure script in order to create
# the makefile that cleans! :-)))
	./configure
	$(MAKE) clean
# Restore upstream provided regenerated files:
	test -e debianbackup.tar && tar -xf debianbackup.tar
	rm -f debianbackup.tar
# FIXME: ask upstream about adding the next files to his clean target:
	rm -f internac/fix.exe internac/gr.po internac/po_list \
		internac/getcolors.exe internac/potfiles.po internac/de.mo \
		internac/es.mo holidays/Makefile redhat/setedit-*.*.spec \
		gettext/Makefile libbzip2/Makefile libpcre/Makefile libz/Makefile \
		makes/rhide.env mp3/libamp/rhide.env mp3/mpegsound/rhide.env
# Don't delete this files (they are on the tarball but not on CVS):
#		doc/Makefile internac/Makefile

# Use the following target when you just want for example to rebuild the
# package without recompiling the sources:
clean-debian:
	dh_testdir
	dh_testroot
	rm -f build-stamp
	dh_clean


# Let's backup modified files to restore them later!:
debianbackup.tar:
	dh_testdir
# Backup upstream provided regenerated files:
	-tar -cf debianbackup.tar --files-from=debian/files_to_backup


# Copy needed files to the temporary directories for the packages:
install: build
	dh_testdir
	dh_testroot
	dh_clean
#   dh_installdirs
# Add here commands to install the package into debian/tmp.
	$(MAKE) install
# We need to move files for other packages than the first one in the control file:
	dh_movefiles -p infview usr/bin/infview usr/share/doc/infview/ usr/share/infview/ usr/share/man/man1/infview.1.gz
# I don't know why dh_movefiles doesn't remove the directories I requested for moving to the infview package, so I move them by hand (anyway I don't know why that command exists in the first place!):
	rm -rf debian/tmp/usr/share/doc/infview/ debian/tmp/usr/share/infview/
	mv debian/tmp/usr/share/doc/setedit/change.log.gz \
		debian/tmp/usr/share/doc/setedit/changelog.gz
	cp debian/tmp/usr/share/doc/setedit/changelog.gz \
		debian/infview/usr/share/doc/infview/
	dh_link usr/share/man/man1/setedit.1.gz usr/share/man/man1/e.1.gz
	mkdir debian/infview/usr/share/info
	cp debian/tmp/usr/share/info/infview.info.gz debian/infview/usr/share/info/infview.info.gz
	cp debian/README.debian \
		debian/infview/usr/share/doc/infview/README.Debian
# Install libset files:
	mkdir -p debian/libset-dev/usr/include/setedit \
		debian/libset-dev/usr/lib
	tar c \
		calcu/calcu.h \
		include/ceditor.h \
		include/sindicat.h \
		include/tvsetuti.h \
		include/pmcoll.h \
		include/ced_exte.h \
		include/ced_clas.h \
		include/ced_inte.h \
		include/ced_coma.h \
		include/ced_pcre.h \
		include/loadkbin.h \
		include/slpinter.h \
		include/loadshl.h \
		infview/include/inf.h \
		infview/include/infbase.h \
		setedit/include/editcoma.h \
		setedit/include/edprint.h \
		settvuti/include/settvuti.h \
		settvuti/include/tstringa.h \
		settvuti/include/sarray.h \
		settvuti/include/setstack.h \
		settvuti/include/tnocastc.h \
		settvuti/include/viewplus.h \
		settvuti/include/tinppipe.h \
		settvuti/include/edhists.h \
		|tar xC debian/libset-dev/usr/include/setedit
	cp makes/libset.a makes/libeasyd.a makes/libsettv.a \
		debian/libset-dev/usr/lib


# Build the packages for a specific architecture:
binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installdebconf
	dh_installdocs
#	dh_installexamples
	dh_installmenu
#	dh_installemacsen
#	dh_installinit
#	dh_installcron
#	dh_installmanpages
#	dh_undocumented e.1 setedit.1
	dh_installchangelogs
	dh_strip -Xlibset.a -Xlibeasyd.a -Xlibsettv.a
	dh_compress
	dh_fixperms
	chmod 4755 debian/tmp/usr/bin/setedit
	chmod 4755 debian/infview/usr/bin/infview
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol -p setedit
	dh_gencontrol -p infview -- -v$$(cat infview/version.txt)-$$(cat version.txt)-$$(head -n1 debian/changelog | sed 's/.* (.*-//' | sed 's/) .*//')
	dh_gencontrol -p libset-dev
#	dh_makeshlibs  This is for libraries.
	dh_md5sums
	dh_builddeb


# Build architecture-independent files here.
binary-indep: build install
#This package doesn't have architecture-independent files for the moment...

source diff:
	@echo >&2 'source and diff are obsolete - use dpkg-source -b'; false

# Build all the packages:
binary: binary-indep binary-arch

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