#!/usr/bin/perl
#
# wanna-build-merge-quinn: merge current quinn-diff output into wanna-build
# database and make wanna-build statistics
# 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-quinn $date --------------\n";

$tmpdir = "/tmp/merge-quinn.$$";
chomp( $arch = `dpkg --print-architecture` );

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

my $stats_only = 1 if @ARGV >= 1 && $ARGV[0] eq "--stat-only";

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

my ($dist, $sect);

if (!$stats_only) {

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

    foreach $dist (@dists) {
	my $ofile = "quinn-$dist";
	unlink( $ofile );

	foreach $sect (@conf::sections) {
	    my $bf = ($conf::quinn_source_multi_section ?
		      $dist : "by_section-$arch.txt");
	    unlink( $bf );
	    my $f = ($conf::quinn_source_multi_section ?
		     "$conf::quinn_source/$arch/$sect/$dist" :
		     "$conf::quinn_source/$dist/by_section-$arch.txt");
	    if ($f =~ m,^/,) {
		if (system "cat '$f' >>$ofile") {
		    warn "cat $f >>$ofile error status $?\n";
		    next;
		}
	    }
	    else {
		system "wget", "-q", $f and next;
		if (system "cat '$bf' >>$ofile") {
		    warn "cat $bf >>$ofile error status $?\n";
		    unlink( $bf );
		    next;
		}
	    }
	    print "Got $f\n";
	    unlink( $bf );
	}

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

    system "wanna-build", "--remove-maintenance-lock";
    $locked = 0;
}

unlink( "stats" );
foreach $dist (@dists) {
    system "wanna-build-statistics --dist=$dist >>stats";
    system "echo '' >>stats";
}

if (-s "stats" && defined($conf::stat_mail) && $conf::stat_mail) {
    system "mail -s 'wanna-build statistics $date' $conf::stat_mail <stats"
	and warn "mail for statistics error status: $?\n";
    if ($conf::web_stats && -w $conf::web_stats) {
	open( F, ">$conf::web_stats" )
	    or die "Cannot create $conf::web_stats: $!\n";
	print F "wanna-build statistics $date\n";
	print F "===================================================\n\n";
	close( F );
	system "cat stats >>'$conf::web_stats'";
	system "chmod", "644", $conf::web_stats;
    }
}

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

exit( 0 );

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