#!/usr/bin/perl -w

use strict;
use File::Glob qw/:glob/;
use File::Basename;
use File::Copy;
use Data::Dumper;
use LWP::Simple;
use List::Util qw/maxstr max/;
use YAML;

my %indexes  = ();

my $debcopystart = qq{
This package was debianized by Jaldhar H. Vyas <jaldhar\@debian.org>  on 
Mon, 10 Apr 2006 11:04:37 -0400

It contains several upstream packages which may have different licenses.

They are all downloaded from http://search.cpan.org/\n
};

my $debcopyright = qq{
-----------------------------------------------------------------------------	
	Perl is distributed under licenses:

    a) the GNU General Public License as published by the Free Software
       Foundation; either version 1, or (at your option) any later
       version, or

    b) the "Artistic License" which comes with Perl.

    On Debian GNU/Linux systems, the complete text of the GNU General
    Public License can be found in /usr/share/common-licenses/GPL' and
    the Artistic Licence in /usr/share/common-licenses/Artistic'.
};

my $debcopyinfo = qq{
  This program is free software, you can redistribute it and/or modify it under
  the same terms as Perl itself.
};

#--- switch off buffering
$|++;

#----------------------------------------------------------------------------
# add/update packages & numbers to debian/control file
#----------------------------------------------------------------------------
sub control($)
{
	my ($files) = @_;
	my $control = '../debian/control';
	my $output = '';
	my $copyright  = '../debian/copyright';
	my $coutput = $debcopystart;
	
	open( FD, '<', $control ) or die "Cannot open $control\n";
	my @lines = <FD>;
	close( FD );
			
	for my $line (@lines)
	{	
		$output .= $line;
		if ($line =~ /Currently the following modules are included/)
		{
			
			for my $file (@$files)
			{
				my (@parts) = $file =~ m!(.+)\-([\d\.]+)(\.tar\.gz)!;
				my $yfile = $parts[0].'.yml';
				$parts[0] =~ s/\-/:/g;
				my $modname = basename($parts[0]);
				
				$output .= qq{ .\n $modname: };
				
				#--- read yml (yaml) file and prepare output
				my $yml = YAML::LoadFile($yfile);
				if (defined $yml->{'description'})
				{
					$output .= qq{$yml->{'description'}};
				}
				else
				{
					print "$yfile has not defined 'description' section\n";
				}
				
				if (defined $yml->{'author'})
				{
					$coutput .= qq{* $modname\n};
					if (ref($yml->{'author'}) eq 'ARRAY')
					{
						$coutput .= qq{\n  Authors:\n};
						foreach  my $author (@{$yml->{'author'}})
						{
							$coutput .= qq{\t$author\n};
						}
					}
					else
					{
						$coutput .= qq{\n  Author:\n\t$yml->{'author'}\n};
					}
					$coutput .= qq{$debcopyinfo\n};
				}
				else
				{
					print "$yfile has not defined 'author' section\n";
				}
				if (defined $yml->{'copyright'})
				{
					$coutput .= qq{\n  $yml->{'copyright'}\n};
				}
			}
			last;
		}
				
	}
	$coutput .= $debcopyright;
	open( FD, '>', $control.".new" ) or die "Cannot open $control.new\n";
	print FD $output;
	close( FD );
	move($control.".new", $control);

	open( FD, '>', $copyright ) or die "Cannot open $copyright\n";
	print FD $coutput;
	close( FD );
	
}

#----------------------------------------------------------------------------
# check upstream versions
#----------------------------------------------------------------------------
sub check($;$)
{
	my ($files, $path) = @_;

	$path = "" unless defined $path;
	
	for my $file (@$files)
	{
		my $current_version = 0;
		my $upstream_version = 'missing';		
	
		#--- check package's current version 
		my ($base, $path, , $suffix) = fileparse($file, '.yml');
		my @current_file = bsd_glob($path.$base."-*.tar.gz");
		
		my $targz = basename(shift @current_file);
		($current_version) = $targz =~ m!\-([\d\.]+)\.tar\.gz$!;
	
		#--- open watch file, read url
		my $yml = YAML::LoadFile($file); 
		open(FD, "<". $file) or die "Cannot open $file\n";
	
		if (defined $yml->{'watch'})
		{
			my $url = $yml->{'watch'};
			
			#--- get "directory" part & "file" part
			my ($u_dir, $u_file) = $url =~ m!(.+/)(.+)$!;
			
			#--- now take index file with directories								
			unless (defined($indexes{$u_dir}))
			{
				#--- cache result
				$indexes{$u_dir} = get($u_dir);
			}
							
			my @found = $indexes{$u_dir} =~ m!$u_file!g;
			$upstream_version = maxstr(@found);
		}
		else
		{
			print "$file has not defined 'watch' section\n";
		}
		print qq{File: $base\t Current: $current_version Upstream: $upstream_version\t};
		print "up to date\n" if ($current_version == $upstream_version);
		print "new upstream version!\n" if ($current_version < $upstream_version);
		print "newer than upstream version!\n" if ($current_version > $upstream_version);
		
	}	
}

if (defined($ARGV[0]))
{
	if ($ARGV[0] eq 'control')
	{
		#--- add/update packages & numbers to debian/control file
		my @f = bsd_glob('??/*.tar.gz');
		control( \@f );
	}	
}
else
{
	#--- check upstream versions
	my @f = bsd_glob('??/*.yml');    
	check( \@f );
}
