#!/usr/bin/make -f
# Sample debian/rules that uses debhelper.
# GNU copyright 1997 to 1999 by Joey Hess.

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

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

# some changed directories must be reflected in the config file
export CONDOR_CONFIGTPL = $(CURDIR)/debian/condor/usr/share/doc/condor/examples-conf/condor_config.debian

ifneq (,$(findstring debug,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -g
endif
ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
	INSTALL_PROGRAM += -s
endif

configure: configure-stamp

configure-stamp:
	dh_testdir
#	Add here commands to configure the package.

	( cd src && ./build_init && ./configure )

	touch configure-stamp


build: build-stamp

build-stamp: configure-stamp 
	dh_testdir

	-$(MAKE) -C src release

	touch build-stamp

clean:
	debconf-updatepo
	dh_testdir
	dh_testroot
	rm -f build-stamp configure-stamp

	# Add here commands to clean up after the build process.
	-$(MAKE) -C src clean
	rm -rf externals/{build,triggers,install}

	dh_clean

install: build
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs
	dh_installdocs
	dh_installexamples

	install -d $(CURDIR)/debian/condor/usr/
	cp -a src/release_dir/{etc,include,src,lib,libexec,bin,sbin,man} $(CURDIR)/debian/condor/usr/

	mv  $(CURDIR)/debian/condor/usr/etc/examples \
	    $(CURDIR)/debian/condor/usr/share/doc/condor/examples-conf
	cp  $(CURDIR)/debian/condor/usr/share/doc/condor/examples-conf/condor_config.generic \
	    $(CONDOR_CONFIGTPL)
	echo MPI_CONDOR_RSH_PATH = /usr/lib/condor|cat $(CONDOR_CONFIGTPL) - > $(CONDOR_CONFIGTPL).new
	echo STORK_MODULE_DIR = /usr/lib/condor|cat $(CONDOR_CONFIGTPL).new - > $(CONDOR_CONFIGTPL)
	sed -e "s,\@GT3_INSTALL\@,/usr/lib/condor/gt3," \
            < $(CURDIR)/debian/condor/usr/lib/gt3/server-config.wsdd.in \
            > $(CURDIR)/debian/condor/usr/lib/gt3/server-config.wsdd
	sed -e "s,\@GT3_INSTALL\@,/usr/lib/condor/gt3," \
            < $(CURDIR)/debian/condor/usr/lib/gt3/local-server-config.wsdd.in \
            > $(CURDIR)/debian/condor/usr/lib/gt3/local-server-config.wsdd

	rm -rf     $(CURDIR)/debian/condor/usr/etc
	rm -rf     $(CURDIR)/debian/condor/usr/src

	install -d $(CURDIR)/debian/condor/usr/share
	install -d $(CURDIR)/debian/condor/usr/lib/condor
	install -d $(CURDIR)/debian/condor/usr/share/java
	mv  $(CURDIR)/debian/condor/usr/lib/*.{class,jar}  \
	    $(CURDIR)/debian/condor/usr/share/java/
	-mv  $(CURDIR)/debian/condor/usr/lib/* \
	    $(CURDIR)/debian/condor/usr/lib/condor/
	mv  $(CURDIR)/debian/condor/usr/man  \
	    $(CURDIR)/debian/condor/usr/share/man
	mv  $(CURDIR)/debian/condor/usr/libexec/rsh \
	    $(CURDIR)/debian/condor/usr/lib/condor/
	chmod ugo-x $(CURDIR)/debian/condor/usr/share/java/*	
	rm -rf $(CURDIR)/debian/condor/usr/libexec
	rm -rf $(CURDIR)/debian/condor/usr/lib/condor/real-ld
	ln -s  ld $(CURDIR)/debian/condor/usr/lib/condor/real-ld

	rm -rf $(CURDIR)/debian/condor/usr/bin/condor_checkpoint
	ln -s  condor $(CURDIR)/debian/condor/usr/bin/condor_checkpoint
	rm -rf $(CURDIR)/debian/condor/usr/bin/condor_vacate
	ln -s  condor $(CURDIR)/debian/condor/usr/bin/condor_vacate
	rm -rf $(CURDIR)/debian/condor/usr/bin/condor_reschedule
	ln -s  condor $(CURDIR)/debian/condor/usr/bin/condor_reschedule

	rm -rf $(CURDIR)/debian/condor/usr/bin/condor_hold
	ln -s  condor_rm $(CURDIR)/debian/condor/usr/bin/condor_hold
	rm -rf $(CURDIR)/debian/condor/usr/bin/condor_release
	ln -s  condor_rm $(CURDIR)/debian/condor/usr/bin/condor_release
	rm -rf $(CURDIR)/debian/condor/usr/bin/condor_vacate_job
	ln -s  condor_rm $(CURDIR)/debian/condor/usr/bin/condor_vacate_job

	rm -rf $(CURDIR)/debian/condor/usr/sbin/condor_on
	ln -s  condor $(CURDIR)/debian/condor/usr/sbin/condor_on
	rm -rf $(CURDIR)/debian/condor/usr/sbin/condor_off
	ln -s  condor $(CURDIR)/debian/condor/usr/sbin/condor_off
	rm -rf $(CURDIR)/debian/condor/usr/sbin/condor_checkpoint
	ln -s  condor $(CURDIR)/debian/condor/usr/sbin/condor_checkpoint
	rm -rf $(CURDIR)/debian/condor/usr/sbin/condor_vacate
	ln -s  condor $(CURDIR)/debian/condor/usr/sbin/condor_vacate
	rm -rf $(CURDIR)/debian/condor/usr/sbin/condor_restart
	ln -s  condor $(CURDIR)/debian/condor/usr/sbin/condor_restart
	rm -rf $(CURDIR)/debian/condor/usr/sbin/condor_reconfig
	ln -s  condor $(CURDIR)/debian/condor/usr/sbin/condor_reconfig
	rm -rf $(CURDIR)/debian/condor/usr/sbin/condor_reconfig_schedd
	ln -s  condor $(CURDIR)/debian/condor/usr/sbin/condor_reconfig_schedd
	rm -rf $(CURDIR)/debian/condor/usr/sbin/condor_reschedule
	ln -s  condor $(CURDIR)/debian/condor/usr/sbin/condor_reschedule
	rm -rf $(CURDIR)/debian/condor/usr/sbin/condor_master_off
	ln -s  condor $(CURDIR)/debian/condor/usr/sbin/condor_master_off
	rm -rf $(CURDIR)/debian/condor/usr/sbin/condor_gridshell
	ln -s  condor_starter $(CURDIR)/debian/condor/usr/sbin/condor_gridshell

	sed -e "s,^[ \t]*LIB[ \t]*=.*,LIB = /usr/lib/condor," \
	    -e "s,^[ \t]*RELEASE_DIR[ \t]*=.*,RELEASE_DIR = /usr," \
	    -e "s,^[ \t]*LOCAL_DIR[ \t]*=.*,LOCAL_DIR = /var," \
	    -e "s,^[ \t]*LOCAL_CONFIG_FILE[ \t]*=.*,#LOCAL_CONFIG_FILE =," \
	    -e "s,^[ \t]*EXECUTE[ \t]*=.*,EXECUTE = /var/spool/condor/execute," \
	    -e "s,^[ \t]*LOCK[ \t]*=.*,LOCK = /var/lock," \
	    -e "s,^[ \t]*LOG[ \t]*=.*,LOG = /var/log/condor," \
	    -e "s,^[ \t]*SPOOL[ \t]*=.*,SPOOL = /var/spool/condor/spool," \
	    -e "s,^[ \t]*HISTORY[ \t]*=.*,HISTORY = /var/spool/condor/history," \
	    -e "s,^[ \t]*CRED_STORE_DIR[ \t]*=.*,CRED_STORE_DIR = /var/spool/condor/cred," \
	    < $(CONDOR_CONFIGTPL) > $(CONDOR_CONFIGTPL).tmp
	mv $(CONDOR_CONFIGTPL).tmp $(CONDOR_CONFIGTPL)

# 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_testdir
	dh_testroot
	dh_installdebconf	
#	dh_installlogrotate
	dh_installinit
#	dh_installcron
	dh_installman
#	dh_installinfo
#	dh_undocumented
	dh_installchangelogs 
	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 configure
