<?php


  if (isset($_POST['submit_now'])) {
    // Ok, we want to start the creation of the database now.


    echo "Trying to initialize database ...";
    include('./adodb-4.65/adodb-errorhandler.inc.php');
    include('./adodb-4.65/adodb.inc.php');
    define(DB_TYPE, 'MySQL');
    define(DB_HOST,$_POST['submit_dbhost'].((isset($_POST['submit_dbhost'])) ? ':'.$_POST['submit_dbport'] : ''));
    define(DB_LOGIN,$_POST['submit_dbuser']);
    define(DB_PWORD,$_POST['submit_dbpwd']);
    define(DB_NAME,$_POST['submit_dbname']);
    $db = ADONewConnection(DB_TYPE);
    $db->SetFetchMode(ADODB_FETCH_ASSOC);
    $db->debug = false;
    $db->Connect(DB_HOST, DB_LOGIN, DB_PWORD, DB_NAME);

    $query=file_get_contents('./docs/database.sql');
    $query=str_replace('{ADMIN_PWD_MD5}',md5($_POST['submit_adminpwd']),$query);
    $_POST['submit_projectname']=str_replace("\'","''",$_POST['submit_projectname']);
    $query=str_replace('{PROJECT_NAME}',$_POST['submit_projectname'],$query);

    $each_query=explode(';',$query);
    foreach ($each_query as $query)  {
      $query=trim($query);
      if (strlen($query)>0) $db->Execute($query);
    }
    echo "... done!</br>";
    echo "Printing config file.</br>";
    $config=file_get_contents('./docs/config.template');

    if ( isset($_SERVER['PHP_SELF']) && isset($_SERVER['HTTP_HOST']) ) {
      $truc_path = explode ('/', $_SERVER['PHP_SELF']);
      if (count($truc_path)>0) {
        unset($truc_path[count($truc_path)-1]);
      }
      $truc_path=implode('/',$truc_path);

      $truc_url = 'http://' . $_SERVER['HTTP_HOST'] . $truc_path . '/';

    } else {
      $truc_url = 'http://localhost/truc/' ;
    }

    $config=str_replace('{TRUC_URL}',$truc_url,$config);
    $config=str_replace('{DB_PWD}',DB_PWORD,$config);
    $config=str_replace('{DB_USER}',DB_LOGIN,$config);
    $config=str_replace('{DB_NAME}',DB_NAME,$config);
    $config=str_replace('{DB_HOST}',DB_HOST,$config);
    $config=htmlspecialchars($config);


    echo '<b>Save</b> the following content, in <b>'.dirname(__FILE__).'/config_inc.php</b>';
    echo '<hr><textarea style="height:80%;width:100%">'.$config.'</textarea>';
    die;

  }

?>
<form method="POST">
  <h1>Install TRUC</h1>
  <table>
    <tr><th>Database host:</th><td><input name="submit_dbhost" type="text" value="localhost"/></td></tr>
    <tr><th>Database port:</th><td><input name="submit_dbport" type="text" value=""/></td></tr>
    <tr><th>Database name:</th><td><input name="submit_dbname" type="text" value="truc"/></td></tr>
    <tr><th>Database username:</th><td><input name="submit_dbuser" type="text" value="truc"/></td></tr>
    <tr><th>Database user password:</th><td><input name="submit_dbpwd" type="password" value=""/></td></tr>
    <tr><th>Truc-Admin password:</th><td><input name="submit_adminpwd" type="password" value=""/></td></tr>
    <tr><th>Truc-Project Name:</th><td><input name="submit_projectname" type="text" value=""/></td></tr>
    <tr><td colspan="2"><input name="submit_now" type="submit" value="Install!" class="submit"/></td>
  </table>
</form>