/*
* Copyright (C) 2008-2009, CompHEP Collaboration
* Author: Alexander Sherstnev
* ---------------------------------------------------
*/
#include <unistd.h>

#include "service2/include/chep_limits.h"
#include "service2/include/tptcmac.h"
#include "service2/include/unix_utils.h"
#include "service2/include/files.h"
#include "service2/include/s_files.h"
#include "service2/include/lbl.h"
#include "service2/include/syst.h"
#include "chep_crt/include/chep_crt.h"

#include "symb/include/sos.h"
#include "symb/include/screen.h"
#include "symb/include/read_mdl.h"

#include "param.h"
#include "configurator.h"
#include "rw_batch.h"
#include "width_12.h"
#include "out_ext.h"
#include "rw_sess.h"

/*
static void 
readtext (char *fname)
{
  FILE *txt;

  trim (fname);
  txt = fopen (fname, "r");
  if (txt == NULL)
    {
      warnanykey (10, 10, " File not found ");
      return;
    }
  showtext (1, 1, 80, maxRow (), "", txt);
  fclose (txt);
}


static void 
viewresults_num (void)
{
  int i, k;
  int doserror;
  char f_name[STRSIZ];
  char menustr[2016];
  void *pscr2 = NULL;
  searchrec s;

  menustr[0] = 15;

  k = 1;
  strcpy (f_name, "*.*");
  doserror = find_first (f_name, &s, anyfile);
  while (doserror == 0 && k <= 2000)
    {
      for (i = 1; (i <= strlen (s.name)) && (i <= 15); i++)
        menustr[k++] = s.name[i - 1];
      for (i = strlen (s.name) + 1; i <= 15; i++)
        menustr[k++] = ' ';
      doserror = find_next (&s);
    }
  menustr[k] = 0;
  if (0 == menustr[1])
    {
      messanykey (10, 15, "directory  is empty");
      return;
    }

  k = 1;
  do
    {
      menu1 (10, 10, "", menustr, "", &pscr2, &k);
      if (k > 0)
        {
          sprintf (f_name, "%.15s", menustr + k * 15 - 14);
          readtext (f_name);
        }
    }
  while (k != 0);
}
*/

int 
main (int argc, char **argv) {
  int n;
  int exitlevel;
  char * p;
  char ExitMessage[] = "Quit session?";
  midstr _pathtocomphep;
  FILE * LOCK;
  FILE * fverion;

  for (n = 1; n < argc; n++) {
    if (strcmp (argv[n], "-blind") == 0) {
      blind = 1;
      inkeyString = argv[n + 1];
    }
  }

  p = getenv ("TEST_COMPHEP");
  if (!p) {
    LOCK = fopen ("LOCK", "a");
    if (ftell (LOCK) == 0) {
      fprintf (LOCK, "CompHEP Configurator has been started, process number %i\n", getpid ());
      fflush (LOCK);
      fclose (LOCK);
    } else {
      fprintf (stderr, "\nError: n_comphep program was already launched and not finished normaly.\n");
      fprintf (stderr, "         Remove the results/LOCK file to start the numerical session anyway");
/*      exit (100);*/
    }
  }

  p = getenv ("COMPHEP");
  if (!p) {
    fprintf (stderr, " Environment variable COMPHEP is not defined.\n");
    exit (-1);
  }
  strcpy (_pathtocomphep, p);
  sprintf (pathtouser, "%s%c", defaultPath, d_slash);
  sprintf (pathtocomphep, "%s%c", _pathtocomphep, d_slash);
  sprintf (pathtohelp, "%shelp%c", pathtocomphep, f_slash);

#ifdef LHAPDF
  {
    midstr _pathtolhapdf;
    p = getenv ("LHAPDFPATH");
    if (!p) {
      fprintf (stderr, " Environment variable LHAPDFPATH is not defined.\n");
      exit (-2);
    }
    strcpy (_pathtolhapdf, p);
    sprintf (pathtolhapdf, "%s%c", _pathtolhapdf, d_slash);
  }
#endif

/*  initialization of the session */
  r_batch_session (NULL);

  {
    shortstr inifile;
    shortstr theversion;
    midstr pathtoversionfile;

    sprintf (pathtoversionfile, "%sversion", pathtocomphep);
    fverion = fopen(pathtoversionfile, "r");
    if (fverion != NULL) {
      fscanf (fverion, "%s", theversion);
    } else {
      strcpy (theversion, "unknown");
    }
    setversion (theversion);

    strcpy (inifile, "comphep.ini;../comphep.ini");
    strcpy (theversion, getname ());
    start1 (theversion, scat ("%s%s", pathtocomphep, "icon"), inifile);
  }

  clr_scr (FGmain, BGmain);
    {
      shortstr procname;
      goto_xy (4, 2);
      clr_eol ();
      scrcolor (Red, BGmain);
      print ("Process: ");
      scrcolor (Black, BGmain);
      get_process_name_safe (procname);
      print (procname);
/*      scrcolor (Red, BGmain);
      print (" (%d subprocesses)", nprc_);*/
    }
/*
  if (calcFunc ())
    {
      warnanykey (15, 15, "Wrong parameters.\n Can not evaluate constraints");
      change_parameter (54, 7);
    }
*/
  do
    {
      configurator_menu ();
    }
  while (!mess_y_n (15, 15, ExitMessage));

  w_batch_session (NULL);

  finish ("End of CompHEP Configurator.");
  return 0;
}
