$zipRepo = "https://gitlab.com/api/v3/projects/" . REPO_ID . "/repository/archive.zip?private_token=" . PRIVATE_TOKEN;
$zipLocal = "{$RepositoryName}.zip";
if (!file_exists(DEPLOY_DIR)) {
    mkdir(DEPLOY_DIR, 0777);
} else {
    chmod(DEPLOY_DIR, 0777);
    recursiveRemoveDirectory(DEPLOY_DIR);
    mkdir(DEPLOY_DIR, 0777);
}
// Download the latest zip
copy($zipRepo, $zipLocal);
$tempDir = uniqid("temp");
$logStr = "";
// Deploy
if (ExtractZip($zipLocal, $tempDir)) {
    if (recursiveMoveDirectory(glob("{$tempDir}/{$RepositoryName}*")[0], DEPLOY_DIR)) {
        $logStr = "Deployed {$RepositoryName} to " . DEPLOY_DIR;
    } else {
        $logStr = "Error: Failed on MOVE";
    }
} else {
    $logStr = "Error: Failed on ExtractZip";
}
writeLog($logStr);
echo $logStr;
// Prevent cPanel SoftException
chmod(DEPLOY_DIR, 0755);
// Clean up
unlink($zipLocal);
chmod($tempDir, 0777);
recursiveRemoveDirectory($tempDir);
}
echo "Deploying...<br>";
$zipRepo = rtrim(GIT_REPO, '/') . '/archive/master.zip';
$repoName = explode('/', rtrim(GIT_REPO, '/'))[count(explode('/', rtrim(GIT_REPO, '/'))) - 1];
$zipLocal = $repoName . ".zip";
if (!file_exists(DEPLOY_DIR)) {
    mkdir(DEPLOY_DIR);
} else {
    chmod(DEPLOY_DIR, 0777);
    recursiveRemoveDirectory(DEPLOY_DIR);
    mkdir(DEPLOY_DIR);
}
// Download the latest zip
copy($zipRepo, $zipLocal);
$tempDir = uniqid("autodeploytemp");
$logStr = "";
// Deploy
if (ExtractZip($zipLocal, $tempDir)) {
    if (rename(rtrim($tempDir, '/') . "/" . $repoName . "-master", DEPLOY_DIR) || recursiveMoveDirectory(rtrim($tempDir, '/') . "/" . $repoName . "-master", DEPLOY_DIR)) {
        $logStr = "Deployed " . GIT_REPO . " to " . DEPLOY_DIR;
    } else {
        $logStr = "Error: Failed on RENAME/MOVE";
    }
} else {
    $logStr = "Error: Failed on ExtractZip";
}
writeLog($logStr);
echo $logStr;
// Clean up
unlink($zipLocal);
recursiveRemoveDirectory($tempDir);