/*****************************************************************************\
*                                                                             *
*  Name   : show_versions                                                     *
*  Author : Chris Koeritz                                                     *
*                                                                             *
*  Purpose:                                                                   *
*                                                                             *
*    Accepts a directory location on the command line and shows the versions  *
*  of all executables and dynamic libraries in that directory.                *
*                                                                             *
*******************************************************************************
* Copyright (c) 2001-$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/string_array.h>
#include <basis/version_checker.h>
#include <basis/version_record.h>
#include <loggers/console_logger.h>
#include <opsystem/directory.h>
#include <opsystem/filename.h>
#include <data_struct/static_memory_gremlin.h>

HOOPLE_STARTUP_CODE;

console_logger out;

int instruct(const char *program_name)
{
  out.log(filename(program_name).rootname() + " usage:\n\
The first parameter is taken as a directory to show versions in.\n\
That directory must exist for the utility to work properly.\n\
If there is a second parameter, it is taken as a version that\n\
is expected for all files in the directory.  Any that are showing\n\
the wrong version are reported.");
  return 1;
}

int main(int argc, char *argv[])
{
#ifdef __WIN32__
  if (argc < 2) return instruct(argv[0]);
  directory the_files(argv[1]);
  if (!the_files.good()) return instruct(argv[0]);
  version compare_with;
  if (argc >= 3)
    compare_with = version::from_text(argv[2]);
  if (!compare_with.bogus()) {
    out.log("Bad Version Report");
    out.log("==================");
  }
  bool any_bad = false;
  for (int i = 0; i < the_files.files().length(); i++) {
    const istring &curr = the_files.files()[i];
    int len = curr.length();
    istring end = curr.substring(len - 3, len - 1).lower();
    if ( (end == "dll") || (end == "exe") ) {
      filename full(argv[1], curr);
      version found = version_checker::get_version(full);
      if (compare_with.bogus()) {
        // just print out all the versions.
        out.log(full.raw() + " -- " + found.text_form());
      } else {
        // only flag versions that are erroneous.
        if (compare_with != found) {
          out.log(full.raw() + " has version " + found.text_form() + "!");
          any_bad = true;
        }
      }
    }
  }
  if (!compare_with.bogus() && !any_bad) {
    out.log("    zero bad versions detected.");
  }
#else
  char *arg2 = argv[argc-1];
  if (arg2) {}
#endif
  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_checker.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__

