#!/usr/bin/perl -w
#
# Rubi's Ultimate Backup Implementation
#
# Version 4.0, (C) 3.1999 by Beat Rubischon <beat@rubis.ch>
#                  8.1999 by Beat Rubischon <beat.rubischon@trivadis.com>
#                  2.2000 by Beat Rubischon <beat@rubis.ch>
#                  2.2004 by Beat Rubischon <rubischon@phys.ethz.ch>
#                  7.2007 by Beat Rubischon <beat@0x1b.ch>
#                 10.2010 by Beat Rubischon <beat.rubischon@dalco.ch>
#                 12.2015 by Beat Rubischon <beat.rubischon@dalco.ch>
#
# 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, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# $Id: cleanup.pl,v 1.2 2015/12/01 16:20:58 beat Exp $
#
# Cleanup backup directories

use POSIX qw(strftime);
use strict;

my $BASEDST=shift || die "Need base directory";
my $KEEPG=shift || die "Need amount of generations";
my $KEEPD=shift || die "Need days to keep broken and unknown backups";
my $time=time;

my $OLDDST="";
if (-f $BASEDST."/lastdst") {
  open IN, $BASEDST."/lastdst";
  $OLDDST=<IN>;
  chomp $OLDDST;
}

# scan directory
my %backups;
my $x=0;
for my $file (<$BASEDST/*>) {
  if (-d $file) {
    my $mtime=(stat(_))[9];
    $backups{$mtime."-".sprintf("%05i", $x)}=$file;
    $x++;
  }
}

# process sorted list
my $kept=0;
my @remove;
foreach my $key (sort {$b cmp $a} keys %backups) {
  my $file=$backups{$key};
  my $mtime=(split(/-/, $key))[0];

  # check log
  my $ok=-1;
  if (-f $file.".log") {
    $ok=0;
    open IN, $file.".log";
    while(<IN>) {
      if (/\* \* \* Backup successfull \* \* \*/) {
        $ok=1;
        last;
      }
    }
    close IN;
  }

  # check for current backup
  my $current=0;
  if ($file eq $OLDDST) {
    $current=1;
  }

  # judge backups
  my $keep=0;
  if ($current) {
    $keep=1;
    $kept++;
  } elsif ($ok == 1 and $kept < $KEEPG) {
    $keep=1;
    $kept++;
  } elsif ($ok == -1 and ($mtime + $KEEPD * 86400) > $time and $kept < $KEEPG) {
    $keep=1;
    $kept++;
  } elsif ($mtime + $KEEPD * 86400 > $time) {
    $keep=1;
  }

  # inform user
  if ($keep) {
    print "* ";
  } else {
    print "  ";
  }
  print $file." ".strftime("%a %b %e %Y %H:%M", localtime($mtime));
  if ($current) {
    print " current";
  } else {
    if ($ok) {
      if ($ok==-1) {
        print " unknown";
      } else {
        print " OK";
      }
    } else {
      print " fail";
    }
  }
  print "\n";

  # add expired backups
  if (! $keep) {
    push @remove, $file;
    if ($ok != -1) {
      push @remove, $file.".log";
    }
  }
}

# remove expired backups
if (@remove > 0) {
  system "/bin/rm", "-Rf", @remove;
}
