/* Read ThermIIC$Logged environment variable
   reformat to html table
   and print to std out */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <time.h>

#include "kernel.h"

int main()
{
  char logged_string[256], query_string[32] = "", *pos, refstr[16],
  comstr[256] ;
  int i, t, m, n, refresh=30, table_triggered = 0, s=0 ;

  t = (time(NULL) / 10)%10000 ;

  _kernel_getenv("QUERY_STRING", query_string, 32) ;

  /* Query string gives Mag number */
  if (strlen(query_string) > 0) {
    /* Query string format: mag&size */
    sscanf(query_string, "%d&%d", &m, &s) ;
    if ((m<0) || (m>3)) {
      m = 0 ;
    }
    if ((s<0) || (s>1)) s = 0 ;
  } else {
    m = s = 0 ;
  }

  n = (s==0) ? m + 4 : m ;

  /* Mag number determines refresh interval */

  for (i=m; i>0; --i) refresh *= 10 ;
  sprintf(refstr, "%d", refresh) ;

  /* Header first */

  printf("<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 3.0//EN\">\n") ;
  printf("<HTML>\n<HEAD>\n") ;
  printf("<LINK REV=MADE HREF=\"mailto:simon@melhuish.net\">\n") ;

  printf("<META HTTP-EQUIV=\"expires\" CONTENT=\"0\">\n") ;
  printf("<META HTTP-EQUIV=\"cache-control\" CONTENT=\"no-cache\">\n") ;
  printf("<META HTTP-EQUIV=\"refresh\" CONTENT=\"%s\">\n", refstr) ;
  printf("<TITLE>ThermIIC</TITLE></HEAD>\n") ;
  printf("<BODY BGCOLOR=\"#FFFFFF\" TEXT=\"#000000\" LINK=\"#000099\">\n") ;

  _kernel_getenv("ThermIIC$Logged", logged_string, 256) ;

  /*printf("%s\n", logged_string) ;*/

  pos = logged_string ;

  /* Each value follows a ',' */

  pos = strchr(pos, ',') ;
  for (i = 0; i < 8; ++i) {
    int triggered = 0 ;
    /*printf("%d > ", i) ;*/
    if ((pos) && (*pos)) {
      ++pos ;

      /* Skip spaces */
      while (*pos == ' ') ++pos ;

      while ((*pos) && (*pos != ',')) {
        if (!triggered) {
          triggered = 1 ;
          if (!table_triggered) {
            table_triggered = 1 ;
            printf("<TABLE cellpadding=\"2\">\n") ;
          }
          printf("<TR><TD WIDTH=\"20\" ALIGN=\"center\"bgcolor=\"#eeeebb\"><FONT color=\"#000000\">%d</FONT></TD><TD WIDTH=\"60\" ALIGN=\"right\" bgcolor=\"#558800\"><FONT color=\"#ffffff\">", i) ;
        }
        putchar(*pos++) ;
      }
      if (triggered) {
        printf("</FONT></TD></TR>\n") ;
      }
    }
    /*printf("\n") ;*/
  }

  if (table_triggered) {
    printf("</TABLE>\n") ;
  }

  printf("<P>") ;
  if (s==0) printf("<A HREF=\"logged?%d&%d\" TARGET=\"_content\">", m, 1) ;
  printf("<IMG SRC=\"mon_pngsN/%d%dN.png\" ALT=\"Mag. %d\"",
    n, t, m) ;
  if (s==0) printf("WIDTH=\"120\" HEIGHT=\"80\" BORDER=\"0\"></A>") ;
  else printf("WIDTH=\"400\" HEIGHT=\"200\">") ;
  printf("</P>\n") ;

  printf("<P>Mag:\n") ;
  for (i=0;i<4;++i) {
    if (i!=m) {
      printf(" [<A HREF=\"logged?%d&%d\">%d</A>]", i, s, i) ;
    } else {
      printf(" [<BF>%d</BF>]", i) ;
    }
  }
  printf("</P>\n") ;
  printf("</BODY></HTML>\n") ;

  /* Generate the command string for wipe */
  sprintf(comstr, "wipe <InetServ$WebDir>.cgi-bin.mon_pngsN.%d*N/png ~C~VF",
    n) ;

  system(comstr) ;

  /* Generate the command string for thermpng */
  sprintf(comstr, "thermpng <TherMonS$Dir>.Mag_%d <InetServ$WebDir>.cgi-bin.mon_pngsN.%d%dN/png mon %d %d",
    m, n, t, (s==0) ? 120 : 400, (s==0) ? 80 : 200) ;

  system(comstr) ;

  return 0 ;
}
