#!/usr/bin/make -f
#
# This file was originally written by Joey Hess and Craig Small.
# As a special exception, when this file is copied by dh-make into a
# dh-make output file, you may use that output file without restriction.
# This special exception was added by Craig Small in version 0.37 of dh-make.
#
# Modified to make a template file for a multi-binary package with separated
# build-arch and build-indep targets  by Bill Allombert 2001

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

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

# 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)
DEB_HOST_MULTIARCH  ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

CFLAGS += -Wall

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

PAM_DIR=/lib/security

OPTIONS = CFLAGS="$(CFLAGS)" LDFLAGS="-Wl,-z,defs" \
	--host=$(DEB_HOST_GNU_TYPE) --build=$(DEB_BUILD_GNU_TYPE) \
	--prefix=/usr --mandir=\$${prefix}/share/man \
	--infodir=\$${prefix}/share/info \
	--disable-static \
	--enable-syslog \
	--enable-rpp --with-rcp=scp \
	--with-default-server=torqueserver \
	--with-pam=$(PAM_DIR) \
	--with-sendmail=/usr/sbin/sendmail \
	--with-server-name-file=/etc/torque/server_name \
        --disable-gcc-warnings


configure: configure-stamp
configure-stamp:
	dh_testdir
	dh_autoreconf
	ln -sf $(CURDIR) /tmp/torque-src
	mkdir -p debian/build/with-x
	mkdir -p debian/build/without-x
	cd debian/build/with-x && /tmp/torque-src/configure $(OPTIONS) \
		--srcdir /tmp/torque-src \
		--with-tcl=/usr/lib/$(DEB_HOST_MULTIARCH)/tcl8.5 --with-tk=/usr/lib/$(DEB_HOST_MULTIARCH)/tk8.5 \
		--disable-server --disable-mom
	cd debian/build/without-x && /tmp/torque-src/configure $(OPTIONS) \
		--srcdir /tmp/torque-src \
		--without-tcl --without-tclx --without-tk
	touch $@

#Architecture 
build: build-arch

build-arch: build-arch-stamp
build-arch-stamp: configure
	$(MAKE) -C debian/build/with-x
	$(MAKE) -C debian/build/without-x
	touch $@

clean:
	dh_testdir
	dh_testroot
	rm -f build-arch-stamp build-indep-stamp configure-stamp
	rm -rf debian/build
	dh_autoreconf_clean
	dh_clean
	rm -f torque-2.4.16


install: build
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs

	mkdir -p $(CURDIR)/debian/torque-common/var/spool/torque/
	ln -s /etc/torque/server_name $(CURDIR)/debian/torque-common/var/spool/torque/server_name

	$(MAKE) -C debian/build/without-x install_lib DESTDIR=$(CURDIR)/debian/libtorque2
	-mv $(CURDIR)/debian/libtorque2/$(PAM_DIR)/pam_pbssimpleauth.so $(CURDIR)/debian/torque-pam/$(PAM_DIR)/pam_pbssimpleauth.so
	rm -f $(CURDIR)/debian/libtorque2/usr/lib/libtorque.a
	rm -f $(CURDIR)/debian/libtorque2/usr/lib/libtorque.la
	rm -f $(CURDIR)/debian/libtorque2/usr/lib/libtorque.so

	$(MAKE) -C debian/build/without-x install_devel DESTDIR=$(CURDIR)/debian/libtorque2-dev
	rm -f $(CURDIR)/debian/libtorque2-dev/usr/lib/libtorque.so.*

	$(MAKE) -C debian/build/without-x install_server DESTDIR=$(CURDIR)/debian/torque-server
	chrpath --delete $(CURDIR)/debian/torque-server/usr/sbin/pbs_server
	
	[ -d $(CURDIR)/debian/torque-scheduler/var/spool/torque/sched_priv ] && \
		rm -r $(CURDIR)/debian/torque-scheduler/var/spool/torque/sched_priv
	mv $(CURDIR)/debian/torque-server/usr/sbin/qschedd $(CURDIR)/debian/torque-server/usr/sbin/pbs_sched \
	   $(CURDIR)/debian/torque-scheduler/usr/sbin/
	chrpath --delete $(CURDIR)/debian/torque-scheduler/usr/sbin/pbs_sched \
			 $(CURDIR)/debian/torque-scheduler/usr/sbin/qschedd

	mv $(CURDIR)/debian/torque-server/var/spool/torque/sched_priv \
	   $(CURDIR)/debian/torque-scheduler/var/spool/torque/
	# redundant with server package -> mv to -common 
	if [ -r $(CURDIR)/debian/torque-server/var/spool/torque/pbs_environment ]; then \
		mv $(CURDIR)/debian/torque-server/var/spool/torque/pbs_environment \
		      $(CURDIR)/debian/torque-common/var/spool/torque/pbs_environment; \
	fi
	echo "Removing the directory /usr/lib from server package, which contains the following files: "
	-find $(CURDIR)/debian/torque-server/usr/lib
	rm -rf $(CURDIR)/debian/torque-server/usr/lib

	$(MAKE) -C debian/build/without-x install_pam DESTDIR=$(CURDIR)/debian/torque-pam
	rm -rf $(CURDIR)/debian/torque-pam/$(PAM_DIR)/*.la

	$(MAKE) -C debian/build/without-x install_clients DESTDIR=$(CURDIR)/debian/torque-client
	rm -rf $(CURDIR)/debian/torque-client/usr/lib
	chrpath --delete `ls $(CURDIR)/debian/torque-client/usr/bin/* | egrep -v '/(nqs2pbs|xpbs)' `
	chrpath --delete $(CURDIR)/debian/torque-client/usr/sbin/pbs_iff
	cp $(CURDIR)/contrib/pbstop $(CURDIR)/debian/torque-client/usr/bin
	chmod 755 $(CURDIR)/debian/torque-client/usr/bin/pbstop
	chrpath --delete $(CURDIR)/debian/torque-client/usr/sbin/momctl
	rm -f $(CURDIR)/debian/torque-client/var/spool/torque/server_name

	$(MAKE) -C debian/build/with-x install_clients DESTDIR=$(CURDIR)/debian/torque-client-x11
	$(MAKE) -C debian/build/with-x install_gui DESTDIR=$(CURDIR)/debian/torque-client-x11
	rm -rf $(CURDIR)/debian/torque-client-x11/usr/lib/libtorque.*
	rm -f $(CURDIR)/debian/torque-client-x11/var/spool/torque/server_name
	chrpath --delete `ls $(CURDIR)/debian/torque-client-x11/usr/bin/* | egrep -v '/(nqs2pbs|xpbs)'`
	chrpath --delete $(CURDIR)/debian/torque-client-x11/usr/lib/xpbs/bin/xpbs_datadump
	chrpath --delete $(CURDIR)/debian/torque-client-x11/usr/lib/xpbs/bin/xpbs_scriptload
	chrpath --delete $(CURDIR)/debian/torque-client-x11/usr/sbin/momctl
	chrpath --delete $(CURDIR)/debian/torque-client-x11/usr/sbin/pbs_iff
	cp $(CURDIR)/contrib/pbstop $(CURDIR)/debian/torque-client-x11/usr/bin
	chmod 755 $(CURDIR)/debian/torque-client/usr/bin/pbstop

	$(MAKE) -C debian/build/without-x install_mom DESTDIR=$(CURDIR)/debian/torque-mom
	rm -rf $(CURDIR)/debian/torque-mom/usr/lib $(CURDIR)/debian/torque-mom/var/spool/torque/server_name $(CURDIR)/debian/torque-mom/etc/torque/server_name
	rm -f $(CURDIR)/debian/torque-mom/usr/sbin/momctl
	chrpath --delete $(CURDIR)/debian/torque-mom/usr/sbin/pbs_demux
	chrpath --delete $(CURDIR)/debian/torque-mom/usr/sbin/pbs_mom

	# redundant with server package -> mv to -common 
	if [ -r $(CURDIR)/debian/torque-mom/var/spool/torque/pbs_environment ]; then \
		mv $(CURDIR)/debian/torque-mom/var/spool/torque/pbs_environment \
		      $(CURDIR)/debian/torque-common/var/spool/torque/pbs_environment; \
	fi

	if [ ! -r $(CURDIR)/debian/torque-common/var/spool/torque/pbs_environment ]; then \
	        touch $(CURDIR)/debian/torque-common/var/spool/torque/pbs_environment; \
	fi

        #  mv /etc/torque/server_name to torque-common
	if [ -r $(CURDIR)/debian/torque-client-x11/etc/torque/server_name ]; then \
                mkdir -p $(CURDIR)/debian/torque-common/etc/torque/; \
                mv $(CURDIR)/debian/torque-client-x11/etc/torque/server_name \
                      $(CURDIR)/debian/torque-common/etc/torque/server_name; \
                rm -rf $(CURDIR)/debian/torque-client-x11/etc/; \
	fi
	if [ -r $(CURDIR)/debian/torque-client/etc/torque/server_name ]; then \
                rm -f $(CURDIR)/debian/torque-client/etc/torque/server_name; \
                rm -rf $(CURDIR)/debian/torque-client/etc/; \
	fi


	$(MAKE) -C debian/build/without-x install_doc DESTDIR=$(CURDIR)/debian/tmp

	dh_install


binary-arch: install
	dh_testdir
	dh_testroot
	dh_installchangelogs CHANGELOG
	dh_installdocs
	dh_installexamples
#	dh_installmenu
#	dh_installdebconf	
#	dh_installlogrotate	
	dh_installinit
#	dh_installcron
#	dh_installinfo
	dh_lintian
	dh_installman
	dh_link
	dh_strip
	dh_compress 
	dh_fixperms -X pbs_iff -X /var/spool/torque/spool -X /var/spool/torque/undelivered
	dh_makeshlibs -p libtorque2 -V
	dh_installdeb
	dh_perl
	dh_shlibdeps -l$(CURDIR)/debian/libtorque2/usr/lib/ -L libtorque2
	dh_gencontrol
	dh_md5sums
	dh_builddeb


binary: binary-arch

# Extract upstream version from the current Debian changelog
UVERSION := $(shell dpkg-parsechangelog | mawk '/^Version:/ { sub("\+.*", "", $$2); print $$2 }')
get-orig-source:
	dh_testdir
	#uscan --force-download
	wget http://www.clusterresources.com/downloads/torque/torque-$(UVERSION).tar.gz -O ../torque_$(UVERSION).orig.tar.gz
	gunzip -dc ../torque_$(UVERSION).orig.tar.gz > ../torque_$(UVERSION)+dfsg.orig.tar
	tar --delete -f ../torque_$(UVERSION)+dfsg.orig.tar torque-$(UVERSION)/doc/admin_guide.ps
	tar --delete -f ../torque_$(UVERSION)+dfsg.orig.tar torque-$(UVERSION)/doc/v2_2_ids.pdf torque-$(UVERSION)/doc/v2_2_ers.pdf
	gzip --best ../torque_$(UVERSION)+dfsg.orig.tar

.PHONY: build clean binary-indep binary-arch binary install install-indep install-arch get-orig-source
