// Emacs style mode select -*- C++ -*-
//--------------------------------------------------------------------------
//
// Copyright(C) 2000 Simon Howard
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
//--------------------------------------------------------------------------
//
// HTML Page Creator
//
// Every few minutes we update the html index page. This includes
// statistics for the server and other information.
//
// By Simon Howard
//
//--------------------------------------------------------------------------

#include <stdio.h>


#include "serv.h"
#include "cmds.h"
#include "main.h"


//"~/public_html/index.html"

// enable if you want to use images on the page
#define USE_IMAGES

// index.html should be stored in ~/public_html
char *html_filename = NULL;

char *bg_color = "#ffffff";
char *text_color = "#000000";

static FILE *html_file;

static void HTML_WriteHeader()
{
  fprintf(html_file, "<html>\n");

  fprintf(html_file, "<head><title> SMMUSERV Stats page </title></head>\n");
  
  fprintf(html_file, "<body bgcolor=%s text=%s link=%s vlink=%s>",
	  bg_color, text_color, text_color, text_color);

#ifdef USE_IMAGES
  fprintf(html_file, "<img src=\"smmuserv.png\" border=0 alt=\"SMMUSERV\">\n");
#else
  fprintf(html_file, "<h2> SMMUSERV statistics </h2>\n");
#endif
}

// current status

extern struct in_addr server_list[MAX_SERVERS];

static void HTML_WriteStatus()
{
  int i, total;
  connection_t *conn;
  int uptime; // uptime in seconds
  time_t timer;

  time(&timer);

  fprintf(html_file,
	  "<br>Stats page: Generated %s <br><br>\n",
	  ctime(&timer));

  fprintf(html_file, "<h3> Status </h3> \n");

  fprintf(html_file, "uptime: ");

  uptime = time(NULL) - startup_time;

  if(uptime > (24 * 60 * 60))
    {
      fprintf(html_file, "%i days ", uptime / (24 * 60 * 60));
      uptime %= 24 * 60 * 60;
    }

  fprintf(html_file, "%02i:%02i:%02i<br>\n",
		uptime / (60*60), (uptime / 60) % 60, uptime % 60);
  
  // count number of people currently connected
  
  for(i=0, conn = connections.next; conn; conn = conn->next, i++);
  
  fprintf(html_file, "%i users currently connected to smmuserv <br>\n", i);
  fprintf(html_file, "%i connections in total<br>", total_connections);
  
  total = 0;

  for(i=0; i<MAX_SERVERS; i++)
    if(server_list[i].s_addr)
      total++;

  fprintf(html_file, "%i servers stored in list<br>\n", total);

  // write message of the day

  if(motd)
    fprintf(html_file, "motd: \"%s\"<br>\n", motd);
  else
    fprintf(html_file, "no motd set <br>\n");

  // usage stats

  fprintf(html_file, "<h3> Usage stats </h3> \n");
  fprintf(html_file, "%i adds to list <br>\n", num_adds);
  fprintf(html_file, "%i list retrievals <br>\n", num_retrieves);
}

static void HTML_WriteLast5()
{
  int i;
  int written = 0;

  fprintf(html_file, "<h3> Last 5 Registered Servers </h3>\n");
  
  // go thru server list
  // we start from the most recently added and go backwards

  i = server_num;
  
  do
    {
      i = (i - 1) % MAX_SERVERS;

      if(i == server_num) break;
      if(!server_list[i].s_addr) break;
      if(++written > 5) break; // only 5
      
      fprintf(html_file, "%s<br>\n", inet_ntoa(server_list[i]));
    } while(1);

  if(written == 0)
    fprintf(html_file, "no servers registered yet<br>\n");
}

void HTML_WriteVersion()
{
  fprintf(html_file, "<h3> Version </h3>\n");
  fprintf(html_file, "Build date/time: <br>\n");
  fprintf(html_file, __DATE__ "\n");
  fprintf(html_file, __TIME__ "\n");
}

void HTML_WriteFooter()
{
  fprintf(html_file,
	  "<br><hr>"
	  "SMMUSERV by Simon Howard 'Fraggle' <br>\n"
	  "<a href=\"http://fraggle.alkali.org/\">Fraggle Rock</a><br>\n"
	  "</body>\n"
	  "</html>");
}

void BuildHTML()
{
#ifdef LOGALL
  LogMessage("write stats file\n");
#endif

  if(!html_filename)
    return;

  html_file = fopen(html_filename, "w");

  if(!html_file)
    error("BuildHTML");
  
  HTML_WriteHeader();

  HTML_WriteStatus();
  HTML_WriteLast5();
  HTML_WriteVersion();

  HTML_WriteFooter();

  fclose(html_file);
}
