/* timer.c */

/* Subroutines for timed events */
#include "os.h"
#include "copyright.h"
#include "config.h"
#include "db.h"
#include "interface.h"
#include "match.h"
#include "externs.h"
static int alarm_triggered = 0;
#ifdef WIN32
static time_t alarm_time = 0;
#endif

#ifndef WIN32
static void alarm_handler (int signo)
{
  alarm_triggered = 1;
  signal (SIGALRM, alarm_handler);
}
#endif

void init_timer (void)
{
#ifndef WIN32
  signal (SIGALRM, alarm_handler);
  signal (SIGHUP, alarm_handler);
  alarm (1);
#else
  time (&alarm_time);
  alarm_time += 1;
#endif
}

void dispatch (void)
{
/*  void do_garbage(); */

#ifdef WIN32
  if (time (0) > alarm_time)
    alarm_triggered = 1;
#endif
  /* this routine can be used to poll from interface.c */
  if (!alarm_triggered)
    return;
  alarm_triggered = 0;
  do_second ();
  /* Free list reconstruction */
#ifdef DESTROY
  {
    static int ticks = FIXUP_INTERVAL;
    if (!ticks--) {
      ticks = FIXUP_INTERVAL;
      strcpy (ccom, "dbck");
      do_dbck (GOD);
    }
  }
#endif
  /* Database dump routines */
  {
    static int ticks = DUMP_INTERVAL;

    if (!ticks--) {
      ticks = DUMP_INTERVAL;
      strcpy (ccom, "dump");
      fork_and_dump ();
    }
  }
#ifdef RWHO_SEND
  {
    static int ticks = RWHO_INTERVAL;
    if (!ticks--) {
      ticks = RWHO_INTERVAL;
      strcpy (ccom, "update_rwho");
      rwho_update ();
    }
  }
#endif
  /* reset alarm */
#ifndef WIN32
  alarm (1);
#else
  time (&alarm_time);
  alarm_time += 1;
#endif
}