#! /usr/bin/env perl

##**************************************************************
##
## Copyright (C) 1990-2007, Condor Team, Computer Sciences Department,
## University of Wisconsin-Madison, WI.
## 
## Licensed under the Apache License, Version 2.0 (the "License"); you
## may not use this file except in compliance with the License.  You may
## obtain a copy of the License at
## 
##    http://www.apache.org/licenses/LICENSE-2.0
## 
## Unless required by applicable law or agreed to in writing, software
## distributed under the License is distributed on an "AS IS" BASIS,
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
## See the License for the specific language governing permissions and
## limitations under the License.
##
##**************************************************************


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;
	if ( $ENV{GLOBUS_LOCATION} eq "" ) {
		$globus_path = "/opt/globus/bin";
	} else {
		$globus_path = $ENV{GLOBUS_LOCATION} . "/bin";
	}
	-r "$globus_path/grid-proxy-info" || die("Can't find grid-proxy-info");

	my $openssl_path = "/usr/bin";
	-r "$openssl_path/openssl" || die("Can't find openssl");

	# We want to use /usr/bin/openssl instead of Globus's openssl because
	# it's 0.9.7 instead of 0.9.6. The two versions produce different
	# names for the email attribute in X509 subject names. Current versions
    # of Condor use 0.9.7, so when the launcher creates a grid-mapfile for
    # the launched schedd to use, it wants to use 0.9.7-formatted subject
    # names.
	$ENV{PATH} = "$bin_path:$sbin_path:$openssl_path:$globus_path:$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');
	}
	print "Starting Condor-C VO Advertiser\n";
	if ( check( "condorc-vo-advertiser" ) == 0 ) {
		print "Skipping Condor-C VO Advertiser\n";
	} else {
		submit($glite_path,'condorc-vo-advertiser');
	}

	return 0;
}

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;
}
