#! /usr/bin/perl
# Copyright (c) 2009-2012 Zmanda, Inc.  All Rights Reserved.
# Copyright (c) 2013-2016 Carbonite, 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
# 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
#
# Contact information: Carbonite Inc., 756 N Pastoria Ave
# Sunnyvale, CA 94086, USA, or: http://www.zmanda.com

use lib '/usr/lib/arm-linux-gnueabihf/amanda/perl';
use strict;
use warnings;

use Getopt::Long;

use Amanda::Device qw( :constants );
use Amanda::Debug qw( :logging );
use Amanda::Config qw( :init :getconf config_dir_relative );
use Amanda::Util qw( :constants );
use Amanda::Changer;
use Amanda::Constants;
use Amanda::MainLoop;
use Amanda::MainLoop qw( :GIOCondition );
use Amanda::Header;
use Amanda::Holding;
use Amanda::Cmdline;
use Amanda::Tapelist;
use Amanda::Xfer qw( :constants );

sub usage {
    my ($msg) = @_;
    print STDERR "$msg\n" if $msg;
    print STDERR <<EOF;
Usage: amrestore [--config config] [-b blocksize] [-r|-c|-C] [-p] [-h]
    [-f filenum] [-l label] [--exact-match] [-o configoption]*
    [--continue-on-filter-error]
    {device | [--holding] holdingfile}
    [hostname [diskname [datestamp [hostname [diskname [datestamp ... ]]]]]]"));
EOF
    exit(1);
}

##
# main

Amanda::Util::setup_application("amrestore", "server", $CONTEXT_CMDLINE, "amanda", "amanda");

my $config_overrides = new_config_overrides($#ARGV+1);

my ($opt_config, $opt_blocksize, $opt_raw, $opt_compress, $opt_compress_best,
    $opt_pipe, $opt_header, $opt_filenum, $opt_label, $opt_holding,
    $opt_restore_src, $opt_exact_match);

my $opt_continue_on_filter_error = 0;

debug("Arguments: " . join(' ', @ARGV));
Getopt::Long::Configure(qw(bundling));
GetOptions(
    'version' => \&Amanda::Util::version_opt,
    'help|usage|?' => \&usage,
    'config=s' => \$opt_config,
    'holding' => \$opt_holding,
    'exact-match' => \$opt_exact_match,
    'continue-on-filter-error' => \$opt_continue_on_filter_error,
    'b=i' => \$opt_blocksize,
    'r' => \$opt_raw,
    'c' => \$opt_compress,
    'C' => \$opt_compress_best,
    'p' => \$opt_pipe,
    'h' => \$opt_header,
    'f=i' => \$opt_filenum,
    'l=s' => \$opt_label,
    'o=s' => sub { add_config_override_opt($config_overrides, $_[1]); },
) or usage();

$opt_compress = 1 if $opt_compress_best;

# see if we have a holding file or a device
usage("Must specify a device or holding-disk file") unless (@ARGV);
$opt_restore_src = shift @ARGV;
if (!$opt_holding) {
    $opt_holding = 1
	if (Amanda::Holding::get_header($opt_restore_src));
}

my $cmd_flags = $Amanda::Cmdline::CMDLINE_PARSE_DATESTAMP;
$cmd_flags |= $Amanda::Cmdline::CMDLINE_EXACT_MATCH if $opt_exact_match;
my @opt_dumpspecs = Amanda::Cmdline::parse_dumpspecs([@ARGV], $cmd_flags);

usage("Cannot check a label on a holding-disk file")
    if ($opt_holding and $opt_label);
usage("Cannot use both -r (raw) and -c/-C (compression) -- use -h instead")
    if ($opt_raw and $opt_compress);

# -r implies -h, plus appending ".RAW" to filenames
$opt_header = 1 if $opt_raw;

set_config_overrides($config_overrides);
if ($opt_config) {
    config_init_with_global($CONFIG_INIT_EXPLICIT_NAME, $opt_config);
} else {
    config_init($CONFIG_INIT_GLOBAL, undef);
}
my ($cfgerr_level, @cfgerr_errors) = config_errors();
if ($cfgerr_level >= $CFGERR_WARNINGS) {
    config_print_errors();
    if ($cfgerr_level >= $CFGERR_ERRORS) {
	die("errors processing config file");
    }
}

Amanda::Util::finish_setup($RUNNING_AS_DUMPUSER);

my $exit_status = 0;
my $res;

sub failure {
    my ($msg, $finished_cb) = @_;
    print STDERR "ERROR: $msg\n";
    $exit_status = 1;
    if ($res) {
	$res->release(finished_cb => sub {
	    # ignore error
	    $finished_cb->();
	});
    } else {
	$finished_cb->();
    }
}

sub main {
    my ($finished_cb) = @_;

    my $dev;
    my $hdr;
    my $chg;
    my $filenum = $opt_filenum;
    $filenum = 1 if (!$filenum);
    $filenum = 0 + "$filenum"; # convert to integer
    my %all_filter;
    my $r                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       