#!/usr/bin/perl -w

use strict;
use warnings;
use JSON::XS;
use Getopt::Long;

my $branch='sisyphus';
my $import_mode=1;
my $infile=$ENV{'HOME'}.'/.repocop/import/repology/json';
my $verbose=1;
our @LONGOPT=(
    'b|branch=s' => \$branch,
    'v|verbose+' => \$verbose,
    'import' => \$import_mode,
    'verify' => sub {$import_mode=0},
    "in=s"  => \$infile,
);
GetOptions (@LONGOPT) or die("Error in command line arguments\n");

print STDERR "repocop-import-repology: entered verification mode\n" if $verbose && !$import_mode;
if (! -s $infile) {
    print STDERR "repocop-import-repology: $infile not found or empty.\n" if $verbose;
    exit;
}

my $content = &read_json($infile);
my $in=decode_json($content);
my ($pipe_dead_url, $pipe_https_redirect);

&prepare_reports() if $import_mode;

foreach my $entry (@$in) {
    die "Bad file? not a $branch: ",$entry->{'family'} if $entry->{'family'} ne $branch;
    die "oops: arch ",$entry->{'arch'} if $entry->{'arch'};
    my $rawversion=$entry->{'rawversion'};
    $rawversion=~s,^\d+:,,;
    my $srcid=$entry->{'srcname'}.'-'.$rawversion.'.src';
    my $type = $entry->{'type'};
    if ($type eq 'homepage_dead' or $type =~ /^homepage_discontinued_/) {
	#"data": {
	#"url": "http://gitorious.org/ac100/abootimg",
	#"code": -100
	#},
	&report_dead_url($srcid) if $import_mode;
    } elsif ($type eq 'homepage_permanent_https_redirect') {
	&report_https_redirect($srcid) if $import_mode;
    } else {
	die "new type: ", $type;
    }
}

&done_reports() if $import_mode;

sub read_json {
    my $dbfile = shift;
    open (my $fh, '<', $dbfile) || die "failed to open $dbfile: $!";
    local $/ = undef;
    my $content = <$fh>;
    close($fh);
    return $content;
}

sub prepare_reports {
    open ($pipe_dead_url, '|-', 'repocop-test-import-tsv',
	  '-t', 'specfile-url-is-dead',
	  '-s', 'warn',
	  '-m', 'Homepage link in URL is dead or discontinued and should be replaced by alive link (or link to archive.org as a last resort). See https://repology.org/repository/altsisyphus/problems')
	|| die 'Failed to open pipe to repocop-test-import-tsv: ',$!;
    open ($pipe_https_redirect, '|-', 'repocop-test-import-tsv',
	  '-t', 'specfile-url-permanent-https-redirect',
	  '-s', 'experimental',
	  '-m', 'Homepage link in URL is a permanent redirect to its HTTPS counterpart. Please, change URL protocol from http:// to https://. See https://repology.org/repository/altsisyphus/problems')
	|| die 'Failed to open pipe to repocop-test-import-tsv: ',$!;
}

sub done_reports {
    close($pipe_dead_url);
    close($pipe_https_redirect);
}

sub report_dead_url {
    my $srcid = shift;
    print $pipe_dead_url $srcid, "\n";
}

sub report_https_redirect {
    my $srcid = shift;
    print $pipe_https_redirect $srcid, "\n";
}

__END__

sub report_dead_url0 {
    my $srcid = shift;
    &run('repocop-test-warn', '-k', $srcid, '-t', 'specfile-url-is-dead', 'Homepage link in URL is dead or discontinued and should be replaced by alive link (or link to archive.org as a last resort). See https://repology.org/repository/altsisyphus/problems');
}

sub report_https_redirect0 {
    my $srcid = shift;
    &run('repocop-test-experimental', '-k', $srcid, '-t', 'specfile-url-permanent-https-redirect', 'Homepage link in URL is a permanent redirect to its HTTPS counterpart. Please, change URL protocol from http:// to https://. See https://repology.org/repository/altsisyphus/problems');
}

sub run {
    my (@cmd) =@_;
    die 'failed ',@cmd if 0!=system(@cmd);
}
