/*****************************************************************************\
*                                                                             *
*  Name   : replace_text                                                      *
*  Author : Chris Koeritz                                                     *
*                                                                             *
*******************************************************************************
* Copyright (c) 2004-$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/function.h>
#include <basis/istring.h>
#include <opsystem/byte_filer.h>
#include <loggers/console_logger.h>
#include <data_struct/static_memory_gremlin.h>
#include <textual/parser_bits.h>

HOOPLE_STARTUP_CODE;

console_logger out;

int main(int argc, char *argv[])
{
  if (argc < 4) {
    out.log("This program takes three arguments.  The first is a file name that will be");
    out.log("edited.  The edit will consist of taking any occurrences of the second");
    out.log("argument and replacing each one with the third argument.");
    out.log("For example:");
    out.log("  replace_text c:/turnips.txt turnip rutabega");
    out.log("This will replace all occurrences of the word 'turnip' with the word");
    out.log("'rutabega' in a file named 'c:/turnips.txt'.");
    out.log("There is also a special trick for replacing with the quote character (\").");
    out.log("Use the keyword @@QUOTE@@ and that will become the quote character.");
    return 3;
  }

  istring replace_filename(argv[1]);

  byte_filer edit_file(replace_filename, "rb");
  if (!edit_file.good()) {
    out.log(istring("Could not find the logfile called ") + replace_filename);
    return 4;
  }
  int file_len = int(edit_file.length());
  istring total_file;
  edit_file.read(total_file, file_len);
  edit_file.close();

  const istring to_replace = argv[2];
  istring replacement_chunk = argv[3];
  if (replacement_chunk.contains("@@QUOTE@@")) {
    const istring quote = "\"";
    while (replacement_chunk.replace("@@QUOTE@@", quote));
      // loop until all occurrences get replaced.
  }

  int last_indy = 0;

  bool found_anything = false;
  while (true) {
    // look for the item that needs to be whacked.
    int indy = total_file.find(to_replace, last_indy);
    last_indy = indy;  // update our position.
    if (negative(indy)) break;  // not found.
    found_anything = true;
    // now whack the thing to be zapped.
    total_file.zap(indy, indy + to_replace.length() - 1);
    // now insert the replacement bit.
    total_file.insert(indy, replacement_chunk);
    last_indy = indy + replacement_chunk.length();
      // add to the starting index to avoid replacing inside the replacement
      // text that we're plugging in.  otherwise, we could have infinite
      // looping.
  }

  if (found_anything) {
    byte_filer new_file(replace_filename, "wb");
    if (!new_file.good()) {
      out.log(istring("Could not re-open the file called ") + replace_filename);
      return 4;
    }
    new_file.write(total_file);
    new_file.close();
  }

  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/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__

