/*****************************************************************************\
*                                                                             *
*  Name   : winvisilaunch                                                     *
*  Author : Chris Koeritz                                                     *
*                                                                             *
*  Purpose:                                                                   *
*                                                                             *
*    Runs a command but also creates a hidden window that installshield can   *
*  latch onto and wait for.                                                   *
*                                                                             *
*******************************************************************************
* Copyright (c) 2006-$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/byte_array.h>
#include <basis/istring.h>
#include <data_struct/static_memory_gremlin.h>
#include <loggers/console_logger.h>
#include <opsystem/application_shell.h>
#include <opsystem/ini_config.h>
#include <win_ext/window_classist.h>

#include <stdio.h>
#include <stdlib.h>

#ifdef __WIN32__
  #include <shlobj.h>
#endif

#undef LOG
#define LOG(to_print) STAMPED_EMERGENCY_LOG(program_wide_logger(), to_print)

class winvisilaunch : public application_shell
{
public:
  winvisilaunch();

  IMPLEMENT_CLASS_NAME("winvisilaunch");

  int execute();
    // performs main body of test.
};

winvisilaunch::winvisilaunch()
: application_shell("winvisilaunch")
{}

int winvisilaunch::execute()
{
  if (__argc < 2) {
    printf("This program takes another program's name (and optional\n");
    printf("parameters) which it will run without showing a window.\n");
    return 1;
  }

  ini_configurator results_file(portable::env_string("TEMP") + "/result.ini",
      ini_configurator::RETURN_ONLY);
  // set a default result code to start with.
  results_file.store("result", "exitcode", "128");

  window_handle window = create_simplistic_window("tmp_invisil",
      "tmp_window_winvisil");

  istring app_name = __argv[1];
  istring cmdline;
//LOG(isprintf("arg 1 is %s", __argv[1]));
  for (int i = 2; i < __argc; i++) {
    char *arg = __argv[i];
    bool spaced = non_negative(istring(arg).find(' '));
    if (spaced) cmdline += "\"";
    cmdline += arg;
//LOG(isprintf("arg %d is %s", i, __argv[i]));
    if (spaced) cmdline += "\"";
    cmdline += " ";
  }
  LOG("");
  LOG(isprintf("appname: %s", app_name.s()));
  LOG(isprintf("cmdline: %s", cmdline.s()));
  u_int kid_id;

#ifdef ADMIN_CHECK
  #ifdef __WIN32__
    if (IsUserAnAdmin()) {
      ::MessageBox(0, to_unicode_temp(istring("IS admin with ") + app_name + " " + cmdline), to_unicode_temp("winvisilaunch"), MB_OK);
    } else {
      ::MessageBox(0, to_unicode_temp(istring("NOT admin for ") + app_name + " " + cmdline), to_unicode_temp("winvisilaunch"), MB_OK);
    }
  #endif
#endif

  u_int to_return = portable::launch_process(app_name, cmdline,
      portable::AWAIT_APP_EXIT | portable::HIDE_APP_WINDOW, kid_id);
  if (to_return) {
    results_file.store("result", "exitcode", isprintf("%d", to_return));
    LOG(isprintf("failure: exited with %d", to_return));
  } else {
    results_file.store("result", "exitcode", "0");
  }

  whack_simplistic_window(window);

  exit(to_return);
}

HOOPLE_MAIN(winvisilaunch, )

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

