#!/usr/bin/php-cgi -dcgi.force_redirect=0
<?php
# encoding: utf-8
# api: cgi
# type: output
# category: access
# title: Cat file
# description: Look up filename and redirect to /raw/UUID
# version: 0.1
# state: beta
# depends: php:sqlite
# config: -
# access: public
#
# Does an indirect lookup by filename,
# then redirects to the /raw/UUID download.
#
# e.g.
#    http://fossil/ext/raw/README.md
#
# Will always lookup the trunk/tip uuid.
#


#-- init
include("./fossil_common.php");
$fn = ltrim($_SERVER["PATH_INFO"], "/");
#print_r($_SERVER);


#-- find uuid
if (empty($fn)) {
    die("Access as /ext/cat/fn.txt");
}
$r = db(
    "SELECT uuid
     FROM blob LEFT JOIN mlink ON blob.rid=mlink.fid
               LEFT JOIN filename ON mlink.fnid=filename.fnid
     WHERE name = ?
     ORDER BY rid DESC LIMIT 1",
    [$fn]
);
if ($r and $uuid = $r[0]["uuid"]) {
    $fn = basename($fn);
    header("Location: $_SERVER[FOSSIL_URI]/raw/$uuid?at=$fn");
}
else {
    header("Status: 404 Not found");
}

    