#! /usr/bin/perl

# GPL license, Copyright (c) 2006 by Nokia Corporation                       
#                                                                            
# Authors:                                                                   
#      Michael Dominic K. <michael.kostrzewa@nokia.com>
#                                                                            
# This program is free software; you can redistribute it and/or modify it    
# under the terms of the GNU General Public License as published by the      
# Free Software Foundation, version 2.                                                                   
#                                                                            
# This program is distributed in the hope that it will be useful, but        
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License   
# for more details.                                                          
#                                                                            
# You should have received a copy of the GNU General Public License along    
# with this program; if not, write to the Free Software Foundation, Inc., 59 
# Temple Place - Suite 330, Boston, MA 02111-1307, USA.                      

use File::Basename;

sub show_banner 
{
        print "Substitution tool by Michael Dominic K.\n";
        print "Copyright 2006 by Nokia Corporation.\n\n";
}

sub show_usage 
{
        my $program_name = basename($0);
        print "Usage: $program_name <layout> <template> <input file> <output file>\n\n";
        print "This tool will process the supplied input file and substitute\n";
        print "the color values with real colors fetched from the layout.\n\n";
}

sub do_substitutions
{
        my $line = $_[0];
        foreach $pat (keys(%substitutions))
        {
                $subst = $substitutions{$pat};
                $line =~ s/$pat/$subst/g;
        }
        return $line;
}

sub process_file
{
        my $file_name = "$_[0]";
        my $file;

        if (not open ($file, "< $file_name")) {
                print STDERR "ERROR: Failed to open \"$file_name\" rc file.\n";
                return 0;
        }
    
        while (<$file>)
        {
                print TARGET do_substitutions ($_);
        }

        close ($file);
        return 1;
}

# Check if we have enough arguments
if ($#ARGV + 1 < 4) {
        show_banner;
        show_usage ();
        print STDERR "Not enough arguments specified.\n";
        exit 128;
}

%substitutions = 
    ( qw{@ThemeName@} => "ThemeName" );

# Get the colors
print "Fetching colors...\n";
$eval_value =`hildon-theme-colourizer $ARGV[0] $ARGV[1]`;
if ($? != 0) {
        print STDERR "ERROR: Could not fetch colors\n";
        exit 128;
}

eval $eval_value;

# Open the output file
if (not open (TARGET, "> $ARGV[3]")) {
        print STDERR "ERROR: Could not open the output file \"$ARGV[3]\"\n";
        exit 128;
}

# Process the file
print "Processing $ARGV[2]...\n";
if (not process_file ($ARGV[2])) {
        exit 128;
} else {
        exit 0;
}
