/**
 * This file is separated out so that it can be run separately for DEB package installation.
 * When installing a DEB package, Postgresql may not be installed yet and thus the database
 * cannot be created.  So this script is run after all DEB packages have been installed.
 */
require_once __DIR__ . '/AirtimeIni.php';
require_once __DIR__ . '/AirtimeInstall.php';
require_once __DIR__ . '/airtime-constants.php';
require_once AirtimeInstall::GetAirtimeSrcDir() . '/application/configs/conf.php';
$CC_CONFIG = Config::getConfig();
require_once 'propel/runtime/lib/Propel.php';
Propel::init(AirtimeInstall::GetAirtimeSrcDir() . "/application/configs/airtime-conf-production.php");
echo PHP_EOL . "* Database Installation" . PHP_EOL;
AirtimeInstall::CreateDatabaseUser();
$databaseExisted = AirtimeInstall::CreateDatabase();
AirtimeInstall::DbConnect(true);
AirtimeInstall::InstallPostgresScriptingLanguage();
//Load Database parameters
$dbuser = $CC_CONFIG['dsn']['username'];
$dbpasswd = $CC_CONFIG['dsn']['password'];
$dbname = $CC_CONFIG['dsn']['database'];
$dbhost = $CC_CONFIG['dsn']['hostspec'];
if (isset($argv[1]) && $argv[1] == 'y') {
    AirtimeInstall::CreateDatabaseTables($dbuser, $dbpasswd, $dbname, $dbhost);
} else {
    if ($databaseExisted) {
        //Database already exists. Ask the user how they want to
        //proceed. Warn them that creating the database tables again
        //will cause them to lose their old ones.
        $userAnswer = "x";