<?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');

function pass_encrypt($password, $saltstring) {
    $salt = substr($saltstring, 0, 2);
    $enc_pw = crypt($password, $salt);
    return $enc_pw;
}
					
function admin_passwd_crypt($pass) {
    return pass_encrypt($pass,$pass);
}


$config_files = array( '/' => array( array('config.inc.php.in', 'config.inc.php')) );
$schema_files = array( 'schema.sql' => 'main' );
$reconf_schema_files = array( 'reconfigure.sql' => 'main' );
$remove_schema_files = array( 'remove.sql' => 'main' );

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

$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);

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

$command = $argv[1];

if($command == "upgrade")
{
    print "FIXME: $command is not implemented.\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);
    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);
    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);
    exit(0);
}

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

?>