# This PL file is used to install NMI framework distribution.
# Usage: perl Makefile.PL [prefix=<dir>]; make; make install

use ExtUtils::MakeMaker qw(WriteMakefile);
use misc::Version;

##
## Main Makefile stuff...
##
my $local = 'framework/local';
my $remote = 'framework/remote';

WriteMakefile(
	      NAME => 'nmi',
	      DISTNAME => 'nmi',
	      VERSION_FROM => 'misc/Version.pm',

          # It's not clear if this is really a supported API,
          # as such, but there's no other way to do it.
          depend => { 'create_distdir' => 'patchweb' },

 	      PMLIBDIRS => [$local],
  	      PM => {
		      "$local/nmi_prereqs.pm"          => '$(PREFIX)/lib/nmi_prereqs.pm',
		      "$local/nmi_condor_status.pm"    => '$(PREFIX)/lib/nmi_condor_status.pm',
		      "$local/NmiDateTime.pm"          => '$(PREFIX)/lib/NmiDateTime.pm',
		      "$local/NmiFileTransfer.pm"      => '$(PREFIX)/lib/NmiFileTransfer.pm',
		      "$local/NmiTools.pm"             => '$(PREFIX)/lib/NmiTools.pm',
		      "$local/ClassadTools.pm"         => '$(PREFIX)/lib/ClassadTools.pm',
		      "$local/NmiConf.pm.patched"      => '$(PREFIX)/lib/NmiConf.pm',
		      "$local/NmiUserLogReader.pm"     => '$(PREFIX)/lib/NmiUserLogReader.pm',
		      "$local/NmiDatabase.pm"          => '$(PREFIX)/lib/NmiDatabase.pm',
		      "$local/NmiDatabaseVariables.pm" => '$(PREFIX)/lib/NmiDatabaseVariables.pm',
		      'scripts/NmiStats.pm'            => '$(PREFIX)/lib/NmiStats.pm',
		  },
	      EXE_FILES => [
			    "$local/nmi_list_prereqs",
			    "$local/nmi_submit",
			    "$local/fetch.pl",
			    "$local/local_put.pl",
			    "$local/fetch_postscript.sh",
			    "$local/fetch_prescript.sh",
			    "$local/monitor_postscript.sh",
			    "$local/nmi_condor_status",
			    "$local/nmi_condor_q",
			    "$local/nmi_getfile",
			    "$local/nmi_gid2runid",
			    "$local/nmi_migrate_run",
			    "$local/nmi_pin",
			    "$local/nmi_putfile",
			    "$local/nmi_rm",
			    "$local/nmi_runid2condor",
			    "$local/nmi_runid2gid",
			    "$local/nmi_run_status",
			    "$local/nmi_testsforrun",
			    "$local/noop.sh",
			    "$local/logged-noop.pl",
			    "$local/notify.pl",
			    "$local/noxml_db_update.pl",
			    "$local/platform_job_postscript.sh",
			    "$local/platform_job_prescript.sh",
			    "$local/platform_post_postscript.sh",
			    "$local/platform_post_prescript.sh",
			    "$local/platform_pre_postscript.sh",
			    "$local/platform_pre_prescript.sh",
			    "$local/post_all_postscript.sh",
			    "$local/post_all_prescript.sh",
			    "$local/pre_all_postscript.sh",
			    "$local/pre_all_prescript.sh",
			    "$local/platform_put_prescript.sh",
			    "$local/platform_put_postscript.sh",
			    "$local/common_put_prescript.sh",
			    "$local/common_put_postscript.sh",
			    "$local/nmi_putattr",
			    "$local/nmi_getattr",
			    "$local/nmi_rundir",
			    "$local/nmi_resource_advertiser",
			    "$local/nmi_resubmit_run",
			    "$local/nmi_iod_admin",
			    "$remote/perl.bat",
			    "$remote/sh.bat",
			    "$remote/bash.bat",
			    "$remote/run.bat",
			    "$remote/task_loop.pl",
			    "$remote/put.pl",
			    "$remote/task_wrapper.sh",
			    'scripts/disk_cleaner.pl',
			    'scripts/nmi_usage_stats.pl',
			    'scripts/nmi_search_classads.pl',
			    'nmi_version',
			    'config.sh',
			    'config.csh',
],
);

sub MY::postamble {
  return <<'END';
nmi_version: misc/Version.pm
	@ echo '#!/bin/sh' > nmi_version
	@ echo echo $(VERSION) >> nmi_version
	@ chmod 0755 nmi_version

config.sh:
	@ echo export 'PATH=$$NMI_BIN:$(PREFIX)/bin:$$PATH' > config.sh
	@ echo >> config.sh
	@ echo export 'PERL5LIB=$$NMI_LIB:$(PREFIX)/lib:$$PERL5LIB' >> config.sh

config.csh:
	@ echo if \( ! $$\?PATH \) then > config.csh
	@ echo   setenv PATH "" >> config.csh
	@ echo endif >> config.csh
	@ echo if \( ! $$\?PERL5LIB \) then >> config.csh
	@ echo   setenv PERL5LIB "" >> config.csh
	@ echo endif >> config.csh
	@ echo if \( ! $$\?NMI_BIN \) then >> config.csh
	@ echo    echo \"ERROR: NMI_BIN is undefined. Aborting!\" >> config.csh
	@ echo    exit >> config.csh
	@ echo endif >> config.csh
	@ echo if \( ! $$\?NMI_LIB \) then >> config.csh
	@ echo    echo \"ERROR: NMI_LIB is undefined. Aborting!\" >> config.csh
	@ echo    exit >> config.csh
	@ echo endif >> config.csh
	@ echo >> config.csh
	@ echo setenv PATH \"$$\{NMI_BIN\}:$(PREFIX)/bin:$$\{PATH\}\" >> config.csh
	@ echo setenv PERL5LIB \"$$\{NMI_LIB\}:$(PREFIX)/lib:$$\{PERL5LIB\}\" >> config.csh

framework/local/NmiConf.pm.patched: framework/local/NmiConf.pm
	perl -lwpe 's!/nmi/etc/nmi.conf!$(PREFIX)/etc/nmi.conf!' framework/local/NmiConf.pm > framework/local/NmiConf.pm.patched

framework/local/nmi_submit: framework/local/create_dag.pl
	cp framework/local/create_dag.pl framework/local/nmi_submit

clean::
	rm -rf config.sh config.csh framework/local/NmiConf.pm.patched framework/local/nmi_submit nmi_version

.PHONY: patchlib

patchlib:
	perl -i -lwpe 's!"/nmi/lib"!"$(PREFIX)/lib"!' blib/script/*
	perl -i -lwpe 's!_XXX_BINDIR!$(PREFIX)/bin!' blib/script/monitor_postscript.sh

patchweb:
	perl -i -lwpe 's/^(\s*)define\("NMI_VERSION",[^\)]*\);.*$$/$$1define\("NMI_VERSION", "$(VERSION)"\);/' web/bin/main.php

all:: patchlib

links: install
	for i in bin lib share; do if [ -e /nmi/$$i ]; then unlink /nmi/$$i; ln -s $(PREFIX)/$$i /nmi/$$i; fi; done
	for i in condor_nmi_submit condor_build_submit.pl condor_test_submit.pl; do if [ -e $(PREFIX)/bin/$$i ]; then unlink $(PREFIX)/bin/$$i; fi; done
	for i in /nmi/scripts/condor/nmi_tools/condor_nmi_submit /nmi/scripts/condor/transition/condor_build_submit.pl /nmi/scripts/condor/transition/condor_test_submit.pl; do if [ -e $$i ]; then ln -s $$i $(PREFIX)/bin/; fi; done

	# Set up soft link for the master config file since the framework
	# will look for it under $(PREFIX)/etc.
	if [ ! -d $(PREFIX)/etc ]; then mkdir $(PREFIX)/etc; fi
	if [ -e $(PREFIX)/etc/nmi.conf ]; then unlink $(PREFIX)/etc/nmi.conf; fi
	ln -s /nmi/etc/nmi.conf $(PREFIX)/etc/nmi.conf

END
}

