#!/usr/bin/perl
#
# Makes repo.cgi/ prefix redundant for fossil cgi server.
# Just use a RewriteRule onto this wrapper instead.
#

#-- Config
$ROOT = "/var/www/usr123/";
$CGI_DIR = "$ROOT/html/fossil";
$REPOS = "$ROOT/files/fossil.d";


#-- Check env
$ENV{'REQUEST_URI'} =~ /^\/([\w-]+)(\/[^?]*)?($|\?.*$)/;
$FN = "$1";  # fossil name
$PI = "$2";  # path_info
$QS = "$3";  # query string


#-- Repository name from RewriteRule
if ( -e "$REPOS/$FN.fossil" )
{

   #-- Override CGI environment 
   $ENV{'SCRIPT_NAME'} = "";
   #$ENV{'REQUEST_URI'} = "/$FN$PI$QS";   # usually retained
   $ENV{'PATH_INFO'} = "/$FN$PI";

   #-- Run fossil repo.cgi script
   exec "$CGI_DIR/repo.cgi";  # contains directory: declaration
   exit;

}
else {
   print "Status: 200\n";
   print "Content-Type: text/plain\n";
   print "\n";
   print "Fossil repository '$FN' does not exist.";
}
