#!/usr/bin/perl -w
#
# Copyright © 2006-2008 Roger Leigh <rleigh@debian.org>
#
# 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/>.
#
#######################################################################

use strict;
use warnings;

use Sbuild::ChrootSetup qw(update upgrade distupgrade clean autoclean
                           autoremove);

package Conf;

use Sbuild::Conf;

BEGIN {
    use Exporter ();
    our (@ISA, @EXPORT);

    @ISA = qw(Exporter Sbuild::Conf);

    @EXPORT = qw();
}

sub init_allowed_keys {
    my $self = shift;

    $self->SUPER::init_allowed_keys();

    my %update_keys = (
	'COMPAT'				=> {
	    DEFAULT => 1
	},
	'UPDATE'				=> {
	    DEFAULT => 0
	},
	'UPGRADE'				=> {
	    DEFAULT => 0
	},
	'DISTUPGRADE'				=> {
	    DEFAULT => 0
	},
	'CLEAN'				=> {
	    DEFAULT => 0
	},
	'AUTOCLEAN'			=> {
	    DEFAULT => 0
	},
	'AUTOREMOVE'			=> {
	    DEFAULT => 0
	},
    );

    $self->set_allowed_keys(\%update_keys);
}

package Options;

use Sbuild::OptionsBase;
use Sbuild::Conf;

BEGIN {
    use Exporter ();
    our (@ISA, @EXPORT);

    @ISA = qw(Exporter Sbuild::OptionsBase);

    @EXPORT = qw();
}

sub set_options {
    my $self = shift;

    $self->add_options(
	"update|u" => sub {
	    $self->set_conf('UPDATE', 1);
	    $self->set_conf('COMPAT', 0);
	},
	"upgrade|g" => sub {
	    $self->set_conf('UPGRADE', 1);
	    $self->set_conf('COMPAT', 0);
	},
	"dist-upgrade|d" => sub {
	    $self->set_conf('DISTUPGRADE', 1);
	    $self->set_conf('COMPAT', 0);
	},
	"clean|c" => sub {
	    $self->set_conf('CLEAN', 1);
	    $self->set_conf('COMPAT', 0);
	},
	"autoclean|a" => sub {
	    $self->set_conf('AUTOCLEAN', 1);
	    $self->set_conf('COMPAT', 0);
	},
	"autoremove|r" => sub {
	    $self->set_conf('AUTOREMOVE', 1);
	    $self->set_conf('COMPAT', 0);
	});
}

package main;

use Getopt::Long;
use Sbuild qw(help_text version_text usage_error);
use Sbuild::Utility qw(setup cleanup);

my $conf = Conf->new();
exit 1 if !defined($conf);
my $options = Options->new($conf, "sbuild-update", "1");
exit 1 if !defined($options);
$conf->check_group_membership();

if ($conf->get('COMPAT')) {
    my $msg = "$0 will perform apt-get command 'update' now, however this ";
    $msg .= "may change at a later revision.\n";
    print "$msg";
    $conf->set('UPDATE', 1);
}

usage_error("sbuild-update", "Incorrect number of options") if (@ARGV < 1);

my $status = 1;

foreach (@ARGV) {
    my $chroot = Sbuild::Utility::get_dist($_);

    my $session = setup('source', $ARGV[0], $conf) or die "Chroot setup failed";

    if (!$session->lock_chroot('SBUILD_UPDATE', $$, $conf->get('USERNAME'))) {
	goto cleanup_unlocked;
    }

    if ($conf->get('UPDATE')) {
	print "Performing update.\n";
	$status = update($session, $conf);
	$status >>= 8;
	if ($status) {
	    print STDERR "Exiting from update with status $status.\n";
	    goto cleanup;
	}
    }

    if ($conf->get('UPGRADE')) {
	print "Performing upgrade.\n";
	my $status = upgrade($session, $conf);
	$status >>= 8;
	if ($status) {
	    print STDERR "Exiting from upgrade with status $status.\n";
	    goto cleanup;
	}
    }

    if ($conf->get('DISTUPGRADE')) {
	print "Performing dist-upgrade.\n";
	my $status = distupgrade($session, $conf);
	$status >>= 8;
	if ($status) {
	    print STDERR "Exiting from distupgrade with status $status.\n";
	    goto cleanup;
	}
    }

    if ($conf->get('CLEAN')) {
	print "Performing clean.\n";
	my $status = clean($session, $conf);
	$status >>= 8;
	if ($status) {
	    print STDERR "Exiting from update with status $status.\n";
	    goto cleanup;
	}
    }

    if ($conf->get('AUTOCLEAN')) {
	print "Performing autoclean.\n";
	my $status = autoclean($session, $conf);
	$status >>= 8;
	if ($status) {
	    print STDERR "Exiting from upgrade with status $status.\n";
	    goto cleanup;
	}
    }

    if ($conf->get('AUTOREMOVE')) {
	print "Performing autoremove.\n";
	my $status = autoremove($session, $conf);
	$status >>= 8;
	if ($status) {
	    print STDERR "Exiting from distupgrade with status $status.\n";
	    goto cleanup;
	}
    }

cleanup:
    # Unlock chroot now it's cleaned up and ready for other users.
    $session->unlock_chroot();

cleanup_unlocked:
    cleanup($conf);

    exit($status ? 1 : 0);
}

exit 0;
