/*****************************************************************************\
*                                                                             *
*  Name   : CGI nechung                                                       *
*  Author : Chris Koeritz                                                     *
*                                                                             *
*******************************************************************************
* Copyright (c) 1997-$now By Author.  This program is free software; you can  *
* redistribute it and/or modify it under the terms of the GNU General Public  *
* License as published by the Free Software Foundation; either version 2 of   *
* the License or (at your option) any later version.  This is online at:      *
*     http://www.fsf.org/copyleft/gpl.html                                    *
* Please send any updates to: fred@gruntose.com                               *
\*****************************************************************************/

//! @file cgi_nechung.cpp Spits out a CGI appropriate chunk of text with a fortune in it.

#include "nechung_oracle.h"

#include <basis/guards.h>
#include <basis/istring.h>
#include <loggers/console_logger.h>
#include <opsystem/filename.h>
#include <data_struct/static_memory_gremlin.h>

#include <stdio.h>

#undef LOG
#define LOG(s) program_wide_logger().log(s)

HOOPLE_STARTUP_CODE;

#define DEFAULT_FORTUNE_FILE "fortunes.dat"

int main(int argc, char *argv[])
{
  SET_DEFAULT_CONSOLE_LOGGER;
  istring name;
  istring index;
  if (argc > 1) {
    // use the first command line argument.
    name = argv[1];
  } else {
    // if nothing on the command line, then use our defaults.
    name = portable::env_string("NECHUNG");
      // first try the environment variable.
    if (!name) name = DEFAULT_FORTUNE_FILE;
      // next, use the hardwired default.
  }

  if (name.length() < 5) {
    LOG(istring("nechung:: file name is too short (") + name + ").");
    return 1;
  }
  filename index_file_name(name);
  istring extension(index_file_name.extension());
  int end = index_file_name.raw().end();
#ifdef DEBUG_NECHUNG
  LOG(istring("fortune filename is ") + name);
  LOG(istring("extension is ") + extension);
#endif
  istring tmp = index_file_name;
  tmp.zap( (end + 1) - extension.length(), end);
  tmp += "idx";
  istring base_part = filename(tmp).basename();
  index_file_name = portable::env_string("TMP") + "/" + base_part;
#ifdef DEBUG_NECHUNG
  LOG(istring("index file is ") + index_file_name);
#endif
  index = index_file_name;

  nechung_oracle some_fortunes(name, index);
  // send the header for html text.
  printf("content-type: text/html\n\n");
  // send the preliminary gunk.
  printf("<body text=\"#00ee00\" bgcolor=\"#000000\" link=\"#66ff99\" "
      "vlink=\"#cc33cc\" alink=\"#ff9900\">\n");
//old text color #33ccff
  printf("<tt style=\"font-weight: bold;\">\n");
  printf("<font size=\"+1\">\n");

  istring to_show = some_fortunes.pick_random();
  int line_posn = 0;
  for (int i = 0; i < to_show.length(); i++) {
    if (to_show[i] == ' ') {
      // spaces get translated to one non-breaking space.
      printf("&nbsp;");
      line_posn++;
    } else if (to_show[i] == '\t') {
      // tabs get translated to positioning at tab stops based on eight.
      int to_add = 8 - line_posn % 8;
      for (int j = 0; j < to_add; j++) printf("&nbsp;");
      line_posn += to_add;
    } else if (to_show[i] == '\r')
      continue;
    else if (to_show[i] == '\n') {
      printf("<br>%c", to_show[i]);
      line_posn = 0;
    } else {
      printf("%c", to_show[i]);
      line_posn++;
    }
  }
  printf("\n");
  printf("</font>\n");
  printf("</tt>\n");
  printf("</body>\n");
  return 0;
}

#ifdef __BUILD_STATIC_APPLICATION__
  // static dependencies found by buildor_gen_deps.sh:
  #include <basis/array.cpp>
  #include <basis/byte_array.cpp>
  #include <basis/callstack_tracker.cpp>
  #include <basis/chaos.cpp>
  #include <basis/convert_utf.cpp>
  #include <basis/definitions.cpp>
  #include <basis/earth_time.cpp>
  #include <basis/guards.cpp>
  #include <basis/istring.cpp>
  #include <basis/log_base.cpp>
  #include <basis/memory_checker.cpp>
  #include <basis/mutex.cpp>
  #include <basis/object_base.cpp>
  #include <basis/outcome.cpp>
  #include <basis/packable.cpp>
  #include <basis/portable.cpp>
  #include <basis/sequence.cpp>
  #include <basis/set.cpp>
  #include <basis/utility.cpp>
  #include <basis/version_record.cpp>
  #include <data_struct/amorph.cpp>
  #include <data_struct/bit_vector.cpp>
  #include <data_struct/byte_hasher.cpp>
  #include <data_struct/configurator.cpp>
  #include <data_struct/hash_table.cpp>
  #include <data_struct/pointer_hash.cpp>
  #include <data_struct/stack.cpp>
  #include <data_struct/static_memory_gremlin.cpp>
  #include <data_struct/string_hash.cpp>
  #include <data_struct/string_hasher.cpp>
  #include <data_struct/string_table.cpp>
  #include <data_struct/symbol_table.cpp>
  #include <data_struct/table_configurator.cpp>
  #include <loggers/console_logger.cpp>
  #include <loggers/file_logger.cpp>
  #include <loggers/locked_logger.cpp>
  #include <loggers/null_logger.cpp>
  #include <loggers/program_wide_logger.cpp>
  #include <opsystem/byte_filer.cpp>
  #include <opsystem/command_line.cpp>
  #include <opsystem/critical_events.cpp>
  #include <opsystem/directory.cpp>
  #include <opsystem/filename.cpp>
  #include <opsystem/filetime.cpp>
  #include <opsystem/ini_config.cpp>
  #include <opsystem/ini_parser.cpp>
  #include <opsystem/path_configuration.cpp>
  #include <opsystem/rendezvous.cpp>
  #include <textual/byte_format.cpp>
  #include <textual/parser_bits.cpp>
  #include <textual/string_manipulation.cpp>
  #include <textual/tokenizer.cpp>
#endif // __BUILD_STATIC_APPLICATION__

