#!/usr/bin/python
import cgi
import os
import sys
#import util
#import pdb
cgi_path = "/home/drmpolicy/www/cgi-bin/logintools/"

#CGI HEADERS
def CGIhead():
	print "Content-Type: text/html"
	print

#pdb.set_trace()


#The logintools modules are located in the following folder, which must be added
#to the path
sys.path.append('modules/')
from logintools import login, isloggedin

#The following block runs the login function for logintools
userdir = 'users/'
action=None
form = cgi.FieldStorage()
action, userconfig = login(form, userdir) 

#If we made it here, the login should have been successful (signs telling otherwise) or a new account was created
#if a new account was created, the 'work' value won't be POSTed, so we can start over
if not form.has_key("work"):
	from logintools import displaylogin
	displaylogin(userdir)

work = form["work"].value
#CGIhead()

if work == "upload":
#	print "Hello " + userconfig['username'] + ", <a href=\"uploadpage\">click here to proceed to the upload page.</a>"
	uploadpage = "Location: http://www.purpuras.net/cgi-bin/logintools/uploadpage"
	print uploadpage;
	CGIhead()
#	util.redirect(req, uploadpage)
#	os.execvp(uploadpage, [uploadpage, ])

elif work == "view":
#	print "Hello " + userconfig['username'] + ", <a href=\"results_main\">click here to view your results.</a>"
	viewpage = "Location: http://www.purpuras.net/cgi-bin/logintools/results_main"
	print viewpage;
	CGIhead()

else:
	CGIhead()
	print "You did not select the proper work to do.  Error!"
