#!/usr/bin/python
# generiert den graphen fuer einen DFA, der
# prueft ob eine zahl dargestellt zur basis k
# durch n teilbar ist
import os, sys, cgi
import cgitb; cgitb.enable()
from calc import CellCloud
form = cgi.FieldStorage()
reuse =  int(form.getvalue('reuse'))
bw = int(form.getvalue('bw'))
ch = int(form.getvalue('ch'))
x = int(form.getvalue('x', 3))
y = int(form.getvalue('y', 3))
scale=  int(form.getvalue('scale'))
if 'HTTP_IF_MODIFIED_SINCE' in os.environ:
	print "Status: 304 Not Modified\n"
	sys.exit(0)
if x * y > 10000: x,y=20,20
if x * y * scale > 1000000: scale = 10
try:
	c = CellCloud(reuse, bw, ch)
	print "Last-Modified: Fri, Nov 16 19:29:10 CET 2007\nContent-Type: image/png\n"
	c.draw((x,y), sys.stdout, scale=scale)
except Exception, e:
	print "Content-Type: text/plain\n"
	print "Failed generating the image because of:", e
