#!/usr/bin/perl
use strict;
use warnings;
#use Pod::Help qw(-h --help);
use Pod::Usage;
use Getopt::Long;

    ## Parse options
    my %opt;
    GetOptions(\%opt, "help|?", "man", "flag1", "version")  ||  pod2usage(2);
    pod2usage(1)  if ($opt{help});
    if ($opt{version}){
        print "Version 3\n";
	exit;
    };
    pod2usage(-exitval => 0, -verbose => 2)  if ($opt{man});

    ## Check for too many filenames
 #   pod2usage("$0: Too many args given.\n")  if (@ARGV > 5);


#print " $ARGV[0] $ARGV[1]\n";

my $ac=scalar @ARGV;
if ($ac<1){
    print STDERR "Cantidad insuficiente de argumentos\n";
    exit;
}

=pod
 
=head1 NAME

    templatesumma - generador heredado de metadatos con Template Toolkit

=head1 SYNOPSIS

    templatesumma PAQUETE SVVERSION PACKAGER LICENCIA LIBMAYVER VERSION

     Options:
       -help            brief help message
       -man             full documentation

=head1 OPTIONS

=over 4

=item B<-help>
    
    Muestra la ayuda breve.

=item B<-man>

    Muestra la referencia completa.

=back

=head1 DESCRIPTION

Desde el directorio actual lee template.changes, template.spec y
debian/changelog.template. Copia el primero a PAQUETE.changes y genera
PAQUETE.spec y debian/changelog con los seis argumentos de la sinopsis.

Puede sobrescribir esos archivos; no tiene --prueba. Requiere Perl y
Template Toolkit (Template). Las plantillas pueden ejecutar codigo Perl:
usar solo material confiable. Admite --version.

Se conserva para las reglas antiguas de makefile.lib. Para un proyecto
nuevo se recomienda summa.py y hacer-pkg; ver el manual de SummaPack.
 
=cut
 

system("cp template.changes $ARGV[0].changes");

   use Template;

    # some useful options (see below for full list)
    my $config = {
        INCLUDE_PATH => '.',  # or list ref
        INTERPOLATE  => 1,               # expand "$var" in plain text
        POST_CHOMP   => 1,               # cleanup whitespace
#        PRE_PROCESS  => 'header',        # prefix each template
        EVAL_PERL    => 1,               # evaluate Perl code blocks
    };

    # create Template object
    my $template = Template->new($config);
#924
#    GPL-3.0+
#    GPLv3
#Packager: dsa <dsa@ututo.org>

    # define template variables for replacement
my $vars = {
    PACK =>  $ARGV[0],
        SVVERSION  => $ARGV[1],
	PACKAGER   => $ARGV[2],
	LICENCE    => $ARGV[3],
    LIBMAYVER  => $ARGV[4],
    VERSION => $ARGV[5]
#        var2  => \%hash,
#        var3  => \@list,
#        var4  => \&code,
#        var5  => $object,
    };

    # specify input filename, or file handle, text reference, etc.
   my  $input = "template.spec";

    # process input template, substituting variables
    my $ok=$template->process($input, $vars,"$ARGV[0].spec");

    if (not $ok) {
        die $template->error(), "\n";
        
        }


  $input = "debian/changelog.template";
 $ok=$template->process($input, $vars,"debian/changelog");

    if (not $ok) {
        die $template->error(), "\n";
        
        }
