#!/System/Library/Frameworks/Python.framework/Versions/2.5/Resources/Python.app/Contents/MacOS/Python
# -*- coding: Latin-1 -*-

# ***** include the biocase.lib directory in the python sys path for importing *****
import os, sys
execfile( os.path.abspath(os.path.join(os.path.dirname( __file__ ), os.path.pardir, os.path.pardir, os.path.pardir, 'lib', 'biocase', 'adjustpath.py' ) ) )

import cgitb; cgitb.enable()
import cgi, logging
import biocase.configuration
from biocase.tools.templating import PageMacro

cfg = biocase.configuration.Cfg()

# read template
tmpl = PageMacro('Content', PageMacro.DELMODE)
tmpl.load('Content', os.path.join(cfg.wwwLocator,'utilities','queryforms','_qf_manual.html') )

# get wrapper url
form = cgi.FieldStorage()
dsa=None
if form.has_key('dsa'):
    from biocase.datasources       import Datasource
    dsa = form.getfirst('dsa')
    try:
        dsaObj = Datasource(name=dsa)
        wrapper_url = dsaObj.getBioCASeAccessPoint()
        psfObj = dsaObj.getPSFObj()
    except:
        raise "Datasource %s does not exist!"%dsa
elif form.has_key('url'):
    wrapper_url = form.getfirst('url')
else:
    raise "No wrapper URL found!"

# fill template with url
tmpl['wrapper_url']=wrapper_url
if cfg.server.debug:
    if dsa is None:
        tmpl['cfg.server.debug']='%s. Logs at <i>%s</i>' %(cfg.html.true, cfg.logLocator)
    else:
        tmpl['cfg.server.debug']='<span class="OK">%s</span>. Logs at <i>%s</i>' %(logging.getLevelName(psfObj.loglevel), cfg.logLocator)
else:
    if dsa is None:
        tmpl['cfg.server.debug']='%s. Logs at <i>%s</i>' %(cfg.html.false, cfg.logLocator)
    else:
        tmpl['cfg.server.debug']='<span class="warning">%s</span>. Logs at <i>%s</i>' %(logging.getLevelName(psfObj.loglevel), cfg.logLocator)


#
# print HTML
#
print 'Content-Type: text/html; charset=utf-8\n\n'
print str(tmpl)
