//
// Name: INTERSTELLAR CONSOLE -- Startup for GUI
// Author: Misato Observatory, Wakayama, Japan
// License: GPL (General Public License)
//

#include <process.h>
#include <stdio.h>
#include <unistd.h>
#include <limits.h>
#include "get_first_line.h"

#define WISH_ARGS "misato-gui.tcl"

int main( int argc, char *argv[] )
{
    int status, flag_use_tcl_path;
    char path[PATH_MAX] = {'\0'};
    char cmd[PATH_MAX] = {'\0'};

    flag_use_tcl_path = get_first_line( "tcl-path.txt", path, PATH_MAX );

    if ( flag_use_tcl_path != 0 ) {

      snprintf(cmd, PATH_MAX, "SET PATH=%s; && start wish.exe %s", path, WISH_ARGS);

    }
    else {

      /* This tests whether tclsh.exe exists in the current PATH */
      status = system("tclsh.exe system-proc.tcl 2>/nul");

      /* go! */
      if ( status != 0 ) {
        /* This assumes the default PATH set by BAWT Tcl/Tk */
        snprintf(cmd, PATH_MAX, "SET PATH=%s; && start wish.exe %s", "%PATH%;C:\\Tcl\\bin", WISH_ARGS);
        flag_use_tcl_path = 1;
      }
      else {
        snprintf(cmd, PATH_MAX, "start wish.exe %s", WISH_ARGS);
      }
    }

    if ( flag_use_tcl_path != 0 ) {
      printf("system(%c%s%c);\n", 34, cmd, 34);
    }
    status = system(cmd);

    return status;
}

