#!/usr/bin/perl -w

use strict;
use PVE::SafeSyslog;
use PVE::Tools qw(extract_param);
use PVE::INotify;
use PVE::RPCEnvironment;
use PVE::CLIHandler;
use PVE::JSONSchema qw(get_standard_option);
use PVE::API2::OpenVZ;

use Data::Dumper; # fixme: remove

use base qw(PVE::CLIHandler);

$ENV{'PATH'} = '/sbin:/bin:/usr/sbin:/usr/bin';

initlog('vzrestore');

die "please run as root\n" if $> != 0;

PVE::INotify::inotify_init();

my $rpcenv = PVE::RPCEnvironment->init('cli');

$rpcenv->init_request();
$rpcenv->set_language($ENV{LANG});
$rpcenv->set_user('root@pam'); 

__PACKAGE__->register_method({
    name => 'vzrestore', 
    path => 'vzrestore', 
    method => 'POST',
    description => "Restore OpenVZ containers.",
    parameters => {
    	additionalProperties => 0,
	properties => {
	    vmid => get_standard_option('pve-vmid'),
	    archive => {
		description => "The backup file. You can pass '-' to read from standard input.",
		type => 'string', 
		maxLength => 255,
	    },
	    storage => get_standard_option('pve-storage-id', {
		description => "Target storage.",
		default => 'local',
		optional => 1,
	    }),
	    force => {
		optional => 1, 
		type => 'boolean',
		description => "Allow to overwrite existing container.",
	    },
	},
    },
    returns => { 
	type => 'string',
    },
    code => sub {
	my ($param) = @_;

	$param->{ostemplate} = extract_param($param, 'archive');

	$param->{node} = PVE::INotify::nodename();

	$param->{restore} = 1;

	return PVE::API2::OpenVZ->create_vm($param);
    }});    

my $cmddef = [ __PACKAGE__, 'vzrestore', ['archive', 'vmid'], undef, 
	       sub {
		   my $upid = shift;
		   my $status = PVE::Tools::upid_read_status($upid);
		   exit($status eq 'OK' ? 0 : -1);
	       }];

push @ARGV, 'help' if !scalar(@ARGV);

PVE::CLIHandler::handle_simple_cmd($cmddef, \@ARGV, undef, $0);

exit 0;

__END__

=head1 NAME

vzrestore - restore OpenVZ vzdump backups

=head1 SYNOPSIS

=include synopsis

=head1 DESCRIPTION

Restores OpenVZ vzdump backups.

=head1 SEE ALSO

vzdump(1) qmrestore(1)

=include pve_copyright
