<?php

if (!(file_exists(getcwd()."/plugins") && file_exists(getcwd()."/spark.phar"))) {
    fwrite(STDERR, "Not running from installer directory! Already installed?\n");
    exit(1);
}

function askConfirm(string $prompt, bool $default) {

    $pstr = sprintf("%s [%s]? ", $prompt, $default?"Y/n":"y/N");
    $read = readline($pstr);
    switch (strtolower($read)) {
        case 'yes':
        case 'y':
            return true;
        case 'no':
        case 'n':
            return false;
        default:
            return $default;
    }

}

function askString(string $prompt, ?string $default=null) {

    $pstr = sprintf("%s [%s]? ", $prompt, $default);
    $read = readline($pstr);
    if (empty($read)) return $default;
    return $read;

}

$logo = require_once __DIR__."/logo.php";
echo $logo;

//echo " ___                _   \n";
//echo "/ __|_ __  __ _ _ _| |__\n";
//echo "\\__ \\ '_ \\/ _` | '_| / /\n";
//echo "|___/ .__/\\__,_|_| |_\\_\\\n";
//echo "    |_|                 \n";

printf("\n%s\n \u{26a1} \e[1mSpark\e[0m Installer\n%s\n\n", str_repeat("\u{2500}",80), str_repeat("\u{2500}", 80));

$destination = askString("Installation directory", getenv("HOME")."/opt/spark");
$binaries = askString("Path for executables", getenv("HOME")."/bin");
if (!is_dir($destination)) {
    $doMakeDir = askConfirm("Create directory", true);
} else {
    $doMakeDir = false;
}
$doAliases = askConfirm("Install default aliases", true);
$doPlugins = askConfirm("Install plugins globally", true);

if (!askConfirm("Continue with the installation", false)) {
    exit(0);
}

if ($doMakeDir) {
    printf("Creating directories...\n");
    mkdir($destination, 0777, true);
    mkdir($destination."/plugins", 0777, true);
}

printf("Installing Spark...\n");
passthru("cp -R spark.phar ".escapeshellarg($destination."/spark.phar"));
@symlink($destination."/spark.phar", $binaries."/spark");

printf("Installing plugins...\n");
passthru("cp -R plugins/* ".escapeshellarg($destination."/plugins/"));

if ($doPlugins) {
    $file = sprintf("export SPARK_PLUGINS=\"%s/plugins\"\n", $destination);
    file_put_contents(getenv("HOME")."/.bashrc_spark", $file);
    printf("Updated \e[3m.bashrc_spark\e[0m.\n");
    
    $file = file_get_contents(getenv("HOME")."/.bashrc");
    if (!str_contains($file, ".bashrc_spark")) {
        $file .= "\nsource ~/.bashrc_spark\n";
        file_put_contents(getenv("HOME")."/.bashrc.new", $file);
        rename(getenv("HOME")."/.bashrc", getenv("HOME")."/.bashrc.bak");
        rename(getenv("HOME")."/.bashrc.new", getenv("HOME")."/.bashrc");
        printf("Updated \e[3m.bashrc\e[0m.\n");
    }
}
if ($doAliases) {
    $file = file_get_contents(getenv("HOME")."/.bash_aliases") . "\n";
    $file .= "alias sparksh=\"spark repl\"\n";
    $file .= "alias sparker=\"spark run\"\n";
    $file .= "alias sparkplug=\"spark plugins\"\n";
    $file .= "alias sparkpipe=\"spark pipe\"\n";
    file_put_contents(getenv("HOME")."/.bash_aliases.new", $file);
    rename(getenv("HOME")."/.bash_aliases", getenv("HOME")."/.bash_aliases.bak");
    rename(getenv("HOME")."/.bash_aliases.new", getenv("HOME")."/.bash_aliases");
    printf("Updated \e[3m.bash_aliases\e[0m.\n");
}

printf("Installation complete!\nPlease restart your shell, or \e[3msource ~/.bash_aliases\e[0m and try typing \e[3mspark\e[0m.\n");
