//
// Name: INTERSTELLAR CONSOLE -- Startup for Dome mode
// 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 TCL_ARGS "exec_isc.tcl -dome"

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; && tclsh.exe %s", path, TCL_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; && tclsh.exe %s", "%PATH%;C:\\Tcl\\bin", TCL_ARGS);
        flag_use_tcl_path = 1;
      }
      else {
        snprintf(cmd, PATH_MAX, "tclsh.exe %s", TCL_ARGS);
      }

    }

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

    return status;
}

