/* log.c */

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

#include "swis.h"

#include "err.h"
#include "log.h"
#include "msgtrans.h"
#include "nfchoices.h"
#include "nfstr.h"
#include "workdir.h"

static FILE *log_file = NULL;
static FILE *found_file = NULL;

static char zak[256];

void log_start()
{
  if (nfchoices.foundfile[0])
    found_file = fopen(nfchoices.foundfile, "w");
  if (nfchoices.log != NoLog)
  {
    char *leaf;
    strcpy(zak, nfchoices.logfile);
    leaf = strrchr(zak, '.');
    if (leaf)
    {
      *leaf = 0;
      _swix(OS_File, _INR(0,1)|_IN(4), 8, zak, 0);
    }
  }
  switch (nfchoices.log)
  {
    case NoLog:
      return;
    case AppendLog:
      log_file = fopen(nfchoices.logfile, "a");
      break;
    case OverWriteLog:
      log_file = fopen(nfchoices.logfile, "w");
      break;
  }
  if (!log_file)
    err_complain(0, msgs_lookup("Log"));
}

void log_complete(int found)
{
  if (nfchoices.resultfile[0])
  {
    FILE *result_file = fopen(nfchoices.resultfile, "w");
    if (result_file)
    {
      fprintf(result_file, "%d\n", found);
      fclose(result_file);
    }
  }
  if (found_file)
  {
    fclose(found_file);
    found_file = NULL;
  }
  if (log_file)
  {
    strcpyC(zak, msgs_lookup("LogComplete"));
    fprintf(log_file, zak, found);
    log_time();
    fprintf(log_file, "\n\n\n");
    fclose(log_file);
    log_file = NULL;
  }
  if (nfchoices.transient)
  {
    /* Prevent result file info being overwritten by log_abort */
    nfchoices.resultfile[0] = 0;
    exit(0);
  }
}

void log_abort()
{
  char zak[128];
  if (nfchoices.resultfile[0])
  {
    FILE *result_file = fopen(nfchoices.resultfile, "w");
    if (result_file)
    {
      fprintf(result_file, "%d\n", 0);
      fclose(result_file);
    }
  }
  if (found_file)
  {
    fclose(found_file);
    found_file = NULL;
  }
  if (log_file)
  {
    strcpyC(zak, msgs_lookup("Aborted"));
    fprintf(log_file, zak);
    log_time();
    fprintf(log_file, "\n\n\n");
    fclose(log_file);
    log_file = NULL;
  }
  if (nfchoices.transient)
    exit(0);
}

void log_log(const char *format, ...)
{
  va_list ap;
  if (!log_file)
    return;
  va_start(ap, format);
  vfprintf(log_file, format, ap);
  va_end(ap);
}

void log_time()
{
  time_t now = time(NULL);
  if (!log_file)
    return;
  fprintf(log_file, asctime(localtime(&now)));
}

void log_newline()
{
  if (log_file)
    putc('\n', log_file);
}

void log_backspace()
{
  if (!log_file)
    return;
  fseek(log_file, -1, SEEK_END);
}

void log_found(unsigned article)
{
  if (found_file)
    fprintf(found_file, "%d ", article);
  if (log_file)
  {
    strcpyC(zak, msgs_lookup("Found"));
    log_log(zak, article);
    log_newline();
  }
}

void log_group(const char *group, int messages)
{
  static char zak2[128];
  if (!found_file && !log_file)
    return;
  strcpycomma(zak, group);
  if (found_file)
    fprintf(found_file, "%s ", zak);
  if (log_file)
  {
    strcpyC(zak2, msgs_lookup("LogGroup"));
    log_log(zak2, zak, messages);
    log_newline();
  }
}

void log_endgroup()
{
  if (found_file)
    putc('\n', found_file);
}
