#!/usr/bin/perl

use strict;
use lib '.', 't';
use helper;
use Config;
use Cwd;
use File::Basename 'dirname';
use List::Util 'uniq';
use Test::More 'no_plan';

# help CPAN testers who installed genhdlist2 using cpan but do not have /usr/local/bin in their PATH:
$ENV{PATH} .= ":/usr/local/bin:$Config{bin}";

# help when CPAN testers have not genhdlist2 installed but do have built rpmtools:
$ENV{PATH} .= join(':', uniq(map {
	my $blib_script = dirname($_) . "/script";
	-d $blib_script ? $blib_script : ();
	} split(':', $ENV{PERL5LIB})));
warn ">> Final PATH is $ENV{PATH}\n";

chdir 't' if -d 't';
system('rm -rf tmp media');
foreach (qw(media tmp tmp/BUILD tmp/RPMS tmp/RPMS/noarch tmp/SRPMS)) {
    mkdir $_;
}
my $genhdlist2 = 'genhdlist2 --xml-info';

my $whereis_genhdlist2 = qx(whereis genhdlist2);
ok("whereis genhdlist2", "genhdlist2 emplacement= $whereis_genhdlist2");
warn ">> genhdlist2: $whereis_genhdlist2\n";
warn ">> FAILED TO FIND genhdlist2\n" if !$whereis_genhdlist2;

# check that genhdlist2 actually works:
my $dir = "genhdlist2-test";
mkdir($dir);
my $out = `genhdlist2 $dir 2>&1`;
chomp($out);
my $expected = "no *.rpm found in $dir (use --allow-empty-media?)";
is($out, $expected, "genhdlist2 works");
warn ">> genhdlist test output=<<$out>>\n" if $out ne $expected;

# locally build test rpms

foreach my $dir (grep { -d $_ } glob("data/SPECS/*")) {
    my ($medium_name) = $dir =~ m!([^/]*)$!;
    next if $medium_name eq 'suggests' && !are_weak_deps_supported();
    rpmbuild($_, $medium_name) foreach glob("$dir/*.spec");
    genhdlist_std($medium_name);
}

foreach my $spec (glob("data/SPECS/*.spec")) {
    warn "SKIPPING /rpm-query-in-scriptlet/" if $spec =~ /rpm-query-in-scriptlet/ && $Config{archname} =~ /bsd/;
    next if $spec =~ /rpm-query-in-scriptlet/ && $Config{archname} =~ /bsd/;
    my $name = rpmbuild($spec);

    if ($name eq 'various') {
	system_("cp -r media/$name media/${name}_nohdlist");
	system_("cp -r media/$name media/${name}_no_subdir");
	system_("$genhdlist2 media/${name}_no_subdir");
	symlink "${name}_nohdlist", "media/${name} nohdlist";
    }
    genhdlist_std($name);
}
foreach my $spec (glob("data/SPECS/srpm*.spec")) {
    my $name = rpmbuild_srpm($spec);
    genhdlist_std($name);
}

{
    my $name = 'rpm-v3';
    system_("cp -r data/$name media");
    system_("cp -r media/$name media/${name}_nohdlist");
    system_("cp -r media/$name media/${name}_no_subdir");
    system_("$genhdlist2 media/${name}_no_subdir");
    genhdlist_std($name);
}

mkdir 'media/reconfig';
system_("cp -r data/reconfig.urpmi media/reconfig");

mkdir 'media/media_info';
system_("cp -r data/media.cfg media/media_info");
system_('gendistrib -s .');

sub genhdlist_std {
    my ($medium_name) = @_;
    system_("$genhdlist2 media/$medium_name");
}

sub rpmbuild {
    my ($spec, $o_medium_name) = @_;

    my $dir = getcwd();
    my ($target) = $spec =~ m!-(i586|x86_64)\.spec$!;
    # unsetting %__os_install_post fixes failure to build on FreeBSD:
    my $nopost = " --define '__os_install_post %nil'";
    system_("rpmbuild --define 'rpm_version %(rpm -q --queryformat \"%{VERSION}\" rpm|sed -e \"s/\\\\.//g\")' --define '_topdir $dir/tmp' --define '_tmppath $dir/tmp' -bb --clean --nodeps $nopost ".($target ? "--target $target" : "")." $spec");

    my ($name) = $spec =~ m!([^/]*)\.spec$!;

    my $medium_name = $o_medium_name || $name;
    mkdir "media/$medium_name";
    system_("mv tmp/RPMS/*/*.rpm media/$medium_name");

    $medium_name;
}

sub rpmbuild_srpm {
    my ($spec) = @_;

    system_("rpmbuild --quiet --define '_topdir tmp' -bs --clean --nodeps $spec");

    my ($name) = $spec =~ m!([^/]*)\.spec$!;

    my $medium_name = "SRPMS-$name";
    mkdir "media/$medium_name";
    system_("mv tmp/SRPMS/*.rpm media/$medium_name");

    $medium_name;
}
