<?php

$newRelease = $argv[1];
$builds = [];

foreach (glob(dirname(__DIR__) . '/*') as $path) {
    if (is_dir($path)) {
        $dirName = pathinfo($path, PATHINFO_BASENAME);
        if (is_numeric($dirName) && $dirName != $newRelease) {
            $builds[$dirName] = $path;
        }
    }
}

// Sort descending so the most recent previous build is first
krsort($builds);

// Keep the first one (most recent previous build), remove the rest
$kept = 0;
foreach ($builds as $dirName => $path) {
    if ($kept < 1) {
        $kept++;
        continue;
    }
    system("rm -rf $path");
}
