#!/usr/bin/make -f


# JAA: I will prefix my comments with JAA.
#
# JAA: I don't understand very well many of what is here. They were in the template
# created by dh_make, they are in all packages that I looked at to find solutions
# to the problems that appeared while I was creating the package, and they are
# not making any harm. I didn't write any comment to the rules file of the
# package that is my homepage. So the comments that don't have the prefix JAA
# are here since the template was created.


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


# These are used for cross-compiling and for saving the configure script
# from having to guess our platform (since we know it already)
DEB_HOST_GNU_TYPE   ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)


CFLAGS = -Wall -g

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

# shared library versions, option 1
version=2.0.5
major=2

config.status: configure
	dh_testdir
	# Add here commands to configure the package.
	# JAA: This is the configure command that was successful for me when
	# building swarm with the traditional "./configure" "make" "make
	# install". Of course I replaced "/usr/local" with "/usr".
	./configure --prefix=/usr --sysconfdir=/usr/share/libswarm --infodir=/usr/share/info --disable-jar --without-jdkdir --with-pic --with-gnu-ld --enable-onelib --with-tclincludedir=/usr/include/tcl8.4/ --with-tclscriptdir=/usr/lib/tcl8.4/ --with-tkscriptdir=/usr/lib/tk8.4/ --with-tcl=/usr/lib/tcl8.4/ --with-tk=/usr/lib/tk8.4/


build: build-stamp
build-stamp:  config.status
	dh_testdir

	# Add here commands to compile the package.
	$(MAKE)

	touch $@

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp 
	# Add here commands to clean up after the build process.
	-$(MAKE) distclean
	# JAA: swarm's "make distclean" does not clean everything. The source
	# code tree should be back to what it was in the original .tar.gz. If I
	# do not delete these files manually, dpkg-buildpackage might refuse to
	# do its job complaining that there are "unrepresentable changes to the
	# source code".
	rm -f tools/findImp.o src/defobj/modulemap.elc src/getters.elc \
	  common.elc protocol.elc interface.elc libobjc/objc/mframe.h \
	  libobjc/doc/objc-features.info libobjc/compiler-info.h \
	  src/collections/collections.xm src/collections/types.h \
	  src/collections/classes.h src/defobj/modulemap.c src/defobj/modulemap \
	  src/activity/activity.xm src/activity/types.h src/activity/classes.h \
	  src/tkobjc/analysis_tcl.x src/tkobjc/simtools_tcl.x 
ifneq "$(wildcard /usr/share/misc/config.sub)" ""
	cp -f /usr/share/misc/config.sub config.sub
endif
ifneq "$(wildcard /usr/share/misc/config.guess)" ""
	cp -f /usr/share/misc/config.guess config.guess
endif
	dh_clean 

install: build
	dh_testdir
	dh_testroot
	dh_clean -k 
	dh_installdirs
	$(MAKE) DESTDIR=$(CURDIR)/debian/libswarm install
	# JAA: The make command above is the "make install" that we normally
	# type while compiling from source, but the package will be installed at
	# "swarm-2.2.4/debian/libswarm/" instead of "/". However, this isn't
	# enough because we need to create three different packages from the
	# same source code. The remaining commands create the other two packages.
	# First we will create a directory tree to libswarm-dev and move to it
	# header files and everything else that is necessary to build but not
	# to run swarm applications.
	mkdir -p debian/libswarm-dev/usr/share/man/man1
	mkdir -p debian/libswarm-dev/usr/share/doc/libswarm-dev
	mkdir -p debian/libswarm-dev/usr/lib/swarm
	mkdir -p debian/libswarm-dbg/usr/lib/debug/usr/lib/swarm
	# JAA: Someone wrote an e-mail to me and told me that Debian libraries
	# usually are split in three packages lib*, lib*-dev, and
	# lib*-dbg. I had already created the first two, but the -dbg (with
	# debugging symbols) was missing. Then, I downloaded a source package,
	# looked at its rules file and found a command similar to the one below: 
	objcopy --only-keep-debug debian/libswarm/usr/lib/swarm/libswarm.so.0.0.0 \
	  debian/libswarm-dbg/usr/lib/debug/usr/lib/swarm/libswarm.so.0.0.0
	cp -p debian/libswarm-dev.README debian/libswarm-dev/usr/share/doc/libswarm-dev/README.Debian
	mv debian/libswarm/usr/include debian/libswarm/usr/bin debian/libswarm-dev/usr
	mv debian/libswarm/usr/share/libswarm debian/libswarm/usr/share/info debian/libswarm-dev/usr/share/
	mv debian/libswarm/usr/share/swarm/* debian/libswarm-dev/usr/share/libswarm
	# JAA: One directory of libswarm became empty after its content was
	# moved to libswarm-dev. Hence, we can delete it.
	rmdir debian/libswarm/usr/share/swarm
	mv debian/libswarm/usr/lib/swarm/libswarm.*a debian/libswarm-dev/usr/lib/swarm/
	# JAA: I had to write a man page for swarm because lintian complains if
	# there is an executable in /usr/bin without the corresponding man page.
	cat debian/swarm.1 | gzip --best > debian/libswarm-dev/usr/share/man/man1/swarm.1.gz
	( cd debian/libswarm-dev/usr/share/man/man1 ; \
	  ln -s swarm.1.gz libtool-swarm.1.gz ; \
	  ln -s swarm.1.gz m2h.1.gz ; \
	  ln -s swarm.1.gz make-h2x.1.gz ; \
	  ln -s swarm.1.gz print-hdf5.1.gz )




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

# Build architecture-dependent files here.
# JAA: Basically I didn't change anything important in the rule below. Because I
# installed the man page manually in libswarm-dev tree, I commented out
# dh_installman. The prefix "dh_" mean "Debian helper application to". They
# automatically do various tasks to assure that the package will follow the
# Debian policy. You can see the man page of each one for details.
binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installchangelogs ChangeLog
	dh_installdocs
#	dh_installexamples
#	dh_install
#	dh_installman
#	dh_link
	dh_strip
	dh_compress
	dh_fixperms
#	dh_makeshlibs
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

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