#!/usr/bin/perl
#
# /pages 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 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; /pages/foo.html will load foo.page, etc.
#
# You must add this to .htaccess:
# <Files pages>
#   SetHandler cgi-script
# </Files>

if(!exists $ENV{'PATH_INFO'} || $ENV{'PATH_INFO'} eq '' || $ENV{'PATH_INFO'} eq '/') { # Go home
  print "Status: 301 Moved Permanently\n";
  print "Location: http://$ENV{'HTTP_HOST'}".GetURI('home');
  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'}=~m!^/(.+)\.html$!) {
  $filename=$1;
  if($filename=~/[^a-z0-9]/) { # probably should make this more permissive
    $filename='badrequest';
  }
}

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