<?php

$isLocal = getenv('LOCAL_ENV') === 'true';

$directory = $isLocal ? "/Users/serhiipozynich" : "/var/www";

$log = $directory . '/gamble-free.com.parsing/providers/providers.log';
// unlink($log);

// List of scripts to run
$scripts = [
    'json_habanero.php',
    'json_ka_gaming.php',
    'json_wazdan.php',
    'json_nolimit_city.php',
    'json_pg_soft.php',
    'json_yggdrasil.php',
    // 'json_playngo.php',
    'json_3oaks.php',
    'json_smartsoft_gaming.php',
    'json_netent.php',
    'json_amatic.php', //needs fix
    'json_blueprint.php', //needs fix
    'dom_thunderkick.php',
    'dom_push_gaming.php',
    // 'dom_jili_games.php', 
    'dom_pragmatic_play.php',
    'dom_playson.php',
    'dom_relax_gaming.php',
    'dom_gamomat.php',
    'dom_quickspin.php',
    'dom_evoplay.php', 
    'dom_ruby_play.php', //needs fix
    'dom_kalamba_games.php', //needs fix
    'dom_elk_studios.php',
    'dom_red_tiger.php',
    'dom_stakelogic.php',
    'dom_wizard_games.php',
    'dom_hacksaw.php',
    'dom_endorphina.php',
    'dom_playtech.php', //needs fix
    'dom_fachai.php',
    'dom_spinomenal.php',
    'dom_gamzix.php',
    'dom_belatra.php',
    'dom_spinoro.php',
    'dom_backseatgaming.php'
    // 'manual_dom_spribe.php',
    // 'manual_dom_fazi.php',
    // 'manual_dom_swintt.php',
    // 'manual_dom_bgaming.php',
    // 'manual_dom_inspired.php',
    // 'manual_dom_isoftbet.php',
    // 'manual_dom_rtg_slots.php',
];

// Loop through the scripts and execute them one after another
foreach ($scripts as $script) {
    $command = 'php ' . $directory . '/gamble-free.com.parsing/providers/' . $script;

    echo "Running script: $script\n";

    $descriptorspec = [
        0 => ["pipe", "r"], // stdin
        1 => ["pipe", "w"], // stdout
        2 => ["pipe", "w"], // stderr
    ];

    $process = proc_open($command, $descriptorspec, $pipes);

    if (is_resource($process)) {
        // Read and display the output
        while ($output = fgets($pipes[1])) {
            echo $output;
        }

        // Close pipes and the process
        fclose($pipes[0]);
        fclose($pipes[1]);
        fclose($pipes[2]);
        proc_close($process);
    }

    // Add a delay between scripts (e.g., 5 seconds)
    sleep(5);
}

echo "All scripts have been executed.\n";
?>
