#!/usr/local/plan9/bin/rc
. ./cgilib.rc
cd ..
forbidden_uri_chars='[^a-zA-Z0-9_+\-\/\.]'
response_format=html
fn get_lib_file {
    if(test -f $sitedir/_werc/lib/$1)
    	echo -n $sitedir/_werc/lib/$1
    if not if(! ~ $#masterSite 0 && test -f $sitesdir/$masterSite/_werc/lib/$1)
    	echo -n $sitesdir/$masterSite/_werc/lib/$1
    if not if(test -f lib/$1)
    	echo -n lib/$1
}
# Title
fn gentitle {
    echo '
'
}
# Don't change var name or trailing ';', dirfilter might be changed from _werc/config!
# TODO: Specify the canonical path input format handled by dirfilter
dirfilter='s/\*$//; s,/+\./+,/,g; /\/[._]/d; /'^$forbidden_uri_chars^'/d; /^\/(robots|sitemap)\.txt$|\/index\.(md|html|txt|tpl)$/d; /_werc\/?$/d; s,^\./,,; s,\.(md|html|txt)$,,; '
# To be used from config files
fn hide_paths {
    for(i in $*)
       dirfilter=$dirfilter^'/^'$i'$/d; '
}
# Sidebar 
fn gensidebar {
    # Ignore stderr, last path element might be a file that doesn't exist (eg., foo for foo.md)
    ls -F $sitedir^/./^$req_paths_list >[2]/dev/null | sed 's,^'$sitedir',,; '^$dirfilter | sort -u | awk -F/ ' 
    function p(x, y, s) {
        for(i=0; i < x-y; i+=1)
            print s 
    }
    # XXX Regexp to hide non-document/non-dir stuff, there must be a better way, if we really want this.
    /^\/([a-zA-Z0-9+_\-]+[*\/]?)+$/ { 
        d = ""
        if(match($0, "/$"))
            d = "/"
        sub("/$", "") # Strip trailing / for dirs so NF is consistent 
        p(NF, lNF, "")
        lNF = NF
        bname = $NF d
        path = $0 d
        gsub("_", " ", bname)
        if(index(ENVIRON["req_path"] "/", path) == 1)
            print "» " bname ""
        else 
            print "› " bname ""
    }
    END { p(lNF, 0, "") }
    '
}
# Handlers
fn set_handler {
    handler=$1
    handler_args=$*(2-)
}
fn md_handler { cat $* | $formatter }
fn tpl_handler { template $1 }
fn html_handler {
    # body states: 0 = no  found, 2 = after , 1 = after , -1 = after 
    awk 'gsub(".*<[Bb][Oo][Dd][Yy][^>]*>", "") > 0 {body=2}
        gsub(" *[Bb][Oo][Dd][Yy][^>]*>.*", "") > 0 {print; body=body-1}
        body==2 {print}
        body==0 {buf=buf "\n" $0}
        END {if(body<=0) {print buf}}' < $1
}
fn txt_handler {
    echo ''
    # XXX Insering a blank line between lines in input is good for fortunes.txt, but maybe not for other .txt files
    # XXX Words are not broken, even if they are way beyond 82 chars long
    cat $1 | sed 's/$/\n/g; s/\</g; s/>/\>/g' | fmt -l 82 -j
    echo ''
}
fn dir_listing_handler {
    d=`{basename -d $1}
    echo $d|sed 's,.*//,,g; s,/$,,; s,/, / ,g; s,.*,&
 ,'
    # Symlinks suck: '/.' forces ls to list the linked dir if $d is a symlink.
    ls -F $d/. | sed $dirfilter' s,.*/([^/]+/?)$,- \1,'
    echo '
'
}
fn select_handler {
    if(test -f $local_path.md) {
        set_handler md_handler $local_path.md
        if(! ~ $#allowComments 0 && ~ $REQUEST_METHOD POST) {
            get_post_args comment_text
            d=`{date -n} # FIXME Obvious race
            d=$local_path.md_werc/comments/$d/
            u=$logged_user
            if(~ $#logged_user 0) {
                get_post_args comment_user_name comment_user_password
                # TODO Should take this path too if the user can login but doesn't
                # belong to required group
                if(! login_user $comment_user_name $comment_user_password) {
                    u=$comment_user_name ':' $comment_user_password
                    d=$d^'_pending'
                }
                if not
                    u = $logged_user
            }
            umask 002
            mkdir -m 775 -p $d
            echo $u > $d/user
            echo $comment_text > $d/body
        }
    }
    if not if(test -f $local_path.tpl)
        set_handler tpl_handler $local_path.tpl
    if not if(test -f $local_path.html)
        set_handler html_handler $local_path.html
    # Explicit .html urls, unfortunately usually the web server will handle this as static files
    if not if(~ $local_path *.html && test -f $local_path)
        perm_redirect `{ echo $req_path|sed 's/.html$//' }
    # Global tpl (eg sitemap.tpl)
    if not if(test -f lib^$req_path^.tpl)
        set_handler tpl_handler lib^$req_path^.tpl
    if not if(test -f $local_path.txt)
        set_handler txt_handler $local_path.txt
    # Apps
    if not if(! ~ $#enabled_apps 0 && select_apps)
        set_handler app_handler
    # Dir listing
    if not if(~ $local_path */index)
        set_handler dir_listing_handler $local_path
    # File not found
    if not {
        set_handler tpl_handler `{get_lib_file 404.tpl}
        dprint 'NOT FOUND: '$SERVER_NAME^$"REQUEST_URI^' - '^$"HTTP_REFERER^' - '^$"HTTP_USER_AGENT
        echo 'Status: 404 Not Found'
    }
}
fn genbody { $handler $handler_args }
# Careful, the proper p9p path might not be set until initrc.local is sourced
path=(. $PLAN9/bin ./bin/ /bin/ /usr/bin) 
site=$SERVER_NAME
headers=lib/headers.tpl
master_template=default_master.tpl
sidebar=sidebar
base_url=http://$site/
current_date_time=`{date}
for(i in siteTitle siteSubTitle pageTitle extraHeaders)
    $i = ''
. ./etc/initrc
if(test -f etc/initrc.local)
    . ./etc/initrc.local
werc_root=`{pwd}
# Parse request URL
# NOTE: $REQUEST_URI is not officially in CGI 1.1, but seems to be de-facto
req_path=`{echo -n $REQUEST_URI | sed 's/\?.*//; s/'^$forbidden_uri_chars^'//g; s/\.\.*/./g; 1q'}
ifs='/' { args=`{echo -n $req_path} }
# Hack: preload post data so we can access it from templates where cgi's stdin is not accesible
if(~ $REQUEST_METHOD POST) {
    get_post_args 
    login_user
}
if(! ~ $args '') {
    if(~ $args($#args) 'index')
        perm_redirect `{echo $req_path | sed 's,/index$,/,'}
        
    pageTitle=`{echo $args | sed -e 's/ / - /g' -e 's/_/ /g'}
}
p=()
cd $sitedir
for(i in ('' $args)) {
    p=($"p^/^$i)
    req_paths_list=($req_paths_list $p)
    if(test -d $i) {
        cd $i
        if(test -f _werc/config)
            . _werc/config
    }
}
cd $werc_root
# Redirections and other preprocessing
if(~ $#redirectPermanent 1)
    perm_redirect $"redirectPermanent
if not if(~ $#redirectPermanent 2 && {echo $SERVER_NAME^$req_path|grep -s $redirectPermanent(1) }) {
    # Experimental regexp sub-based redirect, probably should find a nicer interface
    to=`{echo $SERVER_NAME^$req_path|sed 's@'^$redirectPermanent(1)^'@'^$redirectPermanent(2)^'@'}
    if(! ~ $to $req_path)
        perm_redirect $to
}
# Set Page title
if(~ $pageTitle '')
	pageTitle=$siteTitle^' '^$siteSubTitle
if not
	pageTitle=$"pageTitle^' | '^$"siteTitle^' '^$"siteSubTitle
local_path=$sitedir$req_path
if(test -d $local_path) {
    if(! ~ $local_path */)
    	perm_redirect $req_path^'/'
    local_path=$local_path^'index'
}
select_handler
if(! ~ $#debug 0)
    dprint '  '$"SERVER_NAME^$"REQUEST_URI' - '$"HTTP_USER_AGENT' - '$"REQUEST_METHOD' - '$"handler
# Template/body selection
master_template=`{get_lib_file $master_template}
if(~ $response_format html) {
    # Is awk_buffer really useful?
	template $headers $master_template | awk_buffer
	echo '