/* Program to produce output at regular intervals.  */

#define NOT_C_CODE /* for VMS */
#include <config.h>

#include <stdio.h>
#include <stdlib.h>		/* ttn */
#include <sys/types.h>

#ifdef TIME_WITH_SYS_TIME
#include <sys/time.h>
#include <time.h>
#else
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#else
#include <time.h>
#endif
#endif

#ifdef VMS
#include <descrip.h>
#include <ssdef.h>
#include <lnmdef.h>
#include <maildef.h>
#include "mail.h"
#include <lib$routines.h>	/* ttn */
#endif

struct tm *localtime ();

main (argc, argv)
     int argc;
     char **argv;
{
  int period = 60;
  time_t when;

  struct tm *tp;
#ifdef VMS
  unsigned int status;
  unsigned int userctx; /* unsigned long userctx; */ /* ttn */
  unsigned short newcount;
  struct itm$list3 out_item_list[] = {
    { sizeof(newcount), MAIL$_USER_NEW_MESSAGES, &newcount, 0 },
    { 0, 0, 0, 0}
  };
  $DESCRIPTOR(dsc_tabnam, "LNM$JOB");
  $DESCRIPTOR(dsc_lnm, "EMACS_WAKEUP_MAIL");
  struct itm$list3 dsc_lnm_value[] = {
    { 3, LNM$_STRING, "YES", 0},
    { 0, 0, 0, 0}
  },
  nullitm = { 0, 0, 0, 0 }; /* nullitm[] = { { 0, 0, 0, 0 } }; */ /* ttn */
#endif

  if (argc > 1)
    period = atoi (argv[1]);

  while (1)
    {
#ifdef VMS
      userctx = 0;
      status = mail$user_begin(&userctx, &nullitm, out_item_list);
      if (!(status & 1))
	lib$signal (status);
      status = mail$user_end(&userctx, &nullitm, &nullitm);
      if (!(status & 1))
	lib$signal (status);
      if (newcount)
	status = sys$crelnm (0, &dsc_tabnam, &dsc_lnm, 0, dsc_lnm_value);
      else
	status = sys$dellnm (&dsc_tabnam, &dsc_lnm, 0);
      if (!(status & 1) && status != SS$_NOLOGNAM)
	lib$signal (status);
      printf ("mailcount = %d, ", newcount);
#endif
      /* Make sure wakeup stops when Emacs goes away.  */
      if (getppid () == 1)
	exit (EXIT_SUCCESS);
      printf ("Wake up!\n");
      fflush (stdout);
      /* If using a period of 60, produce the output when the minute
	 changes. */
      if (period == 60)
	{
	  time (&when);
	  tp = localtime (&when);
	  sleep (60 - tp->tm_sec);
	}
      else
	sleep (period);
    }
}

/* wakeup.c ends here */
