#!/usr/bin/perl

=pod

=head1 Name

emtargetcmp - Compare the Emdebian target repository with Debian unstable

=head1 Copyright and Licence

 Copyright (C) 2007  Neil Williams <codehelp@debian.org>

 This package is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.

=cut

use Cache::Apt::Lookup;
use Cache::Apt::Config;
use Cache::Apt::Package;
use File::Basename;
use strict;
use warnings;
use Term::ANSIColor qw(:constants);
use Text::Wrap;
use vars qw/ @newer @older @equal %sources %chain @cross_triplet $verbose $arch $suite /;

$verbose = 2;
$arch = "arm"; # only arm packages are built so far.
# need support for tweaking this.
$suite = "unstable";

&set_verbose ($verbose);
&set_suite($suite);
&check_cache_arch($arch) if (defined ($arch));
my $cache = &get_aptcross_dir;
if (not -d $cache)
{
	mkdir ($cache) or die ("Unable to create the cache directory: $!");
}
# check for (and create) sources.list file.
my $force = 0;
$force = 1 unless ( -f "$cache/sources.compare.$suite");
open (SOURCE, ">$cache/sources.compare.$suite")
	or die ("Cannot create sources list for comparison: $!");
print SOURCE "deb http://www.emdebian.org/emdebian/ unstable main\n";
print SOURCE "deb-src http://www.emdebian.org/emdebian/ unstable main\n";
close (SOURCE);
open (STATUS, ">$cache/status");
close STATUS;
&set_cachedir($cache);
&use_mysources("sources.compare.$suite");
&force_update if ($force > 0);
my $config = &init_cache($verbose);

my $iter = &get_cache_iter();
my $pkg;
my @package_names = ();
my %h = ();

do
{
	$pkg = $iter->next;
	$h{$pkg}++ if ($pkg);
} while ($pkg);

@package_names = sort (keys %h);
# array of AptCrossPackage structs, ordered by package name
my @packages = ();
foreach my $p (@package_names)
{
	my $emp = AptCrossPackage->new();
	$emp->Package($p);
	$emp = &lookup_pkg($emp);
	push @packages, $emp;
}

foreach my $package (@packages)
{
	my $src = $$package->Source;
	my $vers = $$package->Version;
	next unless defined $vers;
	$vers =~ s/em[0-9]+$//;
	$sources{$src} = $vers;
}
print CYAN, "found " . scalar (keys %sources) . " source packages with a version string.\n", RESET;

# we don't need the other cache again. Start the host_cache.
$arch = `dpkg-architecture -qDEB_HOST_ARCH`;
chomp($arch);
&check_cache_arch ($arch);
# probably better to use a separate source file here! :-)
unlink ("$cache/sources.compare.$suite");
&use_hostsources;
&setup_config;
&update_sources;
$config = &init_host_cache($verbose);
@older = ();
@newer = ();
@equal = ();

my @list = ();
@list = sort (keys %sources);

foreach my $check (@list)
{
	my $host;
	my $name;
	$host = &srclookup($check);
	$name = $sources{$check};
	unless (defined $host->{'Version'})
	{
		push (@newer, $check);
		next;
	}
	my $cmp = &ver_compare ($name, $host->{'Version'});
	push (@older, $check) if ($cmp < 0);
	push (@newer, $check) if ($cmp > 0);
	push (@equal, $check) if ($cmp == 0);
}
unlink ("$cache/sources.compare.$suite");
print "\n";
if (scalar @older > 0)
{
	print CYAN, "The following "  . scalar @older .
		" source packages are older in Emdebian than in Debian:\n", RESET;
	print RED, wrap('', '', @older), RESET ;
	print "\n";
}
 if ((scalar @equal) > 0)
 {
	print CYAN, "The following " . scalar @equal .
		" source packages are the same version in Emdebian as in Debian:\n", RESET;
	print GREEN, wrap('', '', @equal), RESET;
	print "\n";
}
if ((scalar @newer) > 0)
{
	print CYAN, "The following source packages are newer in Emdebian than in Debian:\n", RESET;
	print CYAN, wrap('', '', @newer), RESET;
	print "\n";
}
my @scripts = qw#/usr/lib/emdebian-tools/emdebian.crossd
/usr/lib/emdebian-tools/emdebian.gtk /usr/lib/emdebian-tools/emdebian.gpe#;
for my $script (@scripts)
{
	my $parser = qq% mirror_style() { echo -n; }; download_style() { echo -n; }; . $script; work_out_debs; echo -n \$base \$required;%;
	$arch = "arm";
	my $base = `$parser`;
	# more than just chomp
	$base =~ s/\n//g;
	print CYAN, "\nTesting Emdebian rootfs (" . basename($script) . ")\n\n", RESET;
	my $packages = "www.emdebian.org_emdebian_dists_" . $suite;
	$packages .= "_main_binary-" . $arch . "_Packages";
	system ("edos-debcheck -explain -failures $base < $cache/$suite/lists/$packages");
	print "\n";
}

=head1 Description

emtargetcmp prepares cache data from the emdebian/emdebian target cache
at buildd.emdebian.org against the Debian unstable cache at ftp.uk.debian.org
for arm with support for mapping Emdebian version strings to Debian versions.

Uses ftk.uk.debian.org or ftp.de.debian.org due to location of buildd.emdebian.org

One of the remaining issues with this approach is the apparent lack of support
for creating a sources.list in memory without needing lots of little files.
Therefore emtargetcmp creates a special sources list in the apt-cross
directory (e.g. ~/.apt-cross/) and switches the content of the file as
necessary.

This small utility also serves as an example of what can be done with
Cache::Apt::* from libcache-apt-perl.

Currently, emtargetcmp only supports ARM because Emdebian only has a
root filesystem for ARM.

Output uses ANSIColor (where supported) so that packages which are older
in Emdebian than in Debian are listed in red and packages which are the
same version in Emdebian as in Debian (accounting for the emdebian version
suffix) are shown in green. If, for whatever reason, Emdebian contains packages
that are newer than in Debian, these are shown in blue.

=head1 Commands and options

emtargetcmp accepts no commands or options.

=cut
