#! /usr/bin/perl -w
##**************************************************************
##
## 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.
##
##**************************************************************


##*****************************************************************
## Takes a N configuration files and binds them together 
## by creating a third configuration that has the other files
## as local configuration files.
## This technique has two nice properties:
## 1. The global configuration remains pristine and unchanged
## 2. It allows a local config to be specified at a later time (in 
##    my case I needed this because the global config won't know 
##    the location of the local configs until they are both installed
##*****************************************************************

#***
# Uses
#***
use strict;
use Getopt::Long;
use File::Spec;

#***
# Constant Static Variables
#***
my $USAGE = "Usage: condor_config_bind <-o output_file_name>\n".
    "<condor_config_file1> <condor_config_file2>\n".
    "[condor_config_file3 condor_config_file4 ...]\n".
    "[-help]\n";

my $BINDING_COMMENT = 
    "## This file was created by condor_config_bind to bind a disparate \n".
    "## set of configuration files into a single configuration entity\n";

my $LOCAL_CONFIG_FILE_ATTR = 'LOCAL_CONFIG_FILE';

#***
# Non-constant Static Variables
#***
my $output_file = 0;
my @config_files = ();
my $help_flag = 0;

#***
# Main Function
#***
GetOptions('o=s'=>\$output_file,
	   'help'=>\$help_flag,
	   );

# Process the command-line arguments
die $USAGE if( @ARGV < 2 || $help_flag );
@config_files = @ARGV;

# Open the output file
open BINDING_CONFIG, "> $output_file"
    or die "Failed to open $output_file: $!";

# Append the comment line
print BINDING_CONFIG $BINDING_COMMENT;

# Append the local configuration attribute
print BINDING_CONFIG $LOCAL_CONFIG_FILE_ATTR, ' = \\', "\n";

# For each of the configuration files 
# convert to file name to absolute path
# and append it to the attribute

# Prime the pump
my $primer = shift @config_files;
$primer = File::Spec->rel2abs($primer);
print BINDING_CONFIG $primer;

foreach(@config_files){
    $_ = File::Spec->rel2abs($_);
    print BINDING_CONFIG ',\\', "\n", $_;
}

# Close the file
close BINDING_CONFIG;
