/*
 * html.c:
 *
 * Copyright (c) 2000 James McKenzie <james@fishsoup.dhs.org>,
 * All rights reserved.
 *
 */

static char rcsid[] =
    "$Id: buildhtml.c,v 1.3 2002/06/23 17:16:39 root Exp root $";

/*
 * $Log: buildhtml.c,v $
 * Revision 1.3  2002/06/23 17:16:39  root
 * #
 *
 * Revision 1.2  2000/12/01 04:13:42  root
 * #
 *
 * Revision 1.1  2000/11/23 23:45:19  root
 * #
 *
 */


#include "project.h"
#include "disclaimer.h"

static int htmlcompar(const int *i1, const int *i2)
{
  Eweid e1, e2;
  char *t1, *t2;
  int i;

  e1 = files_work_by_num(*i1);
  e2 = files_work_by_num(*i2);

  t1 = eweid_to_text(e1);
  t2 = eweid_to_text(e2);

  i = strcasecmp(t1, t2);

  free(t2);
  free(t1);

  Obj_Delete(e2);
  Obj_Delete(e1);


  return i;
}

static void sort_works(int *index, int n)
{
  int i;
  int *ptr = index;


  for (i = 0; i < n; ++i)
    *(ptr++) = i;


  qsort(index, n, sizeof(int),
        (int (*)(const void *, const void *)) htmlcompar);

}


static void html_top(FILE * f, char *title,int nworks,int ntracks)
{

  fprintf(f,
          "<!doctype html public \"-//w3c//dtd html 4.0 transitional//en\">\n");
  fprintf(f, "<html>\n");
  fprintf(f, "<head>\n");
  fprintf(f,
          "   <meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">\n");
  fprintf(f, "</head>\n");
  fprintf(f,
          "<body text=\"#000000\" bgcolor=\"#FFFFFF\" link=\"#0000EF\" vlink=\"#51188E\" alink=\"#FF0000\">\n");
  fprintf(f, "<font color=\"#FF0000\">%s</font><br>\n", disclaimer);


  if (title) {
    fprintf(f, "<center><h1>%s</h1></center><br>\n", title);
  }

  if ((nworks) || (ntracks)) {
    fprintf(f, "<center>Archive contains: ");
    if (ntracks)  {
	fprintf(f,"%d tracks",ntracks);
	if (nworks) fprintf(f," in ");
		else fprintf(f,".");
    }
    if (nworks) 
	fprintf(f,"%d works.",nworks);
 
    fprintf(f, "</center><br>\n");
   }

  fprintf(f, "<ol>\n");         /* Could use dl */

}

static void html_bottom(FILE * f)
{
  fprintf(f, "</ol>\n");
  fprintf(f, "<br><font color=\"#FF0000\">%s</font>\n", disclaimer);
  fprintf(f, "</body>\n");
  fprintf(f, "</html>\n");
}

static char *htmlfn(Eweid e)
{
  char *a = eweidtoa(e);
  char *ret = (char *) malloc(strlen(a) + 6);
  strcpy(ret, a);
  strcat(ret, ".html");
  free(a);
  return (ret);
}

static char *htmlafn(char *html, Eweid e)
{
  char *a = htmlfn(e);
  char *ret = (char *) malloc(strlen(html) + strlen(a) + 1);

  strcpy(ret, html);
  strcat(ret, a);
  free(a);
  return ret;
}

static char *htmlaidx(char *html)
{
  char *a = "index.html";
  char *ret = (char *) malloc(strlen(html) + strlen(a) + 1);

  strcpy(ret, html);
  strcat(ret, a);
  return ret;
}

static void do_html_entry(FILE * f, char *html, Eweid e, int ig)
{
  char *t;
  char *uri,*urip;

  if (e) {
    t = eweid_to_text(e);

    if (ig) {
      uri = htmlfn(e);
    } else {
      char *abs = eweid_to_filename(e);
      uri = relativise(abs, html);
      free(abs);
    }
    if (uri) {
      urip=percent_encode(uri);

      fprintf(f, "<li><A HREF=\"%s\">%s</A>\n", urip, t); /* Could use dd */

      free(urip);
      free(uri);
    }
    if (t)
      free(t);
  }

}

static void do_group(char *html, Eweid g)
{
  char buf[1024];
  char *b = htmlafn(html, g);
  Flock f;
  Eweid e;
  int ig;
  FILE *fl;
  char *t = eweid_to_text(g);

  fl = fopen(b, "w");
  free(b);

  html_top(fl, t,0,0);
  free(t);


  if (g) {
    f = read_group(g);
    if (!f)
      return;

    e = f->head;

    while (e) {


      ig = is_group(e);

      if (ig)
        do_group(html, e);

      do_html_entry(fl, html, e, ig);

      e = e->next;
    }

    Obj_Delete(f);
  }

  html_bottom(fl);
  fclose(fl);

}


int main(int argc, char *argv[])
{
  Eweid e;
  int n;
  int *index;
  char *html;
  char *d = argv[1];
  int nworks,ntracks;

  enable_magic_heads();

  {
    struct stat s;

    if (!stat(d, &s)) {
      if (!S_ISDIR(s.st_mode)) {
        fprintf(stderr, "%s is not a directory\n", d);
        return -1;

      }
    }


    html = argv[2];

    if (!stat(html, &s)) {
      if (!S_ISDIR(s.st_mode)) {
        fprintf(stderr, "%s is not a directory\n", html);
        return -1;

      }
    }
  }

  files_ftw(d);

  html = directory_of(html);

  n = files_num_works();

  index = (int *) malloc(sizeof(int) * n);

  sort_works(index, n);

  ntracks=files_num();
  nworks=files_num_works();

  {
    int i, ig;
    char *ht = htmlaidx(html);
    FILE *fl = fopen(ht, "w");

    free(ht);
    html_top(fl, "Master Index",nworks,ntracks);

    for (i = 0; i < n; ++i) {
      e = files_work_by_num(index[i]);
      if (e) {

        ig = is_group(e);

        if (ig)
          do_group(html, e);

        do_html_entry(fl, html, e, ig);

/*

	char *a,*b,*c;
          a = eweidtoa (e);
          b = eweid_to_text (e);
  	c=eweid_to_filename(e);

          printf ("%d %s\t(%d:%s) %s\n", is_group(e), b,i,a,c);

	free(c);
          free (b);
          free (a);
*/

        Obj_Delete(e);
      } else {
        printf("\t\t(%d:???)\n", i);
      }
    }

    html_bottom(fl);
    fclose(fl);

  }





  return 0;
}
