#!/usr/bin/perl

##-{{{
=head1 NAME

sbbuilder - A Simple script to create SlackBuild files with directory structure

=head1 SYNOPSIS

sbbuilder --package=foo --version=42 [--user=Bill] [--build=1] ...


    Help Options:
	--package  REQUIRED flag: Name of the package to build SlackBuild for.
	--version  REQUIRED flag  Version of the package to build SlackBuild for.
	--help     Show this scripts help information.
	--manual   Read this scripts manual.

=cut


=head1 OPTIONS

=over 8
=item B<--package=>string

Name of the package.

=item B<--version=>string

Version of the package.

=item B<--build=>string

Build number.

=item B<--user=>string

Author of the SlackBuild.

=item B<--ldoptions=>string

Extra ldoptions.

=item B<--type=>string

Type of SlackBuild.  There are 5 types of SlackBuilds :

I<normal> . Default configure; make and make install flags. 

I<perl> . Slackbuild for perl packages.

I<python> . Slackbuild for python packages.

I<noarch> . noarch package? This is for you. 

I<cmake> . Use cmake instead GNU autoconf.

=item B<--link=>string

Enter a download link if you know where you can find the tar.gz file

If this option is set and nor package nor version are, sbbuilder will try to
guess them from the file. Alert: No success guaranteed

Example: sbbuilder --link=http://foobar.com/foo-2.3.tar.gz [--user=Bill] [--build=1] ...


=item B<--configure_options=>string

=item B<--help>
Show the brief help information.

=item B<--manual>
Read the manual, with examples.

=item B<--version>
Show the version number and exit.

=back

=cut

=head1 DESCRIPTION


  This is a simple demonstration program for Pod::Usage, this text
  will be displayed if the script is invoked with '--manual'.

=cut


=head1 AUTHORS

kidd 
rbistolfi

=cut

=head1 LICENSE

=cut
##-}}}

use strict;
use warnings;
use Template;
use Getopt::Long;
use Pod::Usage;

# main
my $user;
my $type;
my $package;
my $version;
my $link;
my $build = 1;
my $configure_options;
my $ldflags;
my $manual;
my $help;

GetOptions( 
	"user=s"              => \$user,
	"type=s" 	      => \$type,
	"package=s"           => \$package,
	"version=s"           => \$version,
	"link=s"              => \$link,
	"build=s"	      => \$build,
	"configure_options=s" => \$configure_options,
	"ldflags=s"           => \$ldflags,
	"manual"	      => \$manual,
	"help"	 	      => \$help);



my $message_text = "\nVectorLinux SlackBuild Builder.\n";
my $exit_status   = 1;         
my $verbose_level = 0;
$verbose_level=2 if $manual;

if (!$link and (!$package or !$version)) {
       	pod2usage ({ -message => $message_text ,
	     -exitval => $exit_status  ,  
	     -verbose => $verbose_level,
	     });
}

if ($link and !$package and !$version) {
	($package, $version) = $link =~ m{(?:ht|f)tp.*/(.+)-([^-]+)\.t(ar|gz)}i ;
}

chomp ($user = $user || $ENV{VL_PACKAGER} || `whoami`);
$link =~ s/$package/\$NAME/g if ($link) ;
$link =~ s/$version/\$VERSION/g if ($link);

# Get sbbuilder version
my $sbbversion;
my $ls;

open $ls, "ls /var/log/packages/sbbuilder*|";
while (<$ls>) {
	if (m/sbbuilder/) {
		($sbbversion) = m/sbbuilder-(.+)-.+-\d+vl\d+/ ;
	}
}
#---

my $tt = Template->new({
	INCLUDE_PATH => '/usr/share/sbb/template'});

my $file = 'main.template';
my $vars = {
       	user              => $user,
       	type              => $type,
       	'package'         => $package,
       	version           => $version,
       	'link'            => $link,
        build             => $build,
       	configure_options => $configure_options,
       	ldflags           => $ldflags,
	sbbversion        => $sbbversion,
	};

my $sbText;
$tt->process($file, $vars, \$sbText);
$sbText =~ s/^\s+|\s+$//g;

mkdir "$package" or warn "$!. Failed creating top dir.\n";
mkdir "$package/$version" or warn "$!. Failed creating version folder.\n";
mkdir "$package/$version/src" or warn "$!. Failed creating src folder.\n";
chdir "$package/$version/src" or die $!;
open my $fh, "+>$package.SlackBuild" or die $!;
print $fh $sbText;
close $fh;

# vim: set tabstop=4 shiftwidth=4 foldmethod=marker : ##
