#!/usr/local/bin/python
########################################################################
#
#  zeiterfassung.py ... script to log the work of a day at the 
#                       institute of computer graphics, vienna
#
#  there are two ways of using zeiterfassung.py:  1.) zeiterfassung.py
#  is used via http to generate a form which can be displayed by most
#  web-browsers.  2.) zeiterfassung.py is used as call-back-script for
#  the form it produces (usage 1).
#
########################################################################
#
#  (c) Helwig Hauser, 1997-2000.
#
########################################################################
#
#  imports ...
#
import cgi, os, string, time
#
########################################################################
#
#  where is the HTTP-server? (excl. '/')
#  where is the script?      (incl. '/')
#
if os.environ.has_key('DOCUMENT_ROOT') : ZE_HTDOCS_DIR = os.environ['DOCUMENT_ROOT']
else                                   : ZE_HTDOCS_DIR = "/usr/local/httpd/htdocs"
#
ZE_SCRIPT_DIR = ZE_HTDOCS_DIR+"/cgi-cg/"
#
########################################################################
#
#  are we called ... as a callback        (='POST')
#                ... directly via the web (='GET')
#

#
########################################################################
#
#  files that are read ...
#
ZE_DEFAULTS_FILE = ZE_SCRIPT_DIR+"zeiterfassung.defaults.py"
ZE_GLOBALS_FILE  = ZE_SCRIPT_DIR+"zeiterfassung.globals.py"
ZE_FUNCTION_FILE = ZE_SCRIPT_DIR+"zeiterfassung.functions.py"
#
if os.environ.has_key('REMOTE_USER') : ZE_USER_SETTINGS = "/home/private/"+os.environ['REMOTE_USER']+"/.zeiterfassung.py"
else                                  : ZE_USER_SETTINGS = "./.zeiterfassung.py"
#
########################################################################
#
# read zeiterfassung defaults ...
#
execfile(ZE_DEFAULTS_FILE)
#
########################################################################
#
# read user setting ...
#
try    : execfile(ZE_USER_SETTINGS)
except : pass
#
########################################################################
#
# read zeiterfassung globals ...
#
execfile(ZE_GLOBALS_FILE)
#
########################################################################
#
# read zeiterfassung functions and global data ...
#
execfile(ZE_FUNCTION_FILE)
#
########################################################################
#
#  main ...
#
if   not calledViaHTTP()  : printUsageErrorMsg()
elif not authorized()     : printNotAuthorizedErrorMsg()
elif not filesOK()        : printFileSystemErrorMsg()
elif os.environ['REQUEST_METHOD'] == 'GET' :

  initData(today)

  if savedDataFound() : loadData()

  printForm()

else:

  convertForm()

  if   data['request'] == ZE_SAVE_STRING : saveData()
  elif data['request'] == ZE_LOAD_STRING : 
    if savedDataFound()                    : loadData()
    else                                   : newData()
  elif data['request'] == ZE_DEL_STRING :
    if savedDataFound()                    : delData()
    else                                   : 
      oldDataDate = data['dataDate']
      initData(today)
      globals['status'] = 'Keine Daten f&uuml;r den '+oldDataDate+' gefunden.  '+globals['status']

  if data['request'] == ZE_FINE_STRING :
    saveData()
    printSummary()
  else :
    printForm()

#
# EOF.
#

