#!/usr/bin/perl -w

use strict;
use warnings;
use lib 'lib';

use Getopt::Long;
use Gear::Remotes::Tags;
use Gear::Remotes::GitUrl;
#$Gear::Remotes::Tags::verbose=3;
$Gear::Remotes::GitUrl::verbose=2;

my $verbose=0;
my $help;

my $result = GetOptions (
    'quiet'=> sub {$verbose=0},
    "verbose+"  => \$verbose,
    "help"  => \$help,
);

sub help {
    print "Usage: ",$0, " <url>\n";
}
if ($help) {
    &help();
    exit(0);
}

my $exit_code = 2;
my $url = $ARGV[0];
$url = &Gear::Remotes::GitUrl::emptify_no_url($url);
    # explicitly disabled
if (!defined($url) or $url eq '') {
    &help();
    $exit_code = 1;
} elsif (defined($url) and $url eq '0') {
    print "url: ",$url, " is recognized as explicitly disabled\n";
    $exit_code = 0;
} elsif (not &Gear::Remotes::GitUrl::is_git_like_url($url)) {
    print "url: ",$url, " is not accepted\n";
} else {
    warn 'quering ',$url,"\n" if $verbose;
    my ($tag_pairs_ref, $exit_code)=&Gear::Remotes::Tags::ls_remote_git_tags($url);
    unless ($tag_pairs_ref) {
	print 'connection to url failed',"\n";
    } elsif (not @$tag_pairs_ref) {
	print 'no acceptable tags found in ',$url,"\n";
    } else {
	print 'url ',$url," is valid for gear-remotes-batch-watch\n";
	$exit_code = 0;
    }
}
exit ($exit_code);
