#!/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;

	# Seconds between updates.
my $SLEEP_TIME = 300;
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";
}

my $gridmap_file;
my $config_file;
my $temp_file;

exit main();

sub main {
	my $mod_time = 0;
	# TODO determine this dynamically
	$gridmap_file = "/etc/grid-security/grid-mapfile";
	$config_file = "/var/local/condor/condor_config.local";
	$temp_file = "/var/local/condor/condor_config.local.tmp";
	while(1) {
		debug_print(10, "#################\n");

		my $new_mod_time = (stat($gridmap_file))[9];
		if ( $new_mod_time > $mod_time ) {
			debug_print(10, "grid-mapfile has changed, updating config file\n");

			update_config();
			$mod_time = $new_mod_time;
		} else {
			debug_print(10, "No change in grid-mapfile\n");
		}

		sleep($SLEEP_TIME);
	}
}

sub update_config {
	my $subject;

	open( IN, "<$config_file" ) || die( "Can't open $config_file" );
	open( OUT, ">$temp_file" ) || die( "Can't open $temp_file" );
	open( GMF, "<$gridmap_file" ) || die( "Can't open $gridmap_file" );

	while( <IN> ) {
		if ( /GSI_DAEMON_NAME/ ) {
			# just skip it
		} else {
			print OUT $_;
		}
	}
	close( IN );

	print OUT "GSI_DAEMON_NAME =\n";

	while( <GMF> ) {
		chomp( $subject = $_ );
		#$subject =~ s/\"\([^\"]\)\".*/\1/g;
		$subject =~ s/"//;
		$subject =~ s/".*//;
		if ( $subject =~ /.+/ ) {
			print OUT "GSI_DAEMON_NAME = \$(GSI_DAEMON_NAME),$subject\n";
		}
		#print "Subject = $subject\n";
	}

	close( GMF );
	close( OUT );

	`mv $temp_file $config_file`;

	`condor_reconfig`;

}

sub debug_print {
	my($level) = shift;
	if($level > $DEBUG_LEVEL) { return; }
	print tersedate();
	print " ";
	print @_;
}

sub tersedate {
	my ($sec,$min,$hour,$mday,$mon,$year) = localtime(time);
	return sprintf "%04d-%02d-%02d %02d:%02d:%02d",
	 	$year + 1900, $mon + 1, $mday, $hour, $min, $sec;
}
