#!/usr/bin/perl -w
###############################
#Made on 30 Oct 2004
#--Avinash Prasad
###############################

#user options
use Getopt::Std;

#to handle user interrupt
$SIG{'INT'}='CLEANUP';
$SIG{'HUP'}='CLEANUP';

my($autoloader,$tapedev);
my($tapeno,$owner,$dir);
my %option;

$autoloader="/dev/sg0";
$tapedev="/dev/nst0";
$flag=1;

#get user options
getopts('f:d:t:h',\%option);
if ($option{h}) { print "Usage: getbackup.pl -u user -d dir\n";
		  print "Following options are supported\n";
		  print "	[-d] directory to be recovered\n";
		  print "	[-t] the tape number\n";
		  print "	[-h] help\n";
                  print "       [-f filename] read data in loop from a file\n";
		  exit 0;
		 }
if ($option{f}) { $input_file=$option{f};
                  print "Processing Data from $input_file\n";
                  open (INFILE,$input_file) || die "failed to open $input_file\n";
                  while(<INFILE>)
                  {
                    @fields=split(/:/);
                    chomp @fields;
                    $dir=$fields[0];
		    $tapeno=$fields[1];
                    &FetchBackup;
                  }
                  exit;
                }
else
{

(($option{d})&&($option{t}))||die "Insufficient options use -h to see options\n";
if ($option{d}) { $dir=$option{d}; } else {die "No directory name specified\n";}
if ($option{t}) { $tapeno=$option{t};} else {die "The tapeno isn't specified\n";}
&FetchBackup;
}

sub FetchBackup{
my @returnedval;
print "Recovering: ",$dir," ",$tapeno,"\n";	
print `/usr/sbin/mtx -f $autoloader load $tapeno`;
print "\n";
print `/bin/mt -f $tapedev rewind`;
print "\n";
print `/bin/tar -xvf $tapedev $dir`;
print "\n";
print `/bin/mt -f $tapedev rewind`;
print "\n";
print `/bin/mt -f $tapedev eject`;
print "\n";
print `/usr/sbin/mtx -f $autoloader unload`;
print "\n";
print "Data extraction completed\n";
}
