#!/usr/bin/perl -w
#
# Copyright © 2005-2009  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(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 = (
	'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(
	"clean|c" => sub {
	    $self->set_conf('CLEAN', 1);
	},
	"autoclean|a" => sub {
	    $self->set_conf('AUTOCLEAN', 1);
	},
	"autoremove|r" => sub {
	    $self->set_conf('AUTOREMOVE', 1);
	});
}

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-clean", "1");
exit 1 if !defined($options);
$conf->check_group_membership();

if ( ! $conf->get('CLEAN') && ! $conf->get('AUTOCLEAN') &&
    ! $conf->get('AUTOREMOVE') ) {
    $conf->set('CLEAN', 1);
}

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

foreach (@ARGV) {

    my $chroot = Sbuild::Utility::get_dist($_);

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

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

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

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

    cleanup($conf);
}

exit 0;
