#!/usr/bin/perl

# need this for hostname()
use File::Basename;
use Sys::Hostname;
use Getopt::Long;
use strict;

my $MainDir = '/tmp/cpan';

# set default options, then get options
my %options=();
$options{'email'}=(getpwuid($<))[0] . "\@redhat.com" ;
GetOptions(\%options, "email=s", "n=s", "create") || exit 1;

my $InputFile = $ARGV[0];
my $create = '';

if ($options{'create'}) {
  $create = '-c';
}

my $tarball = basename($InputFile);
$tarball =~ /(\S+)\-(\S+)\.tar\.gz/;
my $clm_name=$1;
my $clm_version=$2;

my $class = dirname($InputFile);
$class =~ s/^\.\/[0-9][0-9]_//;
if ($class ne '.') {
  $class = "($class)";
} else {
  $class = "";
}

# Change ::'s to -'s
$clm_name =~ s/::/-/g;

my $clm_changelog = get_changelog();

# complain if either parameter is missing
($clm_name eq "") && die "Module name not specified\n";
($clm_version eq "") && die "Module version not specified\n";

# Create and Open file to create SPEC files.
mkdir($MainDir, 0755);
system("cp $InputFile $MainDir");
my $filename = $clm_name . '.spec'; 
open (FILE, "> $MainDir/$filename");

# Print the spec file. Lots of substitutions here.
print FILE "Summary: $clm_name module for perl $class
Name: perl-$clm_name
Version: $clm_version
Release: 7
Copyright: distributable
Group: Applications/CPAN
Source0: $clm_name-$clm_version.tar.gz
Url: http://www.cpan.org
BuildRoot: %{_tmppath}/perl-$clm_name-buildroot/
BuildRequires: perl >= 0:5.00503
Requires: perl >= 0:5.00503

%description
$clm_name module for perl

# Provide perl-specific find-{provides,requires}.
%define __find_provides /usr/lib/rpm/find-provides.perl
%define __find_requires /usr/lib/rpm/find-requires.perl

%prep
%setup -q -n $clm_name-%{version} $create

%build
CFLAGS=\"\$RPM_OPT_FLAGS\" perl Makefile.PL
make

%clean 
rm -rf \$RPM_BUILD_ROOT

%install
rm -rf \$RPM_BUILD_ROOT
eval `perl '-V:installarchlib'`
mkdir -p \$RPM_BUILD_ROOT/\$installarchlib
make PREFIX=\$RPM_BUILD_ROOT/usr install

[ -x /usr/lib/rpm/brp-compress ] && /usr/lib/rpm/brp-compress

find \$RPM_BUILD_ROOT/usr -type f -print | \
	sed \"s\@^\$RPM_BUILD_ROOT\@\@g\" | \
	grep -v perllocal.pod | \
	grep -v \"\\.packlist\" > $clm_name-$clm_version-filelist
if [ \"\$(cat $clm_name-$clm_version-filelist)X\" = \"X\" ] ; then
    echo \"ERROR: EMPTY FILE LIST\"
    exit -1
fi

%files -f $clm_name-$clm_version-filelist
%defattr(-,root,root)

%changelog
* $clm_changelog
- Spec file was autogenerated. 
";
close(FILE);

# Now build the rpm
create_rcfiles();

open (LOG, "> $MainDir/LogFile");
build_rpm();
close LOG;

sub cleanup {
  unlink "$MainDir/$filename";
  unlink "$MainDir/$tarball";
}

sub build_rpm {
  my $retval;

  # First, make sure it unpacks cleanely
  system("rpm --rcfile $MainDir/rpmrc -bp $MainDir/$filename");
  $retval = $? >> 8;
  if ($retval != 0) {
    print "RPM test unpacking failed!\n";
    print LOG "PREP failed: $filename\n";
    return;
  }

  system("rpm --rcfile $MainDir/rpmrc -bs --rmsource --rmspec --clean $MainDir/$filename");
  $retval = $? >> 8;
  if ($retval != 0) {
    print "RPM building failed!\n";
    print LOG "SOURCE failed: $filename\n";
    return;
  }
  cleanup();
}

sub create_rcfiles {
    open(MACROS, "> $MainDir/macros");
    print MACROS qq{
%_topdir        $MainDir
%_builddir      %{_topdir}/junk
%_rpmdir        %{_topdir}
%_sourcedir     %{_topdir}
%_specdir       %{_topdir}
%_srcrpmdir     %{_topdir}/temp
};
    close(MACROS);
  
    open(RPMRC, "> $MainDir/rpmrc");
    print RPMRC qq{
include: /usr/lib/rpm/rpmrc
macrofiles: /usr/lib/rpm/macros:$MainDir/macros
};
    close(RPMRC);
}

sub get_changelog {
  # generate the changelog entry from available system info
	my ($name);

	$name = (getpwuid($<))[6];
	$name = (split(",", $name))[0];
  return ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat")[(localtime)[6]] . " " .
    ("Jan", "Feb", "Mar", "Apr", "May", "Jun",
     "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")[(localtime)[4]] . " " .
    (localtime)[3] . " " . (1900+(localtime)[5]) . " " .
		$name . " <" . $options{'email'} . ">";
}

