#!/usr/bin/php-cgi -dcgi.force_redirect=0
<?php
# encoding: utf-8
# api: cgi
# type: output
# category: api
# title: project.json
# description: freshcode-style project.json w/ changelog
# version: 0.1
# state: beta
# depends: php:sqlite
# config: -
# access: public
#
# Generates a project.json containing general details
# as well as a `releases` changelog + download links.
#


#-- init
include("./fossil_common.php");


#-- changelog
function rc() {
    return db("
     SELECT event.mtime, tag.tagname, MAX(tag.tagid), DATE(event.mtime) AS d,
            event.comment AS comment
     FROM event
      LEFT JOIN tagxref ON event.objid=tagxref.rid
      LEFT JOIN tag ON tagxref.tagid=tag.tagid
     WHERE type='ci'
     GROUP BY objid
     ORDER BY event.mtime DESC
     LIMIT 1750
    ") + [99999=>["tagname"=>"end-0.0", "d"=>"0-0-0", "comment"=>"-"]];
}
# guess stability
function state($ver, $changes) {
    if (preg_match("/stable|beta|alpha/", $changes, $uu)) {
        return $uu[0];
    }
    elseif (preg_match("/\.0/", $ver)) {
        return "stable";
    }
    else {
        return "development";
    }
}
# judge fix/changeyness
function scope($ver, $changes) {
    $fix = preg_match_all("/fix|correct|merge/i", $changes, $uu);
    $add = preg_match_all("/add|change|intro/i", $changes, $uu);
    $s =  (($fix + $add) > 20) ? "major " : "minor ";
    $s .= ($fix > $add) ? "bugfix" : "changes";
    return $s;
}
# stack release notes by tag/version
function releases() {
    $ver = "trunk";
    $date = time();
    $changes = "";
    $releases = [];
    foreach(rc() as $r) {
        if (preg_match("/\d+\.\d+/", $r["tagname"]) and $r["tagname"] != $ver) {
#            if ($ver != "trunk")
            $releases[] = [
                "version" => preg_replace("/^.+?(?=\d+\.\d+)/", "", $ver),
                "state" => state($ver, $changes),
                "scope" => scope($ver, $changes),
                "changes" => substr($changes, 0, 1024),
                "download" => "$_SERVER[FOSSIL_SELF]zip?name=$ver",
                "published" => $date,
            ];
            $changes = "";
            $ver = $r["tagname"];
            $date = $r["d"];
        }
        $changes .= $r["comment"] . "\n";
    }
    return $releases;
}

#-- output
header("json/vnd.freshcode.club; charset=UTF-8");
print(json_encode([
    "\$feed-origin" => "$_SERVER[FOSSIL_SELF]",
    "\$feed-license" => "CC-BY-SA 4.0",
    "name" => preg_replace("/\.\w+$/", "", basename($_SERVER["FOSSIL_REPOSITORY"])),
    "title" => get_config("project-name", ""),
    "summary" => substr(get_config("project-description", NULL), 0, 50),
    "description" => get_config("project-description", ""),
    "homepage" => get_config("project-homepage", "$_SERVER[FOSSIL_SELF]"),
    "license" => get_config("project-license", ""),
    "tags" => get_config("project-tags", ""),
    "image" => "$_SERVER[FOSSIL_SELF]logo",
    "submitter" => "",
    "urls" => [],
    "releases" => releases(),
], JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES));

