#!/usr/bin/perl

use strict;
use warnings;
use IO::File;
use File::Copy;
use File::Basename;
use POSIX qw(locale_h);
use Locale::gettext;
use Emdebian::Grip; # internal module
use Debian::Packages::Compare;
use IO::Uncompress::Gunzip qw(gunzip $GunzipError) ;

use vars qw/ $grip_name $suite $base $mode $package %sections
 @architectures @components %orig %dupes $arch $prog $dry $c
 @archlist $main @cmd $filter_name %list @suites @srcs /;

setlocale(LC_MESSAGES, "");
textdomain("emdebian-grip");
$filter_name = 'filter';
$grip_name = "grip";
$suite = "unstable";
$prog = basename($0);

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

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

$base .= '/' if ("$base" !~ m:/$:);
if (not -d $base) {
	printf (_g("ERR: Please specify an existing directory for the base-path: %s\n"),$base);
	exit 1;
}

&set_base($base);
&set_repo_names ($filter_name, $grip_name);
my $s = &get_unfrozen_suite_names ($grip_name);
@suites = @$s;
my $a = &get_archlist ($suite, $grip_name);
@architectures = @$a;
$c = &get_components ($suite, $grip_name);
@components = @$c;

if (@ARGV) {
	push @cmd, @ARGV;
} else {
	print (_g("ERR: Please specify a package to remove.\n"));
	exit 2;
}
use Data::Dumper;
foreach $suite (@suites) {
	foreach $arch (@architectures) {
		my $p;
		foreach my $pkg (@cmd) {
			$p = &get_single_package ($suite, $grip_name, $pkg, $arch);
			last if (defined ($p->{Source}));
		}
		last if (not defined $p->{Source});
		$p->{Source} =~ s/\s.*$//;
		$list{$suite} = $p->{Source};
		push @srcs, $p->{Source};
		last if (defined ($p->{Source}));
	}
}

if (scalar keys %list == 0) {
	my $str = join (', ', @cmd);
	foreach $suite (@suites) {
		printf( _g("ERR: Cannot find the source package to remove from %s for binary package:'%s'.\n"),
			$suite, $str);
		foreach $package (@srcs) {
			print _g("INF: Maybe try a different suite:\n");
			system ("reprepro -v -b ${base}$filter_name list $suite $package 2>/dev/null");
			system ("reprepro -v -b ${base}$grip_name list $suite $package 2>/dev/null");
		}
	}
	exit 0;
}

while (($suite, $package) = each (%list)) {
	if (defined $dry) {
		print "reprepro -v -b ${base}$filter_name removesrc $suite $package\n";
		print "reprepro -v -b ${base}$grip_name removesrc $suite $package\n";
		print "reprepro -v -b ${base}locale removesrc $suite $package\n";
	} else {
		system ("reprepro -v -b ${base}$filter_name removesrc $suite $package");
		system ("reprepro -v -b ${base}$grip_name removesrc $suite $package");
		system ("reprepro -v -b ${base}locale removesrc $suite $package");
	}
}
&read_packages ('unstable', $filter_name);
&update_filter;
exit 0;

=pod

=head1 NAME

emgrip-remove - remove a source package from all components in all unfrozen suites

=head1 Synopsis

 Syntax: emgrip-remove -b PATH [OPTIONS] BINARY_PACKAGE
         emgrip-remove -?|-h|--help|--version

 Commands:
 -b|--base-path PATH:           path to the top level grip directory [required]
 -?|-h|--help|--version:        print this help message and exit

Options:
    --grip-name STRING:         alternative name for the grip repository
    --filter-name STRING:       alternative name for the filter repository
 -n|--dry-run:                  print the reprepro commands that would be used.

=head1 Description

Cleanly remove a source package and all associated binary packages from
all components in all unfrozen suites. If the package was in unstable,
all relevant packages are also removed from the filter repository
so that packages can be removed from Grip whether or not the equivalent
package remains in Debian.

=cut

sub usageversion {
	printf STDERR (_g("
%s - remove a source package from all components in a suite

 Syntax: %s -b PATH [OPTIONS] BINARY_PACKAGE
         %s -?|-h|--help|--version

 Commands:
 -b|--base-path PATH:           path to the top level grip directory [required]
 -?|-h|--help|--version:        print this help message and exit

Options:
    --grip-name STRING:         alternative name for the grip repository
    --filter-name STRING:       alternative name for the filter repository
 -n|--dry-run:                  print the reprepro commands that would be used.

"), $prog, $prog, $prog, $prog, $prog)
	or die ("$0: "._g("failed to write usage").": $!\n");
}

=head1 Copyright and Licence

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