/* Copyright (C) 1997, Alexander Pukhov 
 * Copyright (C) 2001-2003, CompHEP Collaboration
 *---------------------------------------------------
 *$Id: n_comphep.c,v 1.6 2003/11/08 19:46:05 kryukov Exp $
 *
 *$log$
*/
#include <unistd.h>

#include "chep_crt/include/chep_crt.h"
#include "service2/include/tptcmac.h"
#include "service2/include/os.h"
#include "service2/include/files.h"

#include "rw_sess.h"
#include "out_ext.h"
#include "n_comphep_.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 (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);
}


static void 
f6_key_prog (int x)
{
  viewresults ();
}

static void 
f9_key_prog (int x)
{
  if (mess_y_n (15, 15, " Quit session? "))
    {
      w_sess__ (NULL);
      finish ("End of CompHEP numerical session.");
      exit (0);
    }
}


#ifdef _WIN32
int 
main_n (int argc, char **argv)
#else
int 
main (int argc, char **argv)
#endif
{
  int n;
  FILE *LOCK;
  interpret = 0;

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

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

  LOCK = fopen ("LOCK", "a+");
  if (ftell (LOCK) == 0)
    {
      fprintf (LOCK, "n_comphep is started here, 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);
    }

  f3_key[3] = f6_key_prog;
  f3_mess[3] = "Results";
  f3_key[6] = f9_key_prog;
  f3_mess[6] = "Quit";

/*  initialization of the session */
  wrtprc_ ();
  inipar_ ();
  r_sess__ (NULL);

  {
    char inifile[100] = "comphep.ini;../comphep.ini";
    start1 (version, scat ("%s%s", pathtocomphep, "icon"), inifile);
  }

  n_comphep ();
  finish ("End of CompHEP numerical session.");
  return 0;
}
