/*
   File:        html.c
   Author:      Andrew W. Moore
   Created:     Sat Jun 22 08:10:39 EDT 2002
   Description: Writing results to HTML

   Copyright 2002, Andrew W. Moore
*/

#include "utils.h"
#include "html.h"

hout *mk_hout_from_string(char *dirname,char *hname,char *title)
{
  hout *x = AM_MALLOC(hout);
  char *filename = mk_printf("%s/%s.html",dirname,hname);

  if ( file_exists(filename) )
    my_errorf("The file %s already exists. I refuse to overwrite it",filename);

  x -> s = fopen(filename,"w");

  if ( x -> s == NULL )
  {
    my_errorf("I cannot create the file %s in order to make an HTML page.\n"
	      "The most likely cause for this is that there is no directory\n"
	      "available to write to. You should create this directory first\n"
	      "(and make sure it's empty) and then run this code.\n"
	      "To make the directory type:\n\nmkdir %s\n",filename,dirname);
  }

  x -> filename = mk_copy_string(filename);
  x -> dirname = mk_copy_string(dirname);
  x -> itemize_levels = 0;

  fprintf(x->s,"<HEAD>\n");
  fprintf(x->s,"<TITLE>%s</TITLE>\n",title);
  fprintf(x->s,"</HEAD>\n");
  fprintf(x->s,"\n");
  fprintf(x->s,"<BODY BGCOLOR=\"#EEFFEE\">\n");
  fprintf(x->s,"\n");
  fprintf(x->s,"<BODY>\n");
  fprintf(x->s,"\n");
  fprintf(x->s,"<H1>%s</H1>\n",title);
  fprintf(x->s,"\n");
  fprintf(x->s,"<P>\n");
  fprintf(x->s,"\n");

  free_string(filename);

  return x;
}

void free_hout(hout *x)
{
  if ( x->itemize_levels != 0 )
    my_errorf("Missing hend_itemize in %s",x->filename);

  fprintf(x->s,"\n");
  fprintf(x->s,"<P>\n");
  fprintf(x->s,"\n");
  fprintf(x->s,"</BODY>\n");

  fclose(x->s);
  free_string(x->filename);
  free_string(x->dirname);

  AM_FREE(x,hout);
}

void fprintf_hout(FILE *s,char *m1,hout *x,char *m2)
{
  char *buff;

  buff = mk_printf("%s -> filename",m1);
  fprintf_string(s,buff,x->filename,m2);
  free_string(buff);

  buff = mk_printf("%s -> dirname",m1);
  fprintf_string(s,buff,x->dirname,m2);
  free_string(buff);

  buff = mk_printf("%s -> itemize_levels",m1);
  fprintf_int(s,buff,x->itemize_levels,m2);
  free_string(buff);
}

void phout(hout *x)
{
  fprintf_hout(stdout,"hout",x,"\n");
}

hout *mk_copy_hout(hout *old)
{
  my_errorf("You may not make a copy of a hout");
  return NULL;
}

void hprint_from_string(hout *ho,char *s)
{
  fprintf(ho->s,"%s",s);
}

void hprintf(hout *ho,const char *format, ...)
{
  VA_MAGIC_INTO_BIG_ARRAY
  hprint_from_string(ho,big_array);
}

hout *mk_hout(char *dirname,char *hname,const char *format, ...)
{
  VA_MAGIC_INTO_BIG_ARRAY
  return mk_hout_from_string(dirname,hname,big_array);
}

void hlink_from_string(hout *ho,char *link_hname,char *string)
{
  fprintf(ho->s,"<A HREF=\"%s.html\">%s</A>\n",link_hname,string);
}

void hlinkf(hout *ho,char *link_hname,const char *format, ...)
{
  VA_MAGIC_INTO_BIG_ARRAY
  hlink_from_string(ho,link_hname,big_array);
}

void hlink_text_from_string(hout *ho,char *link_hname,char *string)
{
  fprintf(ho->s,"<A HREF=\"%s.txt\">%s</A>\n",link_hname,string);
}

void hlinkf_text(hout *ho,char *link_hname,const char *format, ...)
{
  VA_MAGIC_INTO_BIG_ARRAY
  hlink_text_from_string(ho,link_hname,big_array);
}

void hbegin_itemize(hout *ho)
{
  fprintf(ho->s,"<P>\n");
  fprintf(ho->s,"<UL>\n");
  ho -> itemize_levels += 1;
}

void hend_itemize(hout *ho)
{
  fprintf(ho->s,"</UL>\n");
  fprintf(ho->s,"<P>\n");
  if ( ho -> itemize_levels <= 0 )
    my_error("misplaced hend_itemize");
  ho -> itemize_levels -= 1;
}

void hitem(hout *ho)
{
  if ( ho -> itemize_levels <= 0 )
    my_error("misplaced hitem or missing hbegin_itemize");
  fprintf(ho->s,"<LI>\n");
}
    
void hpara(hout *ho)
{
  fprintf(ho->s,"<P>\n");
}

void check_dirname_ok(char *dirname)
{
  char *index_name = mk_printf("%s/index.html",dirname);
  printf("Will be sending HTML output to files in directory %s\n",dirname);
  if ( file_exists(index_name) )
  {
    my_errorf("Sorry. The HTML output code insists on writing to an empty\n"
	      "directory. We are being very careful to avoid over-writing\n"
	      "earlier results which you may not have wanted overwritten.\n"
	      "I find that the file %s exists in the directory\n"
	      "%s, which is why I hereby abort.\n",index_name,dirname);
  }
  else
  {
    FILE *s = fopen(index_name,"w");
    if ( s == NULL )
    {
      my_errorf("Sorry. The HTML output code insists on writing to files in\n"
		"directory %s.\nBut I am not able to create the file %s, and\n"
		"I guess that is because the directory %s does not exist.\n"
		"Please create that directory and re-run.\n",dirname,
		index_name,dirname);
    }
    else
    {
      fclose(s);
      unlink(index_name);
    }
  }
  free_string(index_name);
}

char *dirname_from_args(char *key,int argc,char *argv[],char *default_dirname)
{
  char *dirname = string_from_args(key,argc,argv,default_dirname);

  if (eq_string(dirname,"NULL")) dirname = NULL;
  else check_dirname_ok(dirname);
  return dirname;
}
