/* main.c */

#include "config.h"
#include "object.h"
#include "interp.h"
#include "interface.h"
#include "cache.h"
#include "construct.h"
#include "clearq.h"
#include "globals.h"
#include "dbhandle.h"

int main(int argc, char *argv[]) {
  int count,do_single,do_create,port;
  char *loadpath,*savepath,*panicpath,*arg;
  signed long loop;
  int retval;
  struct object *obj;
  struct var tmp;
  struct var_stack *rts;
  struct fns *func;

  mudlib_path=NULL;
  syslog_path=NULL;
  cache_path=NULL;
  tmpdb_path=NULL;
  count=1;
  port=PORT;
  do_create=0;
  do_single=DEFAULT_INTERFACE;
  loadpath=NULL;
  savepath=NULL;
  panicpath=NULL;
  noisy=0;
  while (count<argc) {
    arg=argv[count];
    if (!strcmp(arg,"-noisy"))
      noisy=1;
    else if (!strncmp(arg,"-load=",6))
      loadpath=&(arg[6]);
    else if (!strncmp(arg,"-save=",6))
      savepath=&(arg[6]);
    else if (!strncmp(arg,"-panic=",7))
      panicpath=&(arg[7]);
    else if (!strncmp(arg,"-mudlib=",8))
      mudlib_path=&(arg[8]);
    else if (!strncmp(arg,"-syslog=",8))
      syslog_path=&(arg[8]);
    else if (!strncmp(arg,"-cache=",7))
      cache_path=&(arg[7]);
    else if (!strncmp(arg,"-tmpdb=",7))
      tmpdb_path=&(arg[7]);
    else if (!strcmp(arg,"-create"))
      do_create=1;
    else if (!strcmp(arg,"-single"))
      do_single=SINGLE;
    else if (!strcmp(arg,"-multi"))
      do_single=MULTI;
    else if (!strncmp(arg,"-port=",6))
      port=atoi(&(arg[6]));
    else {
      fprintf(stderr,"usage: %s [-load=filename] [-save=filename] [-panic="
              "filename]\n             [-mudlib=directory] [-syslog=filename] "
              "[-cache=filename]\n             [-tmpdb=filename] [-create] "
              "[-multi] [-single]\n             [-port=number] [-noisy]\n",
              argv[0]);
      exit(0);
    }
    count++;
  }
  if (!mudlib_path) mudlib_path=MUDLIB_PATH;
  if (!syslog_path) syslog_path=SYSLOG_PATH;
  if (!cache_path) cache_path=CACHE_PATH;
  if (!tmpdb_path) tmpdb_path=TMPDB_PATH;
  log_sysmsg(" system: starting up");
  init_globals(loadpath,savepath,panicpath);
  if (retval=init_interface(port,do_single)) {
    if (retval==NOSINGLE)
      log_sysmsg(" system: single-user mode not supported");
    else if (retval==NOMULTI)
      log_sysmsg(" system: multi-user mode not supported");
    else if (retval==PORTINUSE)
      log_sysmsg(" system: port in use");
    else if (retval==NOSOCKET)
      log_sysmsg(" system: couldn't create socket");
    else
      log_sysmsg(" system: unspecified interface initialization error");
    exit(0);
  }
  if (do_create) {
    if (create_db()) {
      log_sysmsg(" system: database creation failed");
      shutdown_interface();
      exit(0);
    }
  } else {
    if (init_db()) {
      log_sysmsg(" system: database initialization failed");
      shutdown_interface();
      exit(0);
    }
  }
  log_sysmsg(" system: startup complete");
  loop=0;
  now_time=time2int(time(NULL));
  tmp.type=NUM_ARGS;
  tmp.value.num=0;
  while (loop<db_top) {
    obj=ref_to_obj(loop);
    if (obj) {
      if (obj->flags & CONNECTED) {
        obj->flags&=~CONNECTED;
        rts=NULL;
        func=find_fns("disconnect",obj);

#ifdef CYCLE_HARD_MAX
        hard_cycles=0;
#endif /* CYCLE_HARD_MAX */

#ifdef CYCLE_SOFT_MAX
        soft_cycles=0;
#endif /* CYCLE_SOFT_MAX */

        if (func) {
          push(&tmp,&rts);
          interp(NULL,obj,NULL,&rts,func);
          free_stack(&rts);
        }
        handle_destruct();
      }
    }
    handle_destruct();
    loop++;
  }
  handle_input();
  log_sysmsg(" system: return from handle_input()");
  shutdown_interface();
  exit(0);
}