#!/usr/bin/perl
#  Copyright (C) 2005  Stanislav Sinyagin
#
#  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; either version 2 of the License, or
#  (at your option) any later version.
#
#  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.

# Stanislav Sinyagin <ssinyagin@k-open.com>


use strict;
use warnings;

use Template;
use Getopt::Long;
use IO::File;

my $template;
my $outfile;
my $nodelist;
my $parameters;


my $creator = "Torrus version 3.00\n" .
    "This file was generated by command:\n" .
    $0 . " \\\n";
foreach my $arg ( @ARGV )
{
    if( $arg =~ /^--/ )
    {
        $creator .= ' ' . $arg . ' ';
    }
    else
    {
        $creator .= "\'" . $arg . "\'\\\n";
    }
}
$creator .= "\nOn " . scalar(localtime(time));


my $ok = GetOptions( 'tmpl=s'   => \$template,
                     'out=s'    => \$outfile,
                     'nodes=s'  => \$nodelist,
                     'param=s'  => \$parameters );

if( not $ok or not $template or not $outfile or not $nodelist)
{
    print STDERR "Process a template with a nodelist\n\n";

    print STDERR "Usage: $0 options...\n",
    "Mandatory options:\n",
    " --tmpl=filename                 template file name\n",
    " --out=filename                  output file\n",
    " --nodes=filename                file with nodes\n",
    "Options:\n",
    " --param=NAME:VALUE,NAME:VALUE...  parameters passed to template\n";
    exit 1;
}

my @rawnodes;

my $nodesfh = IO::File->new($nodelist, 'r');
if( not defined($nodesfh) )
{
    print STDERR "Cannot open $nodelist: $!\n";
    exit 1;
}

while(<$nodesfh>)
{
    s/^\s+//;
    s/\s+$//;
    push( @rawnodes, split( /\s+/ ) );
}
$nodesfh->close();

my %nodes;
foreach my $node ( @rawnodes )
{
    my $symname = $node;
    if( $node =~ /([^:]+):(.+)/ )
    {
        $node = $1;
        $symname = $2;
    }

    $nodes{$node} = $symname;
}

my %params;
if( defined( $parameters ) )
{
    foreach my $pair ( split( '\s*,\s*', $parameters ) )
    {
        my ($name, $val) = split( '\s*:\s*', $pair );
        $params{$name} = $val;
    }
}

my $tt = new Template( INCLUDE_PATH => '/etc/torrus/templates',
                       ABSOLUTE => 1,
                       RELATIVE => 1,
                       TRIM => 1 );

my $vars =
{
    'nodes'   => \%nodes,
    'param'   => \%params,
    'nodesfile' => $nodelist,
    'creator' => $creator,
    'lc' => sub{ return lc $_[0] },
    'uc' => sub{ return uc $_[0] }
};

my $result = $tt->process($template, $vars, $outfile);

if( not $result )
{
    print STDERR "Error while processing template: ".$tt->error()."\n";
}

exit( $result ? 0:1 );


# Local Variables:
# mode: perl
# indent-tabs-mode: nil
# perl-indent-level: 4
# End:
