#!/usr/bin/perl

=head1 NAME

emgrip-edos - add missing dependencies within Emdebian Grip

=cut

=head1 Synopsis

 $progname [-v|--verbose] [-q|--quiet]
 $progname -?|-h|--help|--version

=cut

=head1 Description

Run edos-debcheck against each Packages file in the Emdebian Grip
repository, identify any missing dependencies and use em_autogrip
to add the missing binaries to the repository. When all missing
binaries have been added, the relevant source packages and Emdebian
TDebs are also updated.

=cut

=head1 Copyright and Licence

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

# ToDo - handle -b option
# handle < filename syntax for Packages file
# have to install the package to handle /org/emdebian/scripts
# 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 vars qw/ $dir $edos @binaries @lines $line $pkg $retval
 @archlist $path $verbose $our_version /;

$verbose = 1;
$our_version = '0.0.5'; # not from Emdebian::Tools
@archlist=qw/ i386 amd64 arm armel powerpc mips mipsel /;
$path = "grip/dists/unstable/main/";

$dir=dirname($0);
chomp($dir);
die if (not -f "$dir/grip-binary.sh");
@binaries=();

while( @ARGV ) {
	$_= shift( @ARGV );
	last if m/^--$/;
	if (!/^-/) {
		unshift(@ARGV,$_);
		last;
	}
	elsif (/^(-\?|-h|--help|--version)$/) {
		&usageversion();
		exit (0);
	}
	elsif (/^(-v|--verbose)$/) {
		$verbose++;
	}
	elsif (/^(-q|--quiet)$/) {
		$verbose--;
	}
	else {
		die "$progname: Unknown option $_.\n";
	}
}

while (@lines)
{
	my %h;
	@lines=();
	$edos='';
	for my $a (@archlist)
	{
		$edos=`edos-debcheck -explain -failures < $path/binary-$a/Packages 2>/dev/null`;
		my @l = split("\n", $edos);
		push @lines, @l;
	}
	foreach my $l (@lines)
	{
		$h{$l}++;
	}
	@lines=();
	@lines = sort keys %h;
	&recurse;
}

# replace with normal call
system ("$dir/grip-update.sh");

exit 0;

sub recurse
{
	@lines = split("\n", $edos);
	for $line (@lines)
	{
		next unless ($line =~ /NOT AVAILABLE/);
		$line =~ s/^.*depends on (.*) \{NOT AVAILABLE\}$/$1/;
		$line =~ s/ \(.*//;
		$retval = system "apt-cache search $line >/dev/null";
		push (@binaries, $line) if ($retval == 0);
	}
	for $pkg (@binaries)
	{
		system ("$dir/grip-binary.sh $pkg");
	}
}

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

Usage:
 $progname [-v|--verbose] [-q|--quiet]
 $progname -?|-h|--help|--version

Run edos-debcheck against each Packages file in the Emdebian Grip
repository, identify any missing dependencies and use em_autogrip
to add the missing binaries to the repository. When all missing
binaries have been added, the relevant source packages and Emdebian
TDebs are also updated.

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