#!/usr/bin/perl -w
# $Id: munge_config_file.pl,v 1.1.2.1 2006/11/11 12:52:05 zak Exp $

use strict;

my $SITE = $ENV{"SITE"};
my $FQDN = $ENV{"FQDN"};
my $MIRRORFQDN = $ENV{"MIRRORFQDN"};
my $SECUREFQDN = $ENV{"SECUREFQDN"};
my $USER = $ENV{"USER"};
my $DBNAME = $ENV{"DBNAME"};
my $DBUSER = $ENV{"DBUSER"};
my $DBPASS = $ENV{"DBPASS"};
my $PRODUCTIONDIR = $ENV{"PRODUCTIONDIR"};
my @ALIASES = defined $ENV{"ALIASES"} ? split ' ', $ENV{"ALIASES"} : ();

while (my $line = <>)
{
    $line =~ s/\$SITE\b/$SITE/g;
    $line =~ s/\$FQDN\b/$FQDN/g;
    $line =~ s/\$MIRRORFQDN\b/$MIRRORFQDN/g;
    $line =~ s/\$SECUREFQDN\b/$SECUREFQDN/g;
    $line =~ s/\$USER\b/$USER/g;
    $line =~ s/\$DBNAME\b/$DBNAME/g;
    $line =~ s/\$DBUSER\b/$DBUSER/g;
    $line =~ s/\$DBPASS\b/$DBPASS/g;
    $line =~ s/\$PRODUCTIONDIR\b/$PRODUCTIONDIR/g;
      
    if ($line =~ /\$ALIAS\b/)
    {
        for my $alias (@ALIASES)
	{
	    my $out = $line;
	    $out =~ s/\$ALIAS\b/$alias/g;
	    print $out;
        }
    }
    else
    {
	print $line;
    }
}
