/*****************************************************************************\
*                                                                             *
*  Name   : splitter                                                          *
*  Author : Chris Koeritz                                                     *
*                                                                             *
*  Purpose:                                                                   *
*                                                                             *
*    Takes text as input and splits the lines so that they will fit on a      *
*  standard 80 column terminal.                                               *
*                                                                             *
*******************************************************************************
* Copyright (c) 1993-$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                               *
\*****************************************************************************/

#include <basis/istring.h>
#include <basis/set.cpp>
#include <opsystem/application_shell.h>
#include <opsystem/byte_filer.h>
#include <opsystem/command_line.h>
#include <loggers/console_logger.h>
#include <opsystem/filename.h>
#include <loggers/file_logger.h>
#include <data_struct/static_memory_gremlin.h>
#include <textual/string_manipulation.h>

#include <stdio.h>

const int MAX_BUFFER = 1024;

class splitter_app : public application_shell
{
public:
  splitter_app() : application_shell(static_class_name()) {}

  IMPLEMENT_CLASS_NAME("splitter_app");

  virtual int execute();

  int print_instructions();

private:
};

////////////////////////////////////////////////////////////////////////////

int splitter_app::print_instructions()
{
  istring name = filename(__argv[0]).basename().raw();
  log(isprintf("%s usage:", name.s()));
  log("");
  log(isprintf("\
This program splits long lines in input files into a more reasonable size.\n\
Any filenames on the command line are split and sent to standard output.\n\
The following options change how the splitting is performed:\n\
   --help or -?\tShow this help information.\n\
   --mincol N\tMinimum column to use for output.\n\
   --maxcol N\tMinimum column to use for output.\n\
"));
  return -3;
}

int splitter_app::execute()
{
  command_line cmds(__argc, __argv);  // parse the command line up.

  // retrieve any specific flags first.
  istring temp;
  int min_col = 0;
  if (cmds.get_value("mincol", temp))
    min_col = temp.convert(min_col);
  int max_col = 77;
  if (cmds.get_value("maxcol", temp))
    max_col = temp.convert(max_col);
  // look for help command.
  int junk_index = 0;
  if (cmds.find("help", junk_index, false)
      || cmds.find('h', junk_index, false)
      || cmds.find("?", junk_index, false)
      || cmds.find('?', junk_index, false) ) {
    print_instructions();
    return 0;
  }

  // gather extra input files.
  string_set input_files;
  for (int i = 0; i < cmds.entries(); i++) {
    const command_parameter &curr = cmds.get(i);
    if (curr.type() == command_parameter::VALUE) {
//log(istring("adding input file:") + curr.text());
      input_files += curr.text();
    }
  }

  istring accumulator;
  for (int q = 0; q < input_files.length(); q++) {
    byte_filer current(input_files[q], "r");
    if (!current.good()) continue;
    while (!current.eof()) {
      istring line_read;
      int num_chars = current.getline(line_read, MAX_BUFFER);
      if (!num_chars) continue;
//printf("line len=%d, cont=%s\n", line_read.length(), line_read.s());
      accumulator += line_read;
////      accumulator += '\n';
    }
  }

  // now get from standard input if there weren't any files specified.
  if (!input_files.length()) {
    char input_line[MAX_BUFFER + 2];
    while (!feof(stdin)) {
      char *got = fgets(input_line, MAX_BUFFER, stdin);
      if (!got) break;
//printf("line=%s\n", got);
      accumulator += got;
////      accumulator += '\n';
    }
  }
//printf("splitting accum with %d chars...\n", accumulator.length());
  istring chewed;
  string_manipulation::split_lines(accumulator, chewed, min_col, max_col);
//printf("chewed string now has %d chars...\n", chewed.length());
  printf(chewed.s());
  return 0;
}

////////////////////////////////////////////////////////////////////////////

HOOPLE_MAIN(splitter_app, )

#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/application_base.cpp>
  #include <opsystem/application_shell.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/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__

