#!/usr/bin/php
<?php
require_once __DIR__. '/../vendor/autoload.php';

use Symfony\Component\Yaml\Yaml;

$parameters = Yaml::parse(file_get_contents('app/config/parameters.yml'));

echo 'Okay, you want to install wallabag, let\'s go!';
echo "\r\n";

function executeQuery($handle, $sql, $params) {
    try
    {
        $query = $handle->prepare($sql);
        $query->execute($params);
        return $query->fetchAll();
    }
    catch (Exception $e)
    {
        return false;
    }
}

$configFile      = 'app/config/config.inc.php';
$dbFile          = 'app/db/poche.sqlite';
$username        = 'wallabag';
$password        = 'wallabag';
$salt            = $parameters['parameters']['secret'];
$defaultLanguage = 'en_EN.UTF8';

if (!copy('app/config/config.inc.default.php', $configFile)) {
    die('Installation aborted, impossible to create ' . $configFile . ' file. Maybe you don\'t have write access to create it.');
}

$content = file_get_contents($configFile);
$content = str_replace("define ('SALT', '');", "define ('SALT', '".$salt."');", $content);
file_put_contents($configFile, $content);

if (!copy('bin/poche.sqlite', $dbFile)) {
    die('Impossible to create ' . $dbFile . ' file.');
}

chmod($dbFile, 0777);

$dbPath = 'sqlite:' . realpath('') . '/' . $dbFile;

$handle = new PDO($dbPath);

$handle->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$saltedPassword = sha1($password . $username . $salt);

$sql    = "INSERT INTO users (username, password, name, email) VALUES (?, ?, ?, '')";
$params = array($username, $saltedPassword, $username);
$query  = executeQuery($handle, $sql, $params);

$idUser = (int)$handle->lastInsertId('users_id_seq');

$sql    = 'INSERT INTO users_config ( user_id, name, value ) VALUES (?, ?, ?)';
$params = array($idUser, 'pager', '10');
$query  = executeQuery($handle, $sql, $params);

$sql    = 'INSERT INTO users_config ( user_id, name, value ) VALUES (?, ?, ?)';
$params = array($idUser, 'language', $defaultLanguage);
$query  = executeQuery($handle, $sql, $params);

echo 'wallabag is now installed';
echo "\r\n";
echo 'Just execute the following commands for using wallabag:';
echo "\r\n";
echo 'cd web';
echo "\r\n";
echo 'php -S localhost:8000';