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

    Sepec template, para construir specs

=head1 SYNOPSIS

    spectemplate

     Options:
       -help            brief help message
       -man             full documentation

=head1 OPTIONS

=over 4

=item B<-help>
    
    spectempalte
    Print a brief help message and exits.

=item B<-man>

    Prints the manual page and exits.

=back

=head1 DESCRIPTION

    spectemplate

    B<This program> will read the given input file(s) and do something
    useful with the contents thereof.
 
=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";
        
        }
