#!/usr/bin/perl

=head1 NAME

emgrip-edos - add missing dependencies within Emdebian Grip

=cut

=head1 Synopsis

 emgrip-edos [-s|--suite STRING] [--grip-name NAME] -b|--base-path PATH
 emgrip-edos -?|-h|--help|--version

 Commands:
 -b|--base-path PATH:      path to the top level repository directory [required]

 Options:
    --grip-name STRING:    alternative name for the grip repository
 -s|--suite STRING:        Name of the distribution [default is unstable]

=cut

=head1 Description

Runs F<edos-debcheck> against each Packages file in the Emdebian Grip
repository, collating data from each component and listing the results.

This program can produce a lot of output - best to redirect STDOUT
to a file and process later. Once #540797 is fixed, the XML output
of edos-debcheck can hopefully be processed into something more
useful and readable.

=cut

=head1 Copyright and Licence

 Copyright (C) 2008,2009  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

# handle < filename syntax for Packages file
# handle OR depends
# handle each ARCH in a hash
# note in manpage: recursive depends can be hidden behind each run.

use strict;
use warnings;
use File::Basename;
use Debian::Packages::Compare;
use vars qw/ $dir $edos @binaries @lines $line $pkg $retval
 @archlist $path $our_version $base $grip_name
 $progname %h $suite /;

$progname = basename ($0);
$grip_name = "grip";
$suite = 'unstable';
$our_version = &scripts_version();
@binaries=();

while( @ARGV ) {
	$_= shift( @ARGV );
	last if m/^--$/;
	if (!/^-/) {
		unshift(@ARGV,$_);
		last;
	}
	elsif (/^(-\?|-h|--help|--version)$/) {
		&usageversion();
		exit (0);
	}
	elsif (/^(-b|--base-path)$/) {
		$base = shift;
	}
	elsif (/^(--grip-name)$/) {
		$grip_name = shift;
	}
	elsif (/^(-s|--suite)$/) {
		$suite = shift;
	}
	else {
		die "$progname: Unknown option $_.\n";
	}
}

die "ERR: Please specify an existing directory for the base-path.\n"
	if (not defined $base);

$base .= '/' if ("$base" !~ m:/$:);
die "ERR: Please specify an existing directory for the base-path: $base\n"
	if (not -d $base);

&set_base($base);
my $a = &get_archlist ($suite, $grip_name);
die ("Unable to read architectures.\n") if (not defined $a);
@archlist = sort @$a;
my $list = get_components ("$suite", "$grip_name");
my @components = (not defined $list) ? ('main') : @$list;

foreach my $a (@archlist)
{
	next if ($a eq 'source');
	print "\n\nArchitecture: $a\n\n\n";
	my $file = `mktemp -t gripedos.XXXXXX`;
	chomp ($file);
	foreach my $cmpnt (@components)
	{
		open (PKG, "${base}${grip_name}/dists/${suite}/${cmpnt}/binary-${a}/Packages")
			or die ("Cannot read Packages file: ${base}${grip_name}/dists/${suite}/${cmpnt}/binary-${a}/Packages. $!\n");
		my @f=<PKG>;
		close (PKG);
		open (TMP, ">>${file}");
		print TMP @f;
		close TMP;
	}
	$edos=`edos-debcheck -explain -failures < $file 2>/dev/null`;
	unlink ($file);
	# until #540797 is fixed, just dump the plain text.
	print "$edos\n";
	next;
	my @l = split("\n", $edos);
	push @lines, @l;
	foreach my $l (@lines)
	{
		$h{$l}++;
	}
	@lines=();
	@lines = sort keys %h;
	print @lines;
}

exit 0;

sub scripts_version {
	my $query = `dpkg-query -W -f='\${Version}' emdebian-grip-server`;
	(defined $query) ? return $query : return "";
}

sub usageversion {
	print(STDERR <<END)
$progname version $our_version

Usage:
 $progname [-s|--suite STRING] [--grip-name NAME] -b|--base-path PATH
 $progname -?|-h|--help|--version

 Commands:
 -b|--base-path PATH:      path to the top level repository directory [required]

 Options:
    --grip-name STRING:    alternative name for the grip repository
 -s|--suite STRING:        Name of the distribution [default is unstable]

Runs edos-debcheck against each Packages file in the Emdebian Grip
repository, collating data from each component and listing the results.

This program can produce a lot of output - best to redirect STDOUT
to a file and process later. Once #540797 is fixed, the XML output
of edos-debcheck can hopefully be processed into something more
useful and readable.

END
	or die "$0: failed to write usage: $!\n";
}
