#!/usr/bin/perl
#
# pseudodirectory script from phroggy.com 5.07
#
# (C) 2004 Andy Lyttle, all rights reserved.
# This code may be distributed and modified under the terms of
# the GNU General Public License.
# http://phroggy.com/opensource/

BEGIN { push(@INC,'/Users/phroggy/Sites/phroggy.com-opensource/weblib'); }
use strict;
use warnings;
use File::Basename;
use Paths;
use Themes;
use Page;
use Navigation;

# This script must be placed at the root level of the web site.  It will pretend
# to be a directory; /foo/ will load foo.page; /foo/bar.html will load foo-bar.page,
# etc.  
#
# You must add this to .htaccess (replacing foo with the filename):
# <Files foo>
#   SetHandler cgi-script
# </Files>

my $DIR=basename($0);

if(!exists $ENV{'PATH_INFO'} || $ENV{'PATH_INFO'} eq '') { # Add trailing slash
  print "Status: 301 Moved Permanently\n";
  print "Location: http://$ENV{'HTTP_HOST'}".GetURI($DIR);
  print '?'.$ENV{QUERY_STRING} if($ENV{QUERY_STRING});
  print "\n\n";
  exit;
}

# Figure out what page the user wants
my $filename='filenotfound';
if($ENV{'PATH_INFO'} eq '/') {
  $filename=$DIR;
} elsif($ENV{'PATH_INFO'}=~m!^/(.+)\.html$!) {
  $filename=$1;
  if($filename=~/[^a-z0-9]/) { # probably should make this more permissive
    $filename='badrequest';
  } else {
    $filename="$DIR-$filename";
  }
}

ChooseTheme();
load Page($filename)->Print;
exit;
