// C++ code Copyright (C) David R. Evans G4AMJ/NQ0I

// MIDAS encapsulation

#define REV_NR 17.00

#include <config.h>

#ifdef MSDOS
#include <alloc.h>
#include <new.h>
#endif

#include <ctype.h>
#include <math.h>
#include <stdio.h>
#include <signal.h>

// the Mac doesn't put some files in the usual places
#ifndef MAC
#include <sys/types.h>
#include <sys/timeb.h>
#endif

#ifdef MAC
#include <Types.h>
#include <Time.h>
#endif

// SunView mode
#ifdef SUNVIEW
#include <sm_sunview_mode.h>
// SunView batch mode
#ifdef BATCH
#include <sm_batch_mode.h>
#endif
#endif


// VGA mode
#ifdef IBMVGA
#include <sm_vga_mode.h>
#include <fcntl.h>
#endif

// MAC mode
#ifdef MAC
#include <sm_mac_mode.h>
#endif

// Windows mode
#ifdef MSWINDOWS
#include <sm_w_mode.h>
// Windows batch mode
#ifdef BATCH
#include <sm_w_bat.h>
#endif
#endif

const float rev_nr = REV_NR;

operating_mode  *mode;

#ifdef IBMVGA
extern vga_mode* vmode;
int _fmode = O_BINARY;
#endif

#ifdef MSWINDOWS
#define MAIN OwlMain
#else
#define MAIN main
#endif

/****************************** Main **********************************/


MAIN(int argc, char** argv)
{
#ifndef MSWINDOWS
  if (argc > 2) then
  { cerr << "Usage : " << argv[0] << " [batch file]\n";
    exit(-1);
  }
#endif

#ifdef SUNVIEW
#ifdef BATCH
  if (argc == 2) then                   // read from batch file
  { sunbmode = new sun_batch_mode(argv[1], rev_nr);
    mode = sunbmode;
    sunbmode->execute();
  }
  else
#endif

  {
	 imode = new sunview_mode(rev_nr);
    mode = imode;
    window_main_loop((Frame)(imode->base()));
    exit(0);
  }
#endif

#ifdef IBMVGA
	 vmode = new vga_mode(rev_nr);
    mode = vmode;
    vmode->execute();
    delete vmode;
#endif


#ifdef MSWINDOWS
#ifdef BATCH
    if (argc == 2) then                   // read from batch file
    { winbmode = new windows_batch_mode(argv[1], rev_nr);
      mode = winbmode;
      winbmode->execute();
      delete winbmode;
    }
    else
#endif

// check that the system meets our minimum requirements
{    if (!mouse_present()) then
      fatal_error("No mouse detected");
	 if ((screen_width() < 800) || (screen_height() < 600)) then
		fatal_error("Screen has resolution < 800x600");

// OK. Go ahead.
	 wmode = new windows_mode(REV_NR);
	 mode = wmode;
	 wmode->execute();
	 delete wmode;
}
#endif

#ifdef MAC
	 mmode = new mac_mode(rev_nr);
	 mode = mmode;
	 mmode->execute();
	 delete mmode;
#endif
  return 0;
}
