#!/usr/bin/perl -w

use strict;
use warnings;

use DistroMap;
use Source::Shared::CLI;
our @ISA=qw/Source::Shared::CLI/;

my $column=1;
my $drop_uncomparable;

sub cli_container_ref {['DistroMap']};
our @LONGOPT=(
    'c|column=i' => \$column,
    'drop-uncomparable' => \$drop_uncomparable,
);
our @LONGOPT_POD2USAGE;
__PACKAGE__->get_and_process_cli_options();

if (defined $column and not $column>0) {
    die "invalid column option: should be > 0\n";
}
my $linenum=$column*2-2;

my $distromap=DistroMap->new();

while (<>) {
    chomp;
    next unless $_; # empty line ?
    next if /^#/;
    next if /^\s*$/;
    if ($column) {
	my @line=split(/(\s+)/,$_,-1);
	my $name=$line[$linenum];
	#warn "name=$name\n";
	die "invalid column $column: value not defined\n" unless defined $name;
	my $map=$distromap->unimap_source_dest2origin($name);
	next if $drop_uncomparable and $distromap->versions_are_uncomparable($map);
	$line[$linenum]=$map ? $map : $name;
	print join('',@line), "\n";
    } else {
	s/^(\s*)//;
	print $1;
	while (s/^(\S+)(\s*)//) {
	    my $name=$1;
	    my $space=$2;
	    my $map=$distromap->unimap_source_dest2origin($name);
	    print $map ? $map : $name , $space;
	}
	print "\n";
    }
}

=head1	NAME

distromap-filter-translate-source-names - 

=head1	SYNOPSIS

B<distromap-filter-translate-source-names>
[B<--column column>]
[B<--drop-uncomparable>]
[B<-h|--help>]
[B<-q|--quiet>]
[<other options, see --help>]

=head1	DESCRIPTION

B<distromap-filter-reverse-translate-source-names> 

=head1	OPTIONS

=over

=item	B<--drop-uncomparable>

Drop line if version of the package is not comparable in origin and destination repositories.
Require column mode.

=item	B<--column column>

Column mode. Set column to translate.

=back

=head1	AUTHOR

Written by Igor Vlasenko <viy@altlinux.org>.

=head1	COPYING

Copyright (c) 2009-2017 Igor Vlasenko, ALT Linux Team.

This 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 2 of the License, or (at your option) any later version.

=cut
