#include "main.h"
#include "command.h"
#include <regex.h>
extern "C"{
// #include <malloc.h>  /** used by print_mem_usage - some systems lack it **/
}
#pragma implementation

Object_Store* db;
Mud_Sockets all_sockets;
Scheduler   all_processes;

/**
*** these are two of the five methods the server assumes are present:
*** #0 must have a method called "connect" & one called "boot"
*** all objects that can be connected to must have a "parse"
*** as well as "tell" and  "quit" -- used in Process.C
**/
static Value connect_name (new String ("connect"), SYM);
static Value parse_name   (new String ("parse"), SYM);
static Value boot_name    (new String ("boot"), SYM);

const int seconds_between_saves = 180;


void try_parse ();


main(int argc, char* argv[]){
  int i, portnum;
  time_t last_save, temp_time;
  struct One_Line* input;
  Value* input_val;

#ifdef LINUX
  re_syntax_options = RE_NO_BK_PARENS | RE_NO_BK_VBAR | RE_CHAR_CLASSES;
#else
  obscure_syntax = RE_NO_BK_PARENS | RE_NO_BK_VBAR | RE_CHAR_CLASSES;  
#endif
  initialize_command_constants();
  if (argc < 2)
    {cout << "Syntax:  main <filename> [<port>]  -- do not add suffix (.txt or .pag)\n";
     exit (1);}
  db = new Object_Store (argv[1]);

  last_save = time ((time_t*)0);

  all_processes.add_process (0 , &boot_name , NULL);
  if ((argc == 3) &&
      (portnum = atoi (argv[2])) &&
      (all_sockets.add_connection(portnum)))
    all_processes.add_process (0 , &connect_name , NULL);
  
  while (1)
    {if ((i = all_sockets.add_connection()) >= 0)
       {all_processes.add_process (0 , &connect_name , NULL);}
     all_processes.one_cycle();
     while (input = all_sockets.receive())
       {input_val = new Value (input->instring);
	all_processes.add_process (input->object_num , &parse_name ,
				   new Val_List (input_val));}
     if (last_save + seconds_between_saves < (temp_time = time ((time_t*)0)))
       {last_save = temp_time;
	db->update();}}

  delete db;
}



void try_parse (){
  CommandList cl;
  Value* tempval;
  Val_List* templist;
  String tempstring;
  char aline [5000];
  templist = new Val_List (new Value (new String ("@dig")),
	     new Val_List (new Value (new String ("NUMBER"), SYM)));
  cl.add (templist, new String ("DIG-MTHD"));
  templist = new Val_List (new Value (new String ("@desc")),
              new Val_List (new Value (new Val_List (new Value (OR, RESERVED),
				 new Val_List (new Value (new String ("ribe")),
                                 new Val_List (new Value (new String))))),
                 new Val_List (new Value (new String ("this")))));
  cl.add (templist, new String ("DESC-MTHD"));
  templist = new Val_List (new Value (new String ("@rename")),
           new Val_List (new Value (new Val_List (new Value (BEFORE, RESERVED),
			      new Val_List (new Value (new String ("to"))))),
			 new Val_List (new Value (new String ("to")),
		       new Val_List (new Value (new String ("REST"), SYM)))));
  cl.add (templist, new String ("RENAME-MTHD"));
  cin.getline(aline, 5000);
  tempstring = aline;
  tempval = new Value (cl.lookup_all (&tempstring));
  tempval->print_val();  cout << "\n";
  tempval->release();
  tempval = cl.list_cmds();
  tempval->print_val();  cout << "\n";
  tempval->release();
}




// since this is just debugging code and some systems don\'t have a good
// malloc.h, i\'m commenting it out by default.
//
//long print_mem_usage (){
//  static long ord_blocks=0, uord_blocks=0;
//  long dob, duob;
//  struct mallinfo memstruct;
//  memstruct = mallinfo();
//  dob = memstruct.ordblks - ord_blocks;
//  duob = memstruct.uordblks - uord_blocks;
//  cout <<"Memory: "<<memstruct.arena<<" "<<memstruct.ordblks<<" ("
//       <<dob<<") "<<memstruct.uordblks<<" ("<<duob<<") ";
//  ord_blocks = memstruct.ordblks;
//  uord_blocks = memstruct.uordblks;
//  return duob;
//}