#!/usr/bin/perl

# This program 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.
#
# 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/>.

# by Igor Vlasenko, 2014

use strict;
use warnings;
use Getopt::Long;
use RPM::uscan;

my $verbose=0;
my $debug;
my ($help,@opt_watchfile,@opt_drop_option,$opt_packagename,$opt_pkg_version,$opt_timeout,$opt_all);
my $opt_any_archive=1;
my $opt_queryformat='%{URL}\n';

my $options_result = 
    GetOptions (
	"h|help"   => \$help,
	"debug"  => \$debug,
	"f|watchfile=s" => \@opt_watchfile,
	"any-archive!" => \$opt_any_archive,
	"drop-option=s" => \@opt_drop_option,
	"a|all" => \$opt_all,
	"package=s" =>	\$opt_packagename,
	"version=s" =>	\$opt_pkg_version,
	"qf|queryformat=s" => \$opt_queryformat,
	"timeout=i" =>	\$opt_timeout,
	"verbose+"  => \$verbose,
	"quiet"  => sub {$verbose=0},
    );

if ($help) {
    usage();
    exit;
}

push @opt_watchfile, @ARGV;
unless (@opt_watchfile) {
    print STDERR "Error: no watch file specified\n\n";
    usage();
    exit 1;
}

sub usage
{
    print STDERR "Usage: uscan-query: [--queryformat <format>] [-f] name1.watch ...

General options:
	-h, --help
	-v, --verbose (can be specified multiple times)
	-q, --quiet

Processing options:
	-a, --all	print all tags and their values
	--any-archve	enable any-archive pattern mangling (default)
	--no-any-archve	disable any-archive pattern mangling
	--skip-on-error	ignore individual watch file failures
	--drop-option <option>	ignore option <option> in watch file
	--timeout <seconds>	set network timeout <seconds>
	--queryformat,--qf <format>	output values in format <format>.
	    option syntax is similar to one of rpmquery utility. 
	    \\t, \\n, is supported, default value is '$opt_queryformat'.
	    For the list of possible tags, use --all.

Default value options (if not determined from gear repository):
	--package <name> set package name as <name>
	--version <version> set local version as <version>

\n";
}

my $ref_opt_override;
if (@opt_drop_option) {
    $ref_opt_override={};
    foreach my $opt (@opt_drop_option) {
	$ref_opt_override->{$opt}=undef;
    }
}

foreach my $watchfile (@opt_watchfile) {
    my $uscan=RPM::uscan->new(
	'WATCH_FILE' => $watchfile,
	'NAME' => $opt_packagename,
	'LOCAL_VERSION' => $opt_pkg_version,
	-debug => $debug,
	-verbose => $verbose,
	-timeout => $opt_timeout,
	-any_archive => $opt_any_archive,
	-undef_stringify => '-',
	-opt_override => $ref_opt_override,
	);
    if ($opt_all) {
	foreach my $tag ($uscan->tags()) {
	    my $val=$uscan->get_tag($tag);
	    print "$tag=",defined $val ? $val : '',"\n";
	}
    } else {
	print $uscan->query($opt_queryformat);
    }
}
