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

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