#!/usr/bin/env perl
# vi: set ts=4 sw=4 :

use warnings;
use strict;

my $show_kept;
my $show_removed;
my $dry_run;
my $just_echo;

use Getopt::Long;
Getopt::Long::Configure(qw( no_ignore_case ));
GetOptions(
    "k"         => \$show_kept,
    "r"         => \$show_removed,
    "n"         => \$dry_run,
    "e"         => \$just_echo,
) or exit 2;

my $errors = 0;
@ARGV or warn "No directories specified - nothing to do\n";
prune_dir($_) for @ARGV;
exit 1 if $errors > 0;
exit;

sub prune_dir
{
    my ($fullexport) = @_;

    my @dirs;
    opendir(my $dh, $fullexport) or die "opendir $fullexport: $!\n";
    push @dirs, sort
        grep { -d "$fullexport/$_" }
        grep /\A\d\d\d\d\d\d\d\d-\d\d\d\d\d\d\z/,
        readdir $dh;
    closedir $dh;

    @dirs or warn "No exports found in $fullexport - are you sure this is a fullexport directory?\n";

    # Keep the newest 2 backups
    for (1..2)
    {
        defined(my $dir = pop @dirs) or last;
        print "Keeping $fullexport/$dir\n" if $show_kept;
    }

    for my $dir (@dirs)
    {
        print "Removing $fullexport/$dir\n" if $show_removed;
        next if $dry_run;

        my @echo = ($just_echo ? ("echo") : ());
        system @echo, "/bin/rm", "-rf", "$fullexport/$dir";

        unless ($? == 0)
        {
                warn "'rm -rf $fullexport/$dir' failed (rc=$?)\n";
                ++$errors;
        }
    }
}

# eof
