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

DEBIAN_DESTINATION=$(shell 'pwd')/debian/tmp/usr

# This is needed for the make install target.
export PREFIX=$(DEBIAN_DESTINATION)


build: build-stamp
build-stamp:
	dh_testdir

	# Add here commands to compile the package.

	# If the makes/editor.mak.orig file doesn't exist, then it means that
	# we are compiling for the first time or after a "debian/rules clean",
	# so, we have to backup the files that are going to be modified and
	# modify the upstream "makes/rhide.env" with the Debian modified one.
ifeq ($(wildcard makes/editor.mak.orig),)
	cp mp3/libamp/libamp.mak mp3/libamp/libamp.mak.orig
	cp makes/amp3.mak makes/amp3.mak.orig
	cp makes/easydiag.mak makes/easydiag.mak.orig
	cp makes/editor.mak makes/editor.mak.orig
	cp makes/extra.mak makes/extra.mak.orig
	cp makes/inffd.mak makes/inffd.mak.orig
	cp makes/librhuti.mak makes/librhuti.mak.orig
	cp makes/libset.mak makes/libset.mak.orig
	cp makes/sdgcline.mak makes/sdgcline.mak.orig
	cp makes/settv.mak makes/settv.mak.orig
	cp makes/testeasy.mak makes/testeasy.mak.orig

	cp makes/rhide.env makes/rhide.env.orig	
	cp debian/rhide.env makes/rhide.env

	#cd makes; $(MAKE) force-patch
	cd makes; perl patchenv.pl
endif

	cd makes; $(MAKE) install
	mv debian/tmp/usr/doc/setedit/change.log.gz debian/tmp/usr/doc/setedit/changelog.gz
	rm -f debian/tmp/usr/bin/e
	dh_link usr/bin/setedit usr/bin/e

	touch build-stamp


clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp install-stamp
	
	# If the makes/editor.mak.orig file exists, then we have to restore
	# the original versions of the upstream build files modified by Debian.
ifneq ($(wildcard makes/editor.mak.orig),)
	mv mp3/libamp/libamp.mak.orig mp3/libamp/libamp.mak
	mv makes/amp3.mak.orig makes/amp3.mak
	mv makes/easydiag.mak.orig makes/easydiag.mak
	mv makes/editor.mak.orig makes/editor.mak
	mv makes/extra.mak.orig makes/extra.mak
	mv makes/inffd.mak.orig makes/inffd.mak
	mv makes/librhuti.mak.orig makes/librhuti.mak
	mv makes/libset.mak.orig makes/libset.mak
	mv makes/sdgcline.mak.orig makes/sdgcline.mak
	mv makes/settv.mak.orig makes/settv.mak
	mv makes/testeasy.mak.orig makes/testeasy.mak

	mv makes/rhide.env.orig	makes/rhide.env
endif

	cd makes; $(MAKE) clean-docs
	cd makes; $(MAKE) clean-o

	dh_clean


clean-debian:
	dh_testdir
	dh_testroot
	rm -f build-stamp install-stamp
	dh_clean


install: install-stamp
install-stamp: build-stamp
	dh_testdir
	dh_testroot
	#dh_clean -k  Not needed because the needed files already copied there and this would delete them!
	#dh_installdirs  Not needed because upstream source takes care to create the directories!

	# Add here commands to install the package into debian/tmp.
	#$(MAKE) install DESTDIR=`pwd`/debian/tmp  Already installed by build-stamp target!

	touch install-stamp


# 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_testversion 1.2.9
	dh_testdir
	dh_testroot
	dh_installdocs
#	dh_installexamples
	dh_installmenu
#	dh_installemacsen
#	dh_installinit
#	dh_installcron
# FIXME: It would be nice if I write a small manpage describing the editor...
#	dh_installmanpages
	dh_undocumented e.1 setedit.1
	dh_installchangelogs 
	dh_strip
	dh_compress
	dh_fixperms
	chmod 4755 debian/tmp/usr/bin/setedit
	dh_suidregister /usr/bin/setedit
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
#	dh_makeshlibs  I don't understand the purpose of this program! The dh_shlibdeps seems to do the same but better... I don't know...
	dh_md5sums
	dh_builddeb

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

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