#!/usr/bin/perl

use strict;
use warnings;

use Getopt::Long;

use DistroDBTools;
use ProjectDBTools;

my $verbose=0;
my $help;
my ($opt_same,$opt_replaceable,$opt_different);

my $distromap_config=&DistroDBTools::read_distromap_config();
my $origin_repo=$distromap_config->{'ORIGINREPO'};
my $origin_branch=$distromap_config->{'ORIGINBRANCH'};
my $dest_repo=$distromap_config->{'DESTREPO'};
my $dest_branch=$distromap_config->{'DESTBRANCH'};

my $ORIGIN=$origin_repo.'/'.$origin_branch if $origin_repo and $origin_branch;
my $DEST=$dest_repo.'/'.$dest_branch if $dest_repo and $dest_branch;

my %known_modes=map {$_=>1} qw/origin2dest origin2origin dest2dest/;
my $mode='origin2dest';
$mode=$1 if $0 =~ /-(origin2dest|origin2origin|dest2dest)\b/;
my $nameuri_choice='nameuri-same';
$nameuri_choice='nameuri-'.$1 if $0 =~ /-(same|different|replaceable)\b/;

GetOptions (
    'origin=s'  => \$ORIGIN,
    'dest=s'  => \$DEST,
    'help'  => \$help,
    'verbose+'  => \$verbose,
    'same' => sub { $nameuri_choice='nameuri-same' },
    'different' => sub { $nameuri_choice='nameuri-different' },
    'replaceable' => sub { $nameuri_choice='nameuri-replaceable' },
    'mode=s' => \$mode,
);

my ($src1, $src2)=@ARGV;

if ($help or !$ORIGIN or !$DEST or scalar @ARGV!=2 or !$known_modes{$mode}) {
	_usage();
	exit(0);
}

$DistroDBTools::verbose=$verbose;
&ProjectDBTools::init_project_db();
&ProjectDBTools::set_nameuri($src1, $src2, $mode, $nameuri_choice, $verbose);
