#! /usr/bin/env perl

##/***************************Copyright-DO-NOT-REMOVE-THIS-LINE**
##
## Condor Software Copyright Notice
## Copyright (C) 1990-2006, Condor Team, Computer Sciences Department,
## University of Wisconsin-Madison, WI.
##
## This source code is covered by the Condor Public License, which can
## be found in the accompanying LICENSE.TXT file, or online at
## www.condorproject.org.
##
## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
## AND THE UNIVERSITY OF WISCONSIN-MADISON "AS IS" AND ANY EXPRESS OR
## IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
## WARRANTIES OF MERCHANTABILITY, OF SATISFACTORY QUALITY, AND FITNESS
## FOR A PARTICULAR PURPOSE OR USE ARE DISCLAIMED. THE COPYRIGHT
## HOLDERS AND CONTRIBUTORS AND THE UNIVERSITY OF WISCONSIN-MADISON
## MAKE NO MAKE NO REPRESENTATION THAT THE SOFTWARE, MODIFICATIONS,
## ENHANCEMENTS OR DERIVATIVE WORKS THEREOF, WILL NOT INFRINGE ANY
## PATENT, COPYRIGHT, TRADEMARK, TRADE SECRET OR OTHER PROPRIETARY
## RIGHT.
##
##***************************Copyright-DO-NOT-REMOVE-THIS-LINE**/

BEGIN { $^W = 1 }
use strict;
use FindBin;

my $DEBUG_LEVEL;
my $LOG_DIR;

chomp( $DEBUG_LEVEL = int ( `condor_config_val GLITE_CONDORC_DEBUG_LEVEL` ) );
if ( $? != 0 ) {
	$DEBUG_LEVEL = 10;
}

chomp( $LOG_DIR = `condor_config_val GLITE_CONDORC_LOG_DIR` );
if ( $? != 0 || ! -d $LOG_DIR ) {
	$LOG_DIR = "/tmp";
}

exit main();

sub main {
	my $glite_path = $FindBin::Bin;
	my $bin_path = $glite_path;
	$bin_path =~ s|/libexec/glite/*|/bin|;
	my $sbin_path = $bin_path;
	$sbin_path =~ s|/bin/*|/sbin|;

	my $globus_path = $ENV{GLOBUS_LOCATION};
	if ( $globus_path eq "" ) {
		$globus_path = "/opt/globus";
	}
	-r "$globus_path/bin/openssl" || die("Can't find openssl");
	
	$ENV{PATH} = "$bin_path:$sbin_path:$globus_path/bin:$ENV{PATH}";

	print "Starting Condor-C Launcher\n";
	if ( check( "condorc-launcher" ) == 0 ) {
		print "Skipping Condor-C Launcher\n";
	} else {
		submit($glite_path,'condorc-launcher');
	}
	print "Starting Condor-C Advertiser\n";
	if ( check( "condorc-advertiser" ) == 0 ) {
		print "Skipping Condor-C Advertiser\n";
	} else {
		submit($glite_path,'condorc-advertiser');
	}
	print "Starting Condor-C Authorizer\n";
	if ( check( "condorc-authorizer" ) == 0 ) {
		print "Skipping Condor-C Authorizer\n";
	} else {
		submit($glite_path,'condorc-authorizer');
	}
}

sub check {
	my($executable) = @_;
	my @jobs = `condor_q -format '\%s\n' Cmd `;
	if ( $? != 0 ) {
		print "condor_q failed!\n";
		return 0;
	}
	foreach ( @jobs ) {
		if ( /$executable\n/ ) {
			print "$executable already submitted!\n";
			return 0;
		}
	}
	return 1;
}

sub submit {
	my($bin_path,$executable) = @_;
	my $fullpath = "$bin_path/$executable";
	if( ! -e $fullpath ) {
		print STDERR "Warning: $fullpath does not exist.\n";
	}
	if( ! -x $fullpath ) {
		print STDERR "Warning: $fullpath is not executable.\n";
	}
	local *OUT;
	open(OUT, "|condor_submit")
		or die "FATAL ERROR: unable to launch condor_submit";
	print OUT <<END;
executable=$fullpath
universe=scheduler
output=/$LOG_DIR/$executable.$>.$$.out
error=/$LOG_DIR/$executable.$>.$$.err
getenv=true
#environment=PATH=$ENV{PATH};CONDOR_CONFIG=$ENV{CONDOR_CONFIG};GLITE_HOST_CERT=$ENV{GLITE_HOST_CERT};GLITE_HOST_KEY=$ENV{GLITE_HOST_KEY};LD_LIBRARY_PATH=$ENV{LD_LIBRARY_PATH}
notification=never
on_exit_remove=false
queue
END
	close OUT;
}
