<?php
function runCommand($command) {
    echo "Running: $command\n";
    $output = shell_exec($command);
    echo $output;
}

// Step 1: Check if Composer is Installed
$composerCheck = shell_exec("composer --version 2>&1");

if (strpos($composerCheck, 'Composer version') === false) {
    echo "Composer is not installed. Installing now...\n";

    // Step 2: Download Composer Installer
    runCommand("php -r \"copy('https://getcomposer.org/installer', 'composer-setup.php');\"");

    // Step 3: Verify and Install Composer
    runCommand("php composer-setup.php");
    runCommand("php -r \"unlink('composer-setup.php');\"");

    // Step 4: Move Composer to Global Path (Windows)
    runCommand("move composer.phar C:\\composer.phar");
    runCommand("setx PATH \"%PATH%;C:\\\""); // Add to system PATH
    echo "Composer installed successfully!\n";
} else {
    echo "Composer is already installed.\n";
}

// Step 5: Install spomky-labs/otphp if not installed
$vendorPath = __DIR__ . "/vendor";

if (!file_exists("$vendorPath/autoload.php")) {
    echo "OTPHP not found. Installing now...\n";
    runCommand("composer require spomky-labs/otphp");
} else {
    echo "OTPHP is already installed.\n";
}

echo "Setup complete!\n";
?>
