/*****************************************************************************\
*                                                                             *
*  Name   : dirtree                                                           *
*  Author : Chris Koeritz                                                     *
*                                                                             *
*  Purpose:                                                                   *
*                                                                             *
*    A utility that shows the directory tree specified on the command line.   *
*                                                                             *
*******************************************************************************
* 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/guards.h>
#include <basis/string_array.h>
#include <opsystem/application_shell.h>
#include <loggers/console_logger.h>
#include <opsystem/directory_tree.h>
#include <opsystem/filename.h>
#include <data_struct/static_memory_gremlin.h>
#include <textual/string_manipulation.h>

class dirtree : public application_shell
{
public:
  dirtree() : application_shell(static_class_name()) {}
  IMPLEMENT_CLASS_NAME("dirtree");
  int execute();
  int print_instructions_and_exit() {
    log(isprintf("\
%s: This utility requires a directory name on the command line.\n\
The subdirectories under that directory will be shown.  If a second paramter\n\
is provided, it is taken as a pattern that will be used to show the files in\n\
those directories also.  Otherwise, just the tree of directories is shown.\n\
", filename(__argv[0]).basename().raw().s()));
    return 23;
  }
};

istring hier_prefix(int depth, int kids)
{
  istring indent = string_manipulation::indentation( (depth - 1) * 2);
  if (!depth) return "";
  else if (!kids) return indent + "|--";
  else return indent + "+--";
}

int dirtree::execute()
{
  istring path;
  if (__argc < 2) {
    return print_instructions_and_exit();
  }

  path = __argv[1];

  // check if we should show any of the files.
  bool show_files = false;
  istring pattern;
  if (__argc >= 3)
    pattern = __argv[2];
  if (pattern.t()) {
    show_files = true;
  }

//  log(istring("Scanning directory tree at \"") + path + "\"");
//  log(istring("Using pattern-match \"") + pattern + "\"");

  directory_tree dir(path, pattern.s(), !show_files);
  if (!dir.good()) {
    continuable_error(class_name(), "tree construction",
        "the directory could not be read");
    return 82;
  }

  dir_tree_iterator *ted = dir.start(directory_tree::prefix);
    // create our iterator to traverse the tree in prefix order.

  filename curr;  // the current path the iterator is at.
  string_array files;  // the filenames held at the iterator.
  int depth;  // current depth in tree.
  int kids;  // number of children below this node.

  while (directory_tree::current(*ted, curr, files)) {
    // we have a good directory to show.
    directory_tree::depth(*ted, depth);
    directory_tree::children(*ted, kids); 
    istring name_to_log = curr.basename().raw();
    if (!depth)
      name_to_log = curr.raw();
    log(hier_prefix(depth, kids) + name_to_log);
    if (show_files) {
      istring names;
      for (int i = 0; i < files.length(); i++) names += files[i] + " ";
      if (names.length()) {
        istring split;
        string_manipulation::split_lines(names, split, depth * 2 + 2);
        log(split);
      }
    }

    // go to the next place.
    directory_tree::next(*ted);
  }

  directory_tree::throw_out(ted);
  return 0;
}

HOOPLE_MAIN(dirtree, )

#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 <nodes/node.cpp>
  #include <nodes/packable_tree.cpp>
  #include <nodes/path.cpp>
  #include <nodes/tree.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/directory_tree.cpp>
  #include <opsystem/file_info.cpp>
  #include <opsystem/filename.cpp>
  #include <opsystem/filename_list.cpp>
  #include <opsystem/filename_tree.cpp>
  #include <opsystem/huge_file.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__

