#python

import os, sys, time

# note that thr33 debug does not carry into pypage execution
debug = 0

def cgi_init():
	"""
	assumes 'data' and 'cgi-bin' are peer directories
	"""
	global l, c, files_dir, SCRIPT
	current = sys.path[0]
	consts = current.split(os.sep)
	consts = (os.sep).join(consts[:consts.index("data")])+os.sep+"cgi-bin"+os.sep+"TheMingServer"
	sys.path.append(consts)

	import Lib3
	l = Lib3.Lib3(debug)
	import Constants
	c = Constants.Constants()
	files_dir = c.FILES_DIR
	SCRIPT = c.CGI_NAME
	return

cgi_init()
l.prn("FILES_DIR",files_dir)

NOTATE = files_dir+os.sep+"gq.notate"
FILE = files_dir+os.sep+"gq.response"
note_dict = l.get_dict(NOTATE)
key = note_dict.keys()[0]
note_dict = note_dict[key]
l.prn("note_dict", note_dict)

id = note_dict["comment"]
text = note_dict["note"]

file_dict = l.get_dict(FILE)
dates = file_dict.keys()
l.prn("file_dict", file_dict)

for date in dates:
	dict = file_dict[date]
	if (dict["id"] == id):
		dict["comment_text"] = dict["comment_text"] + "\n---reply---\n" + text

l.write_data(file_dict, FILE)
print '''<p> Your reply was appended succesfully. <br/>
You should now be redirected to the
<a href="%s?page=sites/ming-cgi/server/open/response/result">Comments page</a>.
</p>''' % SCRIPT
print '''<meta HTTP-EQUIV="Refresh" CONTENT="0;
URL=%s?page=sites/ming-cgi/server/open/response/result">''' % SCRIPT

