<?php
ini_set('include_path', '.');

require_once('env-parser.php');
require_once('file-util.php');
require_once('db-util.php');
require_once('app-util.php');
require_once('upgrade-app.php');

function better_srand($seed = '') {
    static $wascalled = FALSE;
    if (!$wascalled) {
        if ($seed === '') {
            list($usec, $sec) = explode(" ", microtime());
            if ($usec > 0.1)
                $seed = (double) $usec * $sec;
            else // once in a while use the combined LCG entropy
                $seed = (double) 1000000 * substr(uniqid("", true), 13);
        }
        if (function_exists('mt_srand')) {
            mt_srand($seed); // mersenne twister
        } else {
            srand($seed);
        }
        $wascalled = TRUE;
    }
}

function rand_ascii($length = 1) {
    better_srand();
    $s = "";
    for ($i = 1; $i <= $length; $i++) {
    // return only typeable 7 bit ascii, avoid quotes
        if (function_exists('mt_rand'))
    	    // the usually bad glibc srand()
            $s .= chr(mt_rand(40, 126));
        else
            $s .= chr(rand(40, 126));
    }
    return $s;
}

function admin_password_crypt($pass){
    if (function_exists('crypt')) {
        $salt_length = max(CRYPT_SALT_LENGTH,
			    2 * CRYPT_STD_DES,
                            9 * CRYPT_EXT_DES,
			    12 * CRYPT_MD5,
			    16 * CRYPT_BLOWFISH);
	// generate an encrypted password
	$crypt_pass = crypt($pass, rand_ascii($salt_length));
	return $crypt_pass;
    } else {
	return $pass;
    }
}


$config_files = array( '/' => array( array('config.ini.in', 'config/config.ini')), '/cgi-bin' => array() );
$schema_files = array( 'schema.sql' => 'main' );
$reconf_schema_files = array(  );
$remove_schema_files = array(  );

$psa_params = array (  );
$db_ids = array ( 'main' );
$web_ids = array ( '/' );
$settings_params = array ( 'admin_name', 'locale' );
$settings_enum_params = array ( 'locale' => array( 'en-US' => 'en', 'de-DE' => 'de', 'fr-FR' => 'fr' ) );
$crypt_settings_params = array ( 'admin_password' );

$psa_modify_hash = get_psa_modify_hash($psa_params);
$db_modify_hash = get_db_modify_hash($db_ids);
$web_modify_hash = get_web_modify_hash($web_ids);
$settings_modify_hash = get_settings_modify_hash($settings_params);
$settings_enum_modify_hash = get_settings_enum_modify_hash($settings_enum_params);
$crypt_settings_modify_hash = get_crypt_settings_modify_hash($crypt_settings_params);

$additional_modify_hash = get_additional_modify_hash();

if(count($argv) < 2)
{
    print "Usage: configure (install | upgrade <version> | configure | remove)\n";
    exit(1);
}

$command = $argv[1];

if($command == "upgrade")
{
    if($argv[2] && $argv[3]){
	upgrade_app($argv[2], $argv[3], $config_files, $db_ids, $psa_modify_hash, $db_modify_hash, $settings_modify_hash, $crypt_settings_modify_hash, $settings_enum_modify_hash, $additional_modify_hash);
	exit(0);
    }
    else{
	print "Error: upgrade version or release not specified.\n";
	exit(1);
    }
}

if($command == "install")
{
    configure($config_files, $schema_files, $db_ids, $psa_modify_hash, $db_modify_hash, $settings_modify_hash, $crypt_settings_modify_hash, $settings_enum_modify_hash, $additional_modify_hash);
    exit(0);
}

if($command == "remove")
{
    remove_app($remove_schema_files, $db_ids, $psa_modify_hash, $db_modify_hash, $settings_modify_hash, $crypt_settings_modify_hash, $settings_enum_modify_hash, $additional_modify_hash);
    exit(0);
}

if($command == "configure")
{
    configure($config_files, $reconf_schema_files, $db_ids, $psa_modify_hash, $db_modify_hash, $settings_modify_hash, $crypt_settings_modify_hash, $settings_enum_modify_hash, $additional_modify_hash);
    exit(0);
}

print "Error: unknown command $command.\n";
exit(1);

?>