<?php

$secret = "QE3MUmmIKuRrRAOIUBT4g0I56Tc5hsxEApJDL9WhWehcePMYQ0YXuA7gEG9Hi2OZ";

if (!isset($_GET['s']) || $_GET['s'] !== $secret) {
    http_response_code(403);
    echo "Forbidden";
    exit;
}

echo "<pre>";

echo "User: " . shell_exec("whoami") . "\n";

// Siirry repositorioon
$repoPath = "/home/cseerp/repositories/cseerpLive";
$deployPath = "/home/cseerp/public_html/live";

// Ved채 uusimmat p채ivitykset GitHubista
echo shell_exec("cd $repoPath && git fetch origin && git reset --hard origin/beta 2>&1");

// Rsync kaikista tiedostoista, mutta j채t채 configuration.php pois
echo shell_exec("rsync -av --delete --exclude 'configuration.php' $repoPath/ $deployPath/ 2>&1");


// Korjaa oikeudet ja omistajuudet
echo "Fixing permissions...\n";

// Varmista että pääkansio saa 755
echo shell_exec("chmod 755 $deployPath 2>&1");

// Korjaa alihakemistot ja tiedostot
echo shell_exec("find $deployPath -type d -exec chmod 755 {} \\; 2>&1");
echo shell_exec("find $deployPath -type f -exec chmod 644 {} \\; 2>&1");

// Korjaa omistajuus (vaihda käyttäjä jos eri)
echo shell_exec("chown -R cseerp:cseerp $deployPath 2>&1");

echo "Deployment complete.\n";

echo "</pre>";