#!/usr/bin/perl -w

use Gear::Rules;
use Gear::Remotes;
use Gear::Remotes::Entry;

use strict;
use warnings;

my $topdir=Gear::Git::Helper::get_git_toplevel();
die "fatal: $topdir - not a gear repository.\n" unless &Gear::Remotes::check_if_gear_dir($topdir);
my $gear_remotes_file=&Gear::Remotes::get_gear_remotes_file($topdir);

if (-f $gear_remotes_file ) {
    die "$gear_remotes_file already exist; first run
rm $gear_remotes_file\n";
}

my $url=$ARGV[0];
unless ($url) {
    # TODO usage() here;
    die "Error: URL is required.";
}

&run (qw/mkdir -p/, $topdir.'.gear/upstream/');
Gear::Remotes::Entry->new(-url => $url)->append($gear_remotes_file);
&run (qw/git add/, $gear_remotes_file);
print "now run gear-remotes-restore\n";

sub run {
    die "failed to run: ",join(' ',@_),"\n" if (system(@_)!=0);
}

__END__

=head1	NAME

gear-remotes-set-from-url - create .gear/upstream/remotes file from the given url

=head1	SYNOPSIS

B<gear-remotes-set-from-url>
I<URL>

=head1	DESCRIPTION

B<gear-remotes-set-from-url> creates I<.gear/upstream/remotes> file from the given remote git repository url.
After I<.gear/upstream/remotes> is created, run B<gear-remotes-restore>.

=head1	ARGUMENTS

I<URL> -- remote git repository url.

=head1	OPTIONS

none.

=head1	AUTHOR

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

=head1	COPYING

Copyright (c) 2014-2022 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

__END__
