#!/usr/bin/perl -w

# Copyright (c) 2008 Miek Gieben
# See LICENSE for the license

use strict;
use Sys::Hostname;
use Getopt::Long qw(:config no_ignore_case bundling);
use File::Basename;
use File::Spec;
use POSIX;

# common functions
my $prefix="/usr";
my $datarootdir = "${prefix}/share";
require "${datarootdir}/rdup/shared.pl"; 

my $BACKUPROOT;
my $HOST = hostname;
my $DAYSECONDS = 86400;
my $progName = basename $0;
my ($diff, $cat, $copy, $version, $help);

my $abs_path;
my $back = 0;
my %mod = ( d => 1, w => 7, m => 30, y => 365 );
my $d = i18n();

# check if we have a -NUMBER[modifier] option
my $i = 0;
foreach my $opt (@ARGV) {
    if ($opt =~ /-([0-9]+)(d|w|m|y)?/) {
	$back = $1;
	if ($2) {
	    # multiple with the modifier
	    $back *= $mod{$2} 
	}
	splice(@ARGV, $i, 1); # kill the option
	last;
    }
    $i++;
} 

# normal options
GetOptions(h => \$help,
	"H=s" => \$HOST,
	V => \$version,
	"b=s" => \$BACKUPROOT,
	d => \$diff,
	c => \$cat,
	C => \$copy);

usage() if $help;
version($progName) if $version;

if (! defined $BACKUPROOT) {
    die $d->get("** Need the -b option");
}
	
if (! defined $ARGV[0]) {
    die $d->get("** Need a pathname");
}

$abs_path = File::Spec->rel2abs($ARGV[0]);

# convert timestamp to a YYYYMM/DD name
sub time2dir($) {
    my (undef, undef, undef, $d, $m, $y) = localtime(shift);
    $y += 1900;
    $m += 1;
    $m = sprintf("%02d", $m);
    $d = sprintf("%02d", $d);
    if (wantarray) {
	($y, $m, $d);
    } else {
	$y . $m . "/" . $d;
    }
}

# get info on the file, return uid, gid, size
sub backinfo($) {
    my (undef, undef, undef, undef, $uid, $gid, undef,
	    $size) = stat(shift);
    ($uid, $gid, $size);
}

sub usage {
    print "$progName [-NUMBER[d|w|m|y]] [OPTIONS] PATH\n\n";
    print "Look for PATH in the backup and print the last 5 backups\n";
    print "With the -NUMBER option the searching starts NUMBER days ago\n";
    print "The -NUMBER accepts an modifier:\n";
    print "d (day, default), w (week), m (month) and y (year)\n\n";
    print "OPTIONS\n";
    print " -b  PATH  root of the backup directory\n";
    print " -H  HOST  optional hostname, defaults to current host\n";
    print " -c        cat the contents of the last backup of PATH\n";
    print " -C        copy the last backup of PATH to \'basename PATH'.rdup\n";
    print " -d        show the diff -u of the last backup and the current file\n";
    print " -h        this help\n";
    print " -V        print version\n";
    exit 0;
}

# default mode: show 5 entries back from now - $back
# otherwise use the first "hit" to do a cat, diff or copy
# the diff is done with the current file on disk

my $t = time;
$t -= ($back * $DAYSECONDS);
for (my $i = 0; $i < 5; $i++) {
    my $p = "$BACKUPROOT/$HOST/" . time2dir($t - ($i * $DAYSECONDS)) . $abs_path;

    my ($uid, $gid, $size) = backinfo($p);
    # cat
    if ($cat) {
	open F, $p or die "** Can not open \'$p'";
	while(<F>) { print; }
	close F;
	last;
    }
    if ($copy) {
	system "cp", "-i", $p, basename $p . ".rdup";
	last;
    }
    if ($diff) {
	system "diff", "-u", $p, $ARGV[0];
	last;
    }
    if ($uid) {
	# default 
	print time2dir($t - ($i * $DAYSECONDS)) . " " . $uid .
	    " " . $gid . " " . $size . " " . $abs_path . "\n";
    } 
}
