<?php

$request_uri = $_SERVER['REQUEST_URI'];
$repository = "invalid";
$branch = "invalid";

if (preg_match('/^\/IcingaForWindows\/snapshot\/([a-z0-9-]*)\/([a-zA-Z0-9-_\/]*)\/ifw\.repo\.json/', $request_uri, $matches)) {
  $repository = $matches[1];
  $branch = $matches[2];
}

$repofile = new stdClass();
$repofile->Info = new stdClass();
$repofile->Info->LocalSource = "";
$repofile->Info->RemoteSource = "RemoteSource";
$repofile->Info->Created = date("Y/m/d H:i:s");
$repofile->Info->Updated = date("Y/m/d H:i:s");
$repofile->Info->RepoHash = "";

preg_match('/icinga-powershell-([a-zA-Z0-9-_]*)/', $repository, $package_name_matches);
$package_name = $package_name_matches[1];

$repofile->Packages = new stdClass();
$repofile->Packages->$package_name = array();
$repofile->Packages->$package_name[0] = new stdClass();
$repofile->Packages->$package_name[0]->Hash = "";
$repofile->Packages->$package_name[0]->Location = "https://github.com/Icinga/$repository/archive/refs/heads/$branch.zip";
$repofile->Packages->$package_name[0]->RelativePath = false;
$repofile->Packages->$package_name[0]->Version = $branch;
$repofile->Packages->$package_name[0]->Snapshot = true;
$repofile->Packages->$package_name[0]->Architecture = "Multi";

$json = json_encode($repofile, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT);

echo $json;
?>
