/*
      _______                          .__ .__   .__   .__                 
      \      \    ____   __ __ _______ |__||  |  |  |  |__|  ____    ____  
      /   |   \ _/ __ \ |  |  \\_  __ \|  ||  |  |  |  |  | /  _ \  /    \ 
     /    |    \\  ___/ |  |  / |  | \/|  ||  |__|  |__|  |(  <_> )|   |  \
     \____|__  / \___  >|____/  |__|   |__||____/|____/|__| \____/ |___|  /
    =========\/======\/=================================================\/==
  v0.01 04/JUL/2007 (C) Copyright 2007-2007 Scott D. Yelich SOME RIGHTS RESERVED
 .,-*~'`^`'~*-,._.,-*~'`^`'~*-,._.,-*~'`^`'~*-,._.-*~'`^`'~*-,._.,-*~'`^`'~*-,. 


  LICENSE:  Creative Commons Attribution 3.0 License.
  SEE:      http://creativecommons.org/licenses/by/3.0/


  Sun Jul 29 21:33:38 EDT 2007, v0.02 sdy

  This is a *very* simple program written to do a web page
  hit counter using sqlite3.

*/

//  C++

#include <iostream>
#include <iomanip>
#include <sstream>
#include <fstream>
#include <string>

//  LOCAL

#include "Unique.h"

int
main()
{

  int hits = 0;;

  Unique unique;

  char *qs;
  qs = getenv("QUERY_STRING");
  if (qs) {
    std::string q(qs,strlen(qs));
    std::stringstream isstr;
    isstr.str(q);
    int x;
    if ((isstr >> x)) {
      unique.period = x;
    }
  }

  char * ip_cstr = getenv("REMOTE_ADDR");
  char * referer_cstr = getenv("HTTP_REFERER");

  if (ip_cstr && referer_cstr) {
    std::string page(referer_cstr, strlen(referer_cstr));
    std::string ip(ip_cstr, strlen(ip_cstr));
    hits = unique.hit(page, ip);
  }

  std::cout << "Content-Type:  image/gif" << std::endl
    << "Expires: 0" << std::endl
    << "Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0" << std::endl
    << "Pragma: no-cache" << std::endl
    << std::endl;

  static const char gif1x1t[] = "\x47\x49\x46\x38\x39\x61\x01\x00\x01\x00\x80\x00\x00\xff\xff\xff\xff\xff\xff\x21\xf9\x04\x01\x0a\x00\x01\x00\x2c\x00\x00\x00\x00\x01\x00\x01\x00\x00\x02\x02\x4c\x01\x00\x3b\x00";

  std::string gif(gif1x1t,
sizeof(gif1x1t));
  std::cout << gif;

  return 0;

}
