<?php

ini_set('include_path', '.');

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



$config_files = array( '/' => array( array('.htaccess.in', 'admin/.htaccess'), array('configure.php.in', 'includes/configure.php'), array('configure_sws_.php.in', 'admin/includes/configure.php')) );
$schema_files = array( 'schema.sql' => 'main' );
$reconf_schema_files = array( 'reconfigure.sql' => 'main' );

$psa_params = array (  );
$db_ids = array ( 'main' );
$web_ids = array ( 'tmp', 'images', 'admin/backups', 'admin', '/' );
$settings_params = array ( 'admin_login', 'admin_passwd' );
$crypt_settings_params = array (  );

$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);
$crypt_settings_modify_hash = get_crypt_settings_modify_hash($crypt_settings_params);

function configure($config_files, $schema_files, $db_ids, $psa_modify_hash, $db_modify_hash, $settings_modify_hash, $crypt_settings_modify_hash)
{
    // -- Creating inital DB --

    foreach($db_ids as $db_id){
        if(get_db_type($db_id) != "mysql")
        {
            print "FIXME: database type " . get_db_type($db_id) . " is not supported.\n";
            exit(1);
        }
        foreach($schema_files as $schema_filename => $schema_db_id){
            if($schema_db_id == $db_id){
                $sql = modify_content($schema_filename,
                                      array_merge($psa_modify_hash,
                                            $db_modify_hash,
                                            $settings_modify_hash,
					    $crypt_settings_modify_hash));
                mysql_db_connect(get_db_address($db_id),
                                 get_db_login($db_id),
                                 get_db_password($db_id),
                                 get_db_name($db_id));

                populate_mysql_db($sql);
            }
        }
    }

    // -- Writing config file --

    foreach($config_files as $web_id => $arr2){
        foreach($arr2 as $arr){
            $template_file = $arr[0];
            $dest_path = get_web_dir($web_id).'/'.$arr[1];
            modify_file($template_file,
                    $dest_path,
                    array_merge($psa_modify_hash,
                          $db_modify_hash,
                          $settings_modify_hash,
			  $crypt_settings_modify_hash));
	}
    }
    $admin_htpasswd_file = fetch_env_var("WEB___DIR") . '/admin/.htpasswd';
    $ht_bins = array('htpasswd2', 'htpasswd', '/usr/sbin/htpasswd2', '/usr/sbin/htpasswd'); 
    $htpasswd_bin = ''; 
 
    foreach ($ht_bins as $ht_bin) { 
        $cmd; 
        $cmd = "".$ht_bin."  -cb  ".$admin_htpasswd_file." "; 
        $cmd .= fetch_env_var("SETTINGS_admin_login"); 
        $cmd .= ' "' . fetch_env_var("SETTINGS_admin_passwd") .'" '; 

	print_stderr("cmd = ".$cmd."\n");
        system($cmd, $ret);
        if ($ret != 0) { 
            print_stderr("Unable to create file .htpasswd\n"); 
            $is_error = 1; 
        } 
        else{
            $htpasswd_bin = $ht_bin; 
        }
    } 

    if ($htpasswd_bin == '') { 
        print_stderr("Unable to find htpasswd binary"); 
        exit(1); 
    } 
}

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);
    exit(0);
}

if($command == "remove")
{
    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);
    exit(0);
}

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

?>