#!/usr/bin/perl

#----------------------------------------------------------------------
# copyright (C) 2006 Charlie Brady
#
# 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
#----------------------------------------------------------------------

package esmith;

use warnings;
use strict;
use esmith::ConfigDB;
use esmith::Backup;

my $event = shift;
my $path = shift;

my $confdb = esmith::ConfigDB->open_ro;
my $backup = $confdb->get('backup') or die "No backup db entry found\n";
my $program = $backup->prop('Program');

exit 0 unless $program eq 'CopyFromDisk';

die("Root path to restore from must be provided\n") unless defined $path && -d $path;
chdir $path or die "Could not chdir to $path: $!";

my $backup_db = new esmith::Backup or die "Couldn't create Backup object\n";

my @restore = $backup_db->restore_list;

die "Can't fork: $!" unless defined(my $pid = open(STDIN, "-|"));
if ($pid)
{
    # parent - extract files to /
    exec(qw(tar -C / -xf -));
}
else
{
    # create archive from $path which are to be restored
    chroot($path) || die "Can't chroot to $path";
    open(STDERR, ">/dev/null");
    exec(qw(tar -C / -cf -), @restore);
}

