Syntax Listing --- test

begin
site_resource_path = "../site_resources"
 
require 'rubygems'
 
# cgi systems
require 'cgi'
require 'cgi/session'
require 'cgi/session/pstore' 
# mime detection
require 'mime/types'
# import constants
require File.join(site_resource_path, "constants.rb")
include SiteConstants
 
 
# create a or session and set up cookies
def create_or_resume_session(cgi)
    session = CGI::Session.new(cgi, 
	  'database_manager' => CGI::Session::PStore, 
	  'prefix' => 'pstore_sid_',
	  'session_path' => '/')
 
 
    return session
end
 
 
# builds context (binding) and renders the template
def render_template(cgi, temppath, dir_list, file_list, both_list)
  require 'erb'
  formatter = ERB.new(File.open( temppath, 'r' ).read())
 
  #data = { :cgi => cgi }
 
 return formatter.result(binding).to_s
 
end
 
# Close a session, saving so it can persist
def save_close_session
  @session.close
end
 
# Delete session from the presistence area
def delete_session
  @session.delete
end
 
# Get a lot of data about a given file
def get_file_data(path)
 
    # sanitise input
    # TODO: bake the regex for speedliness
    fpath   = (SRV_ROOT + path.to_s )	.gsub(/[\/\$]\.\.\.*\//, "/").gsub(/\/+/, "/")
    path    = ("/" + path.to_s)		.gsub(/[\/\$]\.\.\.*\//, "/").gsub(/\/+/, "/")
 
    # split into an array
    fpath_segments  = fpath.split("/")[1..-1]
    path_segments   = path.split("/")[1..-1]
 
 
 
    filedata = {    :exists	=> File.exists?(fpath),
		    :path	=> path,
		    :fpath	=> fpath,
		    :patharr	=> path_segments || [],
		    :fpatharr	=> fpath_segments || [],
		    :readable	=> File::readable?(fpath),
		    :writable	=> File::writable?(fpath),
		    :executable => File::executable?(fpath),
		    :zerosize	=> File::zero?(fpath),
		    :sticky	=> File::sticky?(fpath),
		    :type	=> File.exists?(fpath) ? File::ftype(fpath) : "none",
		    :ctime	=> File.exists?(fpath) ? File::ctime(fpath) : "none",
		    :mtime	=> File.exists?(fpath) ? File::mtime(fpath) : "none",
		    :atime	=> File.exists?(fpath) ? File::atime(fpath) : "none",
		    :basename	=> File::basename(fpath, File::extname(fpath)),
		    :filename	=>  File.basename(fpath),
		    :extname	=> File::extname(fpath).gsub(".", ""),
		    :directory	=> File::directory?(fpath),
		    :mode	=> File.exists?(fpath) ? File.lstat(fpath).mode  & 0777 : 0000,
		    :mime	=> MIME::Types.type_for(fpath)
 
		    }
 
    return filedata 
end
 
# Get a directory, file list
def get_dir_listing(dir)
    dir_list	= []
    file_list	= []
    both_list	= []
 
    Dir.foreach(dir) do |entry|
	if entry != "." and entry != ".."
	    if File::directory?(dir + "/" + entry)
		dir_list << entry
	    else
		file_list << entry
	    end
	    both_list <<  entry
	end
    end
 
    return dir_list, file_list, both_list
end
 
#-------------------------------------------------------------------------
# Actual code starts here
#
#
#
 
 
 
# new header object 'n' such 
cgi = CGI.new("html4")
@session = create_or_resume_session(cgi)
 
 
 
 
# TODO: also check default is a file, etc
if !@session[:template] 
      @session[:template] = DEF_TEMPLATE_NAME
end
 
 
# TODO: reorganise layout such that
#       the templates are superdirectories of 
#	all their files and renderers.
#SRV_ROOT	= "/srv/http/"
#INDIRECT_ACCESS	= "/browse/"
 
 
#TEMPLATE_EXT	= "erb"
template_file	= File.join( SRV_ROOT, TEMPLATE_PATH, @session[:template] + "." + TEMPLATE_EXT)
TEMPLATE_ROOT = File.join( SRV_ROOT, TEMPLATE_PATH, @session[:template])
 
 
HIGHLIGHTER_CMD = File.join(SITE_RC_ROOT, "highlighter", "highlight.sh")
ODT_CONVERT_CMD = File.join(SITE_RC_ROOT, "odt_converter", "convert_odt_xhtml.sh")
PDF_CONVERT_CMD = File.join(SITE_RC_ROOT, "pdf_converter", "convert_pdf_html.sh")
 
 
 
# current file, dir    
@file	    = get_file_data( cgi.params['path']	    ? 
		cgi.params['path'][0]	: '/'	    )
@directory   = @file[:directory]			    ? 
		@file			: get_file_data( @file[:patharr][0..-2].join("/"))
 
# file listings
dir_list, file_list, both_list = get_dir_listing( File.join(SRV_ROOT, @directory[:path] ))
 
 
##	and build more advanced render choice engine
index_files = ["index.txt", "index.html", "index.htm", "README", "index.php"]
 
 
# need to find, redirect to index, readme etc in the view.
if @file[:directory] then
 
    if file_list.length == 1 then
	@file = get_file_data(File.join(@directory[:path], file_list[0]))
    else
	Dir.foreach(@directory[:fpath]){ |dir|
	if index_files.include?(dir)
	    @file = get_file_data(File.join(@directory[:path], dir))
	    break
	end
	}
    end
end
 
 
 
# happy fun render commands
cgi.out{
    cgi.html{
	 render_template( 
		cgi, 
		template_file,
		dir_list,
		file_list,
		both_list)
    }
}
 
 
# try to persist the session, but don't bother if it fails
begin
    save_close_session
rescue Exception => e
    # this happens on occasion
    # it's not important enough to warn the user
    # and does not affect rendering
end
 
 
# handy debug output
# wise to remove for production really, but meh.
rescue Exception => e
    puts "<h1> Oops.</h1>"
    puts e.to_s
    puts e.backtrace
end