#ifdef __arm
#define FAKE_UNIX 1
#define NO_ALARM 1
#endif
/* 2 general comments:
      a) Don't use timer events to display; use #include <time.h> functions,
         and poll them periodically. (Once every N iterations through the
         main loop for instance) 
      b) If you must use register, use it more selectively.  In fact, for
         all the compilers I know, your choice of register variables actually
         slows it down.  Trust the compiler. The compiler is your friend.
 */


#ifdef DEBUG
#include <curses.h>    /* Not portable */
#else
#include <signal.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#ifdef FAKE_UNIX
#include <time.h>
#define srandom(i) srand((unsigned int)(i))
#else
#include <sys/time.h>
#ifndef time_t
#define time_t long
#endif
#endif
#include "grid.h"
#include "word.h"
#include "fill.h"
#include "misc.h"

void intr(int ignored)
{
  int i;
  time_t	clock;
  static char last_clock[32] = { '\0' }, this_clock[32];

  clock=time(0);
  strcpy(this_clock, (char *)ctime(&clock));
  if (ignored == 1) {
    if (strcmp(last_clock, this_clock) == 0) return;
  }
  strcpy(last_clock, this_clock);
  putc('\n', stdout);
  puts(this_clock);
  for(i=0; i<=G_HEIGHT+1; i++)
  puts(grid[i]);
  putc('\n', stdout);
  fflush(stdout);
}

int main(int ac, char **av)
{
  int i;
#ifdef DEBUG
  int j;
  unsigned long	ul;
  char c;
#else
#ifndef NO_ALARM
  struct itimerval value, ovalue; /* Timer alarms not portable ANSI */
#endif
#endif

  if(ac!=2)
  {
    fprintf(stderr, "Usage:  1across grid-file\n");
    exit(1);
  }

  srandom(time(0));
  initGrid(av[1]);
  initWords();

#ifdef DEBUG
  initscr();
  for(i=0; i<=G_HEIGHT+1; i++)
  {
    for(j=0; j<=G_WIDTH+1; j++)
    {
      if(grid[i][j]==BLOCK)
      {
	standout();
	mvaddch(i, j, grid[i][j]);
	standend();
      }
      else
	mvaddch(i, j, grid[i][j]);
    }
  }
  refresh();
#else
  /*signal(SIGINT, intr);*/
#ifndef NO_ALARM
  /*signal(SIGALRM, intr);*/
  value.it_value.tv_sec=1200;
  value.it_value.tv_usec=0;
  value.it_interval=value.it_value;
  /*setitimer(ITIMER_REAL, &value, &ovalue);*/
#endif
  intr(0);
#endif

  i=fill(0);
#ifdef DEBUG
  move(G_HEIGHT+2, 0);
  refresh();
#else
#ifndef NO_ALARM
  /*signal(SIGALRM, SIG_IGN);*/
#endif
#endif

#ifdef DEBUG
  clear();
  refresh();
  endwin();
#endif /* DEBUG */

  intr(0);
  if(!i)
  {
    printf("Could not fill grid.\n");
    exit(1);
  }
  return(0);
}
