#!/Library/Frameworks/Python.framework/Versions/Current/bin/python
# L-34 MCS 275 Wed 9 Apr 2008 : cookie_date.py

import Cookie
import os
import time

def GetCookie():
   """
   Retrieves cookie or creates one.
   If the date field is not there,
   then it is initialized with ''.
   Returns the cookie.
   """
   if os.environ.has_key('HTTP_COOKIE'):
      c = Cookie.Cookie(os.environ['HTTP_COOKIE'])
   else:
      c = Cookie.Cookie()
   if not c.has_key('date'): c['date'] = ''
   return c

def main():
   """
   Retrieves a cookie and writes
   the value of counter to the page.
   """
   c = GetCookie()
   if c['date'].value == '':
      m = 'welcome first time user'
   else:
      m = 'last visited on %s' % c['date'].value
   c['date'] = time.ctime()
   print c
   print "Content-Type: text/plain\n"
   print m

main()
