#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
#include <sys/time.h>
using namespace std;

int main(int argc, char *argv[]) {
  int needacookie, newcookie, vpos ;
  char *envVar ;
  string *envVarS ;


  envVar = getenv("HTTP_COOKIE") ;
  if (envVar == (char *)NULL)
    needacookie = 1 ;
  else {
    envVarS = new string(envVar) ;
    if ((vpos=envVarS->find("visitor="))
                == envVarS->npos)
      needacookie = 1 ;
    else {
      envVarS->erase(0, vpos+8) ;
      if ((vpos=envVarS->find(";")) < envVarS->npos)
        envVarS->erase(vpos) ;
      needacookie = 0 ;
    }
  }


  if (needacookie) {
    struct timeval now ;
    gettimeofday(&now, (struct timezone *)NULL) ;
    newcookie = (now.tv_sec % 1000) * 1000000
                   + now.tv_usec ;
  }


  cout << "Content-type: text/html" << endl ;
  if (needacookie)
    cout << "Set-Cookie: visitor=" << newcookie << "; "
	 << "domain=cs.unca.edu; "
	 << "path=/~brock/cgiplay/; "
	 << "expires=Fri, 31-Dec-1999 13:46:00 GMT" << endl;
  cout << endl << "<HTML>" << endl 
       << "<HEAD>" << endl 
       << "<TITLE>Hello cookie owner</TITLE>" << endl
       << "</HEAD>" << endl
       << "<BODY>" << endl
       << "<H1>Hello cookie owner</H1>" << endl ;
  if (needacookie)
    cout << "Your new cookie is <kbd>"
         << newcookie << "</kbd>" << endl ;
  else
    cout << "Your old cookie is <kbd>"
         << *envVarS << "</kbd>" << endl ;
  cout << "</BODY>" << endl
       << "</HTML>" << endl ;
}

