/* Time-stamp: <97/01/22 13:51:42 john> */

/* 
# Purpose: html headers and trailers, debugging, time of day, whatever
 */

#include <stdio.h>
#include "attr.h"
#include "cgi.h"
#include "style.h"
#include "misc.h"

static int done_header = 0;

void html_header(char *title)
{
  if (!done_header)
    {
      printf("Content-type: text/html\n\n");
      printf("<html><head><title>%s</title></head>\n", title);
      done_header = 1;
    }
}

void vrml_header()
{
  if (!done_header)
    {
      printf("Content-type: x-world/x-vrml\n\n");
      printf("#VRML V1.0 ascii\n");
      done_header = 1;
    }
}

void error_header(char *error_title)
{
  if (!done_header)
    {
      printf("Content-type: text/html\n\n");
      printf("<html>\n<head>\n  <title>%s</title>\n</head>\n<body>\n<h1>%s</h1>\n",
	     error_title, error_title);
      done_header = 1;
    }
}

void error_trailer()
{
  printf("</body></html>\n");
}

void show_entry(struct entry *entry)
{
  struct attribute *attr = entry->attributes;

  printf("<pre>\n");

  printf("%s ``%s'' (h=%d,w=%d)\n",
	 entry->type, entry->name,
	 entry->height, entry->width);

  while (attr != NULL)
    {
      printf("  %s: \"%s\"\n", attr->key, attr->value);
      attr = attr->next;
    }

  printf("</pre>\n\n");

}

void show_entries(struct entry *entries)
{
  printf("showing entries\n");
  while (entries != NULL)
    {
      printf("<h3>%s: <a name=\"%s\">%s</a>(%d,%d) (column %d)</h3>\n",
	     entries->type,
	     entries->name, entries->name,
	     entries->height, entries->width,
	     entries->column);
      show_entry(entries);
      entries = entries->next;
    }

}

#include <sys/types.h>
#include <sys/time.h>
#include <time.h>

#define HOSTNAMEMAX 100

void print_time()
{
  time_t my_time;
  
  if (time(&my_time) == -1)
    {
      printf("an unknown time ");
  } else {
    printf("%s ",
	   ctime(&my_time));
  }
}

void print_run_details()
{
  char hostname[HOSTNAMEMAX];

  printf("<hr>\n<p>Generated at ");
  print_time();
  if (gethostname(hostname, HOSTNAMEMAX) == 0)
    {
      printf("on %s", hostname);
  } else {
    printf("on an unknown host");
  }
  printf(".</p>\n");
}

char *output_modes[9] =
{
  /* substring of X must come after X in these tables */
  /* must match the order of OUTPUT_... in style.h */
  "html-pre",
  "html",
  "parsed",
  "raw",
  "grid",
  "text",
  "vrml2",
  "vrml",
  "data"
  ""
};

char *months[13] =
{
  /* substring of X must come after X in these tables */
  "jan", "feb", "mar", "apr", "may", "jun",
  "jul", "aug", "sep", "oct", "nov", "dec", ""
};

char *weekdays[8] =
{
  /* substring of X must come after X in these tables */
  "sun", "mon", "tue", "wed", "thu", "fri", "sat", ""
};

void fill_in_current_time(struct style *style)
{
  time_t my_time;
  struct tm *time_parts;
  
  if (time(&my_time) == -1)
    {
#if 0
      printf("Could not get time\n");
#endif
  } else {
    time_parts = localtime(&my_time);
    style->month = time_parts->tm_mon+1;
    style->hour = time_parts->tm_hour;
    style->day = time_parts->tm_wday+1;
    style->forced_date = 0;
  }
}

/* end of misc.c */
