#!/usr/bin/perl
#
# Copyright (C) 2005,2006 Mandriva
# 
# Author: Florent Villard <warly@mandriva.com>
#
# 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, 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, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
# upload packages in queue when all the mandatory architectures are done
#
# PREFIX : sprintf "$year%02d%02d%02d%02d%02d.$user.$host.${$}_", $mon, $mday, $hour, $min, $sec;

use strict;
use Iurt::Config qw(config_usage config_init);
use Iurt::Process qw(check_pid);
use Iurt::Queue qw(get_upload_tree_state);
use Iurt::Util qw(plog_init plog);
use Iurt::Emi qw(find_prefixes_ready_to_upload record_uploaded_packages upload_prefix_in_media);
use MDK::Common qw(cat_);

my %run;
my $program_name = 'emi';
$run{program_name} = $program_name;

my $LOG;
if (!$ENV{EMI_LOG_FILE} || !open($LOG, '>>', $ENV{EMI_LOG_FILE})) {
    open($LOG, ">&STDERR");
}

plog_init($program_name, $LOG, 7, 1);

my $HOME = $ENV{HOME};

my $configfile = "$HOME/.upload.conf";
my $sysconfigfile = "/etc/iurt/upload.conf";

my $config = {};
foreach my $f ($configfile, $sysconfigfile) {
    plog('DEBUG', "load config: $f");
    if (-f $f) {
        $config = eval(cat_($f))
          or die "FATAL $program_name: syntax error in $f";
        last;
    }
}

my %config_usage = ( 
    admin => {
	desc => 'mail address of the bot administrator',
	default => 'distrib-admin@mandrivalinux.org'
    },
    'arch' => {  
	desc => "List of arch",
	default => [ 'i586', 'x86_64', 'ppc' , 'sparcv9' ]
    },
    'arch_translation' => {  
	desc => "Renaming of arch",
	default => { 'sparc64' => 'sparcv9' }
    },
    http_queue => {
	desc => 'Address where log can be consulted',
	default => 'http://kenobi.mandriva.com/queue/'
    },
    mandatory_arch => {
	desc => 'List of mandatory architecture to be able to upload',
	default => [ 'i586', 'x86_64' ]
    },
    tmp => {
	desc => "Temporary directory",
	default => "$HOME/tmp"
    },
    queue => {
	desc => 'root directory of the various upload queues',
	default => "$HOME/uploads"
    },
);

config_usage(\%config_usage, $config) if $run{config_usage};
config_init(\%config_usage, $config, \%run);

$run{pidfile_home} = $config->{tmp};
$run{pidfile} = "upload";
my $pidfile = check_pid(\%run);

my $todo = "$config->{queue}/todo";
my $done = "$config->{queue}/done";
my $reject = "$config->{queue}/rejected";

my %pkg_tree = get_upload_tree_state($config);
my %targets = find_prefixes_ready_to_upload($config, %pkg_tree);

foreach my $target (keys %targets) {
    foreach my $media (keys %{$targets{$target}}) {
    	my %is_finisher;
	
	foreach (values %{$targets{$target}{$media}{arch_finisher}}) {
	    $is_finisher{$_} = 1;
	}

	# TODO: We should run several of those in parallel
	foreach my $prefix (@{$targets{$target}{$media}{to_upload}}) {
	    next if $is_finisher{$prefix};
	    upload_prefix_in_media($config, \%pkg_tree, $prefix, $media);
	}

	foreach my $prefix (keys %is_finisher) {
	    upload_prefix_in_media($config, \%pkg_tree, $prefix, $media, 1);
	}

	# Now that the finishers are done, metadata was updated so the packages are available
	foreach my $prefix (@{$targets{$target}{$media}{to_upload}}) {
	    my $path = $pkg_tree{$prefix}{media}{$media}{path};
	    record_uploaded_packages($config, \%pkg_tree, $prefix, $media) unless -f "$reject/$path/$prefix.youri";;
	}
    }
}

unlink($pidfile);
exit();

