#!/usr/bin/perl
#
# wanna-build-merge-packages: merge current Packages and Sources files into
# wanna-build database
# Copyright © 1998-2000 Roman Hodek <Roman.Hodek@informatik.uni-erlangen.de>
#
# 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/>.
#
#######################################################################

package conf;
use WannaBuild::Conf;

package main;

sub cleanup ($);

use strict;
use warnings;
use vars qw($HOME $tmpdir $arch @dists $locked);

$HOME = $ENV{'HOME'}
or die "HOME not defined in environment!\n";
@dists = qw(stable frozen unstable);

open( STDOUT, ">>$HOME/lib/merge.log" );
open( STDERR, ">&STDOUT" );
$| = 1;
select(STDERR); $| = 1; select(STDOUT);
chomp( my $date = `date` );
print "-------------- merge-packages $date --------------\n";

$tmpdir = "/tmp/merge-packages.$$";
$arch = `dpkg --print-architecture`;

# make backups of databases
my $d;
foreach $d (@dists) {
    my $db = "$conf::basedir/$conf::dbbase-$d";
    system "cp", "-p", $db, "$db.bak" if -d $db;
}

mkdir( $tmpdir, 0755 ) or die "mkdir $tmpdir: $!\n";
chdir( $tmpdir ) or die "chdir $tmpdir: $!\n";

foreach (qw(HUP INT QUIT PIPE TERM __DIE__)) {
    $SIG{$_} = \&cleanup;
}

system "wanna-build", "--create-maintenance-lock"
    and die "wanna-build --create-maintenance-lock error status $?\n";
$locked = 1;

# fetch Packages files and run wanna-build
my ($dist, $sect);
foreach $dist ("proposed-updates", @dists) {
    my @sects = ($dist eq "proposed-updates") ? ("") : @conf::sections;
    my $bindir = ($dist eq "proposed-updates") ? "" : "binary-$arch";
    my $sect;

    foreach $sect (@sects) {
	unlink( "Packages", "Packages.gz" );
	my $f = "$conf::pkgs_source/dists/$dist/$sect/$bindir/Packages.gz";
	if ($f =~ m,^/,) {
	    if (system "gunzip -dc '$f' >Packages") {
		warn "gunzip Packages.gz error status $?\n";
		unlink( "Packages" );
		next;
	    }
	}
	else {
	    system "wget", "-q", $f and next;
	    if (system "gunzip", "Packages.gz") {
		warn "gunzip Packages.gz error status $?\n";
		unlink( "Packages", "Packages.gz" );
		next;
	    }
	}
	print "Got $f\n";
	if (! -s "Packages") {
	    warn "$0: Packages file for $dist/$sect is zero length; skipping\n";
	    unlink( "Packages" );
	    next;
	}

	my $d = ($dist eq "proposed-updates") ? "stable" : $dist;
	next if ! -f "$conf::basedir/$conf::dbbase-$d";
	print "--> $dist/$sect\n";
	system "wanna-build", "--merge-packages", "-v", "--dist=$d", "Packages"
	    and warn "wanna-build --merge-packages error status $?\n";
	unlink( "Packages" );
    }
}

# make Sources files
foreach $dist (@dists) {
    foreach $sect (@conf::sections) {
	unlink( "Sources", "Sources.gz" );
	my $f = "$conf::pkgs_source/dists/$dist/$sect/sources/Sources.gz";
	if ($f =~ m,^/,) {
	    if (system "gunzip -dc '$f' >Sources") {
		warn "gunzip Sources.gz error status $?\n";
		unlink( "Sources" );
		next;
	    }
	}
	else {
	    system "wget", "-q", $f and next;
	    if (system "gunzip", "Sources.gz") {
		warn "gunzip Sources.gz error status $?\n";
		unlink( "Sources", "Sources.gz" );
		next;
	    }
	}
	print "Got $f\n";

	if (! -s "Sources") {
	    warn "$0: Sources file for $dist/$sect is zero length; skipping\n";
	    unlink( "Sources" );
	    next;
	}

	next if ! -f "$conf::basedir/$conf::dbbase-$dist";
	print "--> $dist/$sect (src)\n";
	system "wanna-build", "--merge-sources", "-v", "--dist=$dist","Sources"
	    and warn "wanna-build --merge-sources error status $?\n";
	unlink( "Sources" );
    }
}

system "wanna-build", "--remove-maintenance-lock"
    and die "wanna-build --remove-maintenance-lock error status $?\n";
$locked = 0;

chdir( "/" ) or die "chdir /: $!\n";
system "rm", "-rf", $tmpdir and die "rm -rf $tmpdir error status $?";

system "buildd-addpkg", "--clean"
    if -x "/usr/bin/buildd-addpkg";

exit( 0 );

sub cleanup ($) {
    system "wanna-build", "--remove-maintenance-lock" if $locked;
    system "rm -rf $tmpdir" if $tmpdir;
}
