#! /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.
##
##**************************************************************

use strict;
use warnings;

die "usage: $0 source dest" if ( $#ARGV < 0 );
my $Source = shift @ARGV;
my $Dest = "";
$Dest = shift @ARGV if ( $#ARGV >= 0 );

die "$Source does not exist" if ( ! -f $Source );

# Get versions
my %Versions;
open( CV, "../condor_tools/condor_version|" )
    or die "Can't run condor_version";
while( <CV> )
{
    chomp;
    if ( /^\$(\w+): .*\$$/ )
    {
	$Versions{$1} = $_;
    }
}
close( CV );

# Finally, do the filtering
open( SOURCE, $Source ) or die "Can't read $Source";
if ( $Dest ne "" )
{
    open( DEST, ">$Dest" ) or die "Can't write to $Dest";
}
else
{
    open( DEST, ">&STDOUT" ) or die "Can't copy stdout";
}
while( <SOURCE> )
{
    foreach my $key ( keys %Versions )
    {
	my $value = $Versions{$key};
	s/\$$key\$/$value/;
    }
    print DEST $_;
}
close( DEST );
close( SOURCE );
