<?php

$isParsing = getenv('PARSING_ENV') === 'true';
$isProd = getenv('PROD_ENV') === 'true';
$isLocal = getenv('LOCAL_ENV') === 'true';

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

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

// List of scripts to run
$scripts = [
    // 'fizzslots.php',    //1 CLOSED
    // 'bcgame.php',       //2
    //slottarget 'm88.php',          //3
    // 'surfcasino.php',   //4 CLOSED
    // 'dlx.php',          //5 CLOSED
    // 'ltc.php',          //6
    // 'fastpay.php',      //7
    // 'cobber.php',       //8
    // 'casino4u.php',     //9
    /// 'wildblaster.php',  //10
    //slottarget 'casinoin.php',     //11
    //slottarget 'betmaster.php',    //12
    //slottarget 'bongogg.php',      //13
    // 'n1bet.php',        //14
    // 'joo.php',          //15
    //slottarget 'dollycasino.php',  //16
    // 'freshcasino.php',  //17
    // 'izzicasino.php',   //18
    // 'lalabet.php',      //19
    // 'rant.php',         //20 stopped for gb
    // 'winz.php',         //21
    // 'rajabets.php',     //22
    // 'luckycasino.php',  //23
    // 'dripcasino.php',   //24
    // 'rocketpot.php',    //25
    // 'voltslot.php',     //26
    //slottarget 'skycrown.php',     //27
    // 'loftcasino.php',   //28
    // 'bons.php',         //29
    //slottarget 'mrpacho.php',      //30
    // 'mrvegas.php',      //31
    // 'pvpbet.php',       //32 CLOSED
    // 'videoslots.php',   //33
    // 'NOCASINO',         //34 NOCASINO
    // 'solcasino.php',    //35
    // 'monrocasino.php',  //36
    // 'bk8casino.php',    //37
    // '1gocasino.php',    //38
    //slottarget 'zotabet.php',      //39
    //slottarget 'bohocasino.php',    //40
    // 'winsroyal.php'     //41 ABSENT
    //slottarget 'betunlim.php'      //42
];

// Loop through the scripts and execute them one after another
foreach ($scripts as $script) {
    if ($isProd){
        $command = 'php ' . $directory . '/gamble-free.com.parsing/casinos/' . $script;   
    } else {
        $command = 'php ' . $directory . '/gamble-free.com.parsing/casinos/' . $script . ' --proxy=172.17.0.2:3128';
    }

    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";
?>
