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

/**
 * Samba initialization script.
 *
 * @category   apps
 * @package    samba
 * @subpackage scripts
 * @author     ClearFoundation <developer@clearfoundation.com>
 * @copyright  2012 ClearFoundation
 * @license    http://www.gnu.org/copyleft/lgpl.html GNU Lesser General Public License version 3 or later
 * @link       http://www.clearfoundation.com/docs/developer/apps/samba/
 */

///////////////////////////////////////////////////////////////////////////////
// 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';

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

// Factories
//----------

use \clearos\apps\mode\Mode_Factory as Mode;

clearos_load_library('mode/Mode_Factory');

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

use \clearos\apps\mode\Mode_Engine as Mode_Engine;
use \clearos\apps\samba\OpenLDAP_Driver as OpenLDAP_Driver;
use \clearos\apps\samba_common\Samba as Samba;

clearos_load_library('mode/Mode_Engine');
clearos_load_library('samba/OpenLDAP_Driver');
clearos_load_library('samba_common/Samba');

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

use \Exception as Exception;

///////////////////////////////////////////////////////////////////////////////
// F U N C T I O N S
///////////////////////////////////////////////////////////////////////////////

function ttyecho($on)
{
    global $ttyecho;

    if ($on) {
        if (isset($ttyecho))
            exec('stty ' .$ttyecho);
    } else {
        $ttyecho = exec('stty -g');
        exec('stty -echo');
    }
}

///////////////////////////////////////////////////////////////////////////////
// O P T I O N S
///////////////////////////////////////////////////////////////////////////////

$short_options = '';
$short_options .= 'p:'; // Password
$short_options .= 'd:'; // Domain
$short_options .= 'n:'; // Netbios name
$short_options .= 'h';  // Help
$short_options .= 'f';  // Force

$help_options  = '';
$help_options .= "  -d: Windows domain (e.g. TORONTO) - not used on slave systems\n";
$help_options .= "  -n: Server name\n";
$help_options .= "  -p: Password\n";
$help_options .= "  -f: Force initialization\n";
$help_options .= "\n";
$help_options .= "  -h: Help\n";

$options = getopt($short_options);

$help = isset($options['h']) ? TRUE : FALSE;
$force = isset($options['f']) ? TRUE : FALSE;
$domain = isset($options['d']) ? $options['d'] : '';
$netbios = isset($options['n']) ? $options['n'] : '';
$password = isset($options['p']) ? $options['p'] : NULL;


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

$mode = Mode::create();
$samba = new Samba();
$openldap = new OpenLDAP_Driver();

// Basic usage stuff
//------------------

if ($help) {
    echo "usage: " . $argv[0] . " [options]\n";
    echo $help_options;
    exit(0);
}

try {
    if (!$force && $samba->is_initialized()) {
        echo "Samba is already initialized\n";
        exit(0);
    }
} catch (Exception $e) {
    echo "error: " . $e->GetMessage() . "\n";
}

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

$system_mode = $mode->get_mode();

if ($mode !== Mode_Engine::MODE_SLAVE) {
    while ($openldap->validate_workgroup($domain)) {
        echo 'Windows domain (e.g. TORONTO): ';
        $domain = trim(fgets(STDIN));
    }
}

while ($openldap->validate_netbios_name($netbios)) {
    echo 'Server name (e.g. SERVER1): ';
    $netbios = trim(fgets(STDIN));
}

while ($openldap->validate_password($password)) {
    ttyecho(FALSE); // Disable echo to terminal
    echo 'Password: ';
    $password = trim(fgets(STDIN));
    ttyecho(FALSE); // Re-enable echo to terminal
}

// Run it
//-------

echo "The following settings will be used to set up the Samba Directory\n\n";
echo "Mode:            $system_mode\n";
if ($mode !== Mode_Engine::MODE_SLAVE)
    echo "Domain:          $domain\n";
echo "Server name:     $netbios\n";
echo "Password:        " . str_repeat("*", strlen($password)) . "\n";
echo "\n";

// Dirty - try it twice
try {
    if ($mode === Mode_Engine::MODE_SLAVE)
        $openldap->initialize_samba_as_slave($netbios, $password, $force);
    else
        $openldap->initialize_samba_as_master_or_standalone($netbios, $domain, $password, $force);
} catch (Exception $e) {
    // Try again
}

if (! $samba->is_initialized()) {
    sleep(15);
    if ($mode === Mode_Engine::MODE_SLAVE)
        $openldap->initialize_samba_as_slave($netbios, $password, $force);
    else
        $openldap->initialize_samba_as_master_or_standalone($netbios, $domain, $password, $force);
}
