<?php

include_once 'installer/OsUtils.class.php';
include_once 'installer/Log.php';
// start the log
startLog("pre_install_log_" . date("d.m.Y_H.i.s"));
logMessage(L_USER, 'start');
$currWD = getcwd();
logMessage(L_USER, "dir: {$currWD}");
$kalturaUserName = '******';
$kalturaUserPassword = '******';
for ($i = 1; $i < $argc; $i++) {
    if ($argv[$i] != '-s' && $argv[$i] != '-c') {
        $kalturaUserPassword = $argv[$i];
    }
}
logMessage(L_USER, 'add user');
if (OsUtils::execute("useradd {$kalturaUserName} -g root")) {
    logMessage(L_USER, 'create password');
    if (!OsUtils::execute("passwd {$kalturaUserName} <<EOF\n" . $kalturaUserPassword . "\n" . $kalturaUserPassword . "\nEOF 2>&1")) {
        logMessage(L_USER, 'Failed creating user password');
        return "\nFailed creating user password\n";
    }
}
//logMessage(L_USER, 'chmod');
//if (!OsUtils::execute("chmod -R 740 /home/$kalturaUserName ")) {
//echo "Failed chmod ";
//return "\nFailed chmod \n";
//}
//logMessage(L_USER, 'chmod');
//if (!OsUtils::execute("chmod 664 /opt/kaltura/app/batch/KGenericBatchMgr.class")) {
include_once 'installer/AppConfig.class.php';
include_once 'installer/Installer.class.php';
include_once 'installer/InputValidator.class.php';
include_once 'installer/phpmailer/class.phpmailer.php';
include_once 'lib/utils.php';
// constants
define("K_TM_TYPE", "TM");
define("K_CE_TYPE", "CE");
define("FILE_INSTALL_SEQ_ID", "install_seq");
// this file is used to store a sequence of installations
// installation might take a few minutes
ini_set('max_execution_time', 0);
ini_set('memory_limit', -1);
ini_set('max_input_time ', 0);
// start the log
startLog("update_config_files_log_" . date("d.m.Y_H.i.s"));
logMessage(L_INFO, "Update started");
// variables
$silentRun = false;
if ($argc > 1 && $argv[1] == '-s') {
    $silentRun = true;
}
$cleanupIfFail = true;
if ($argc > 1 && $argv[1] == '-c') {
    $cleanupIfFail = false;
    $silentRun = true;
}
$app = new AppConfig();
$installer = new Installer();
$user = new UserInput();
$db_params = array();