#!/usr/bin/perl
#
# Copyright (c) 2006 Zmanda Inc.  All Rights Reserved.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 2 as published
# by the Free Software Foundation.
#
# 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.,
# 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
#
# Contact information: Zmanda Inc, 505 N Mathlida Ave, Suite 120
# Sunnyvale, CA 94085, USA, or: http://www.zmanda.com
#
#

use strict;
use warnings;
use File::Spec::Functions;
use lib "/usr/local/lib/mysql-zrm";
use ZRM::Common;


#usage strings 
my $USAGE_LIST_STRING=  
		"\t\t[--source-directory <directory name>]\n".  
		"\t\t[--all-backups|--till-lastfull]\n".
		"\t\t[--index|--noindex]\n";
my @LISTOPT = qw/
		source-directory=s
		index!
		status
		stop-before=s
              	all-backups
             	till-lastfull
		/;
my @dirList;
my %checkForDir;

#outputs contents to STDOUT
#$_[0] gives the name of the directory
sub listIndexContents()
{
	#if( !$_[0] ){
		#return;
	#}
	if( ! -d $_[0] ){
		&printWarning( "$_[0] not a valid directory\n" );
		return;
	}
	my $filename = catfile( $_[0], $INDEX_FILENAME );
	my $r = &parseIndexFile( $filename );
	if( $r == 0 ){
		&printError( "Could not find index file in $_[0]\n" );
		return;
	}
	my $msg = $indexdata{"backup-level"}." ".$indexdata{"backup-directory"};
	if( defined $inputs{"status"} ){
		if( !defined $indexdata{"backup-status"} ){
			$indexdata{"backup-status"} = "Backup failed";
		}
		$msg .= " ".$indexdata{"backup-date"}." ".$indexdata{"backup-status"};	
	}
	print $msg."\n";
	if( !defined $inputs{"index"} || $inputs{"index"} > 0 ){
		print "Index File\n{";
		for( keys%indexdata )
		{
       			print "\t".$_."=".$indexdata{$_}."\n";
		}
		print "}\n";
	}
	%indexdata = ();
}

#Create a list of backup run directories before the given one(inclusive)
#Result will be in the global @dirList;
#$_[0] directory from which to create list;
sub getListOfDirectories()
{
	my $dir = $_[0];
	my $filename = catfile( $dir, $INDEX_FILENAME );
        unshift @dirList, $dir;
        my $r = &parseIndexFile( $filename );
	if( $r == 0 ){
		print "ERROR: Looks like backup data for $dir is not available\n";
		return;
	}
	if( $indexdata{"backup-level"} == 0 &&
	    !defined $inputs{"all-backups"} ){
		return;
	}
        if( defined $indexdata{"last-backup"} ){
		if( $checkForDir{$indexdata{"last-backup"}} ){
			&printError( "Loop detected in the backup directory $dir\n" );
			print "ERROR: Loop detected in the backup directories.\nERROR: $dir has its previous backup pointing to a backup directory ";
                        print $indexdata{"last-backup"}." that is pointed to by another backup ";
			print $checkForDir{$indexdata{"last-backup"}}." in this list\n"; 
			return;
		}else{
			$checkForDir{$indexdata{"last-backup"}}=$dir;
		}
		return $indexdata{"last-backup"};
	}
        return;
}


#This executes the list action
sub doList()
{
	if( !$inputs{"source-directory"} ) {
		if( !$lastBackupDir ){
			print "Nothing to list\n";
			return;
		}
		$inputs{"source-directory"}=$lastBackupDir;
	}
	if( !-d $inputs{"source-directory"} ) {
		&printAndDie( "Cannot find source directory ".$inputs{"source-directory"}."\n" );
	}

	if( defined $inputs{"all-backups"} && defined $inputs{"till-lastfull"} ){
		&printAndDie( "all-backups and till-lastfull should not be specified at the same time\n" );
	}
	
	if( !defined $inputs{"all-backups"} && 
	    !defined $inputs{"till-lastfull"} ) {
		&listIndexContents( $inputs{"source-directory"} );
		return;
	}
	my $dir = $inputs{"source-directory"};
	while( defined $dir ){
		if( defined $inputs{"stop-before"} && 
		    $inputs{"stop-before"} eq $dir ){
			last;
		}
		$dir = &getListOfDirectories( $dir );
	}
	foreach( @dirList ){
		&listIndexContents( $_ );
	}
}

#Sets up defaults for backup
sub setupDefaults()
{
        $action = "list";
        $USAGE_STRING = $USAGE_LIST_STRING.$USAGE_COMMON_OPTIONS_STRING;
}

sub main()
{
	&setupDefaults();
	&initCommon(@LISTOPT);
	&createConfigFile();
	&doList();
	&my_exit();
}

&main();
