#!/usr/clearos/sandbox/usr/bin/php
<?php

/**
 * ClearOS Configuration Restore script.
 *
 * @category   apps
 * @package    configuration-backup
 * @subpackage scripts
 * @author     ClearFoundation <developer@clearfoundation.com>
 * @copyright  2008-2015 ClearFoundation
 * @license    http://www.gnu.org/copyleft/gpl.html GNU General Public License version 3 or later
 * @link       http://www.clearfoundation.com/docs/developer/apps/configuration_backup/
 */

///////////////////////////////////////////////////////////////////////////////
//
// 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 3 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, see <http://www.gnu.org/licenses/>.
//
///////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////
// B O O T S T R A P
///////////////////////////////////////////////////////////////////////////////

$bootstrap = getenv('CLEAROS_BOOTSTRAP') ? getenv('CLEAROS_BOOTSTRAP') : '/usr/clearos/framework/shared';
require_once $bootstrap . '/bootstrap.php';

///////////////////////////////////////////////////////////////////////////////
// T R A N S L A T I O N S
///////////////////////////////////////////////////////////////////////////////

clearos_load_language('base');
clearos_load_language('configuration_backup');

///////////////////////////////////////////////////////////////////////////////
// D E P E N D E N C I E S
///////////////////////////////////////////////////////////////////////////////

// Classes
//--------

use \clearos\apps\base\Daemon as Daemon;
use \clearos\apps\base\Script as Script;
use \clearos\apps\configuration_backup\Configuration_Backup as Configuration_Backup;

clearos_load_library('base/Daemon');
clearos_load_library('base/Script');
clearos_load_library('configuration_backup/Configuration_Backup');

// Exceptions
//-----------

use \Exception as Exception;

///////////////////////////////////////////////////////////////////////////////
// M A I N
///////////////////////////////////////////////////////////////////////////////

//--------------------------------------------------------------------
// Command line options
//--------------------------------------------------------------------

$short_options  = '';

// Common
$short_options .= 'f:'; // Filename
$short_options .= 'L'; // Live file-system restore
$short_options .= 'o::'; // Output
$short_options .= 'h';   // Help

$helpopts  = '
  Usage Arguments
  ---------------

  Modes:
    -f=filename
    -L (live file-system restore)

  Options:
    -o=output (json [default] or stdout)
    -h: help

';

// Handle command line options
//----------------------------

$options = getopt($short_options);

if (isset($options['h'])) {
    echo "usage: " . $argv[0] . " -f=/path/to/backup.tgz|-L [options]\n";
    echo "usage: " . $argv[0] . " -L [options]\n";
    echo $helpopts;
    exit(0);
}

if (!isset($options['f']) && !isset($options['L'])) {
    echo "A backup archive (-f=filename) or a live file-system (-L) mode must be specified.\n";
    exit(1);
}

if (isset($options['f']) && isset($options['L'])) {
    echo "Only one mode can be specified, either -f=filename or -L.\n";
    exit(1);
}

$script = new Script();
$configuration_backup = new Configuration_Backup();
$filename = NULL;

if (isset($options['f']))
    $filename = $options['f'];

$output = isset($options['o']) ? $options['o'] : 'json';

try {
    if ($script->lock() !== TRUE) {
        $configuration_backup->update_status(0, 0, lang('configuration_backup_restore_already_in_progress'), $output);
        exit(0);
    }

    $configuration_backup->run_restore($filename, $output);
    $script->unlock();
} catch (Exception $e) {
    $configuration_backup->update_status(-1, 0, clearos_exception_message($e), $output);
    $script->unlock();

    // Make sure clearsync is running again if something goes horribly wrong
    $daemon = new Daemon('clearsync');
    if ($daemon->is_installed()) {
        $daemon->set_running_state(TRUE);
        $daemon->set_boot_state(TRUE);
    }
}
