#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <intuition/intuitionbase.h>
#include <intuition/intuition.h>
#include <devices/timer.h>
#include <graphics/gfx.h>
#include <graphics/rastport.h>
#include <exec/memory.h>
#include "posbb.h"
#include "posbb_rev.h"
#include "compiler.h" /* for compiler specific stuff */
/* #include "posbb_logo.h" */

FILE *fi = 0;
UBYTE Version []=VERSTAG;
BYTE Copyright []=VERS;
struct IntuitionBase *IntuitionBase=0;
struct GfxBase *GfxBase=0;
struct Window *window=0;
struct timerequest *timereq = NULL;
struct MsgPort *msgport=0;
struct Device *TimerBase=0;


int main( argc, argv )
int argc;
char *argv[];
{
ULONG tests = 0;
int c,precision = POSBB_PRECISIONLOW;  /* default precision is 'low' at this time. In future posbb will able to change it depending on system speed */
BOOL freeze = FALSE;   /* by default posbb doesn't freeze multitasking */
BOOL mt = FALSE;       /* by default manual timing is turned off */
   if (argc == 1)
   {
      printf("%s (Amiga)\n",VSTRING);
      printf("Portable OS Based Benchmark\n");
      printf("Created by Pietro Altomani <altomanipietro@pragmanet.it>\n");
      printf("Usage: %s <options>\nOptions:\n  -all  Perform all tests\n  -gfx Perform only graphics tests\n  -mem Perform only memory tests\n  -disk Perform only disk tests\n  -math Perform only math tests\n  -o <filename> Output file name\n  -freeze Freeze the machine when running\n-p <precision> Set precision. <precision> can be 'low','medium','high','higher' or 'highest'.\n-mt  Turn on manual timing (beeps starting and finishing tests)\n See docs for more infos.\n",argv[0]);
      exit(10);
   }

   for (c=0;c<argc;c++)
   {
#ifndef NOFORBID
      if (strcmp(argv[c],"-freeze") == 0)
      {
	freeze = TRUE;
      }
#endif
      if ((strcmp(argv[c],"-all") == 0) || (strcmp(argv[c],"-full")  == 0))
      {
	 tests = -1;
      }
      if (strcmp(argv[c],"-gfx") == 0)
      {
	 tests |= (POSBB_TESTWRITEPIXEL | POSBB_TESTDRAWELLIPSE | POSBB_TESTDRAW);
      }
      if (strcmp(argv[c],"-disk") == 0)
      {
	 tests |= (POSBB_TESTREAD | POSBB_TESTWRITE);
      }
      if (strcmp(argv[c],"-math") == 0)
      {
	 tests |= (POSBB_TESTIMATH | POSBB_TESTFPMATH);
      }
      if (strcmp(argv[c],"-mem") == 0)
      {
	 tests |= (POSBB_TESTCOPYMEM | POSBB_TESTCOPYMEM_1MB | POSBB_TESTCOPYMEM_512KB);
      }
      if (strcmp(argv[c],"-mt") == 0)
      {
	mt = TRUE;
      }
      if ((strcmp(argv[c],"-o")) == 0)
      {
	 fi = fopen(argv[c+1],"w");
      }
      if ((strcmp(argv[c],"-p")) == 0)
      {
	 if ((strcmp(argv[c+1],"low")) == 0) precision = POSBB_PRECISIONLOW;
	 if ((strcmp(argv[c+1],"medium")) == 0) precision = POSBB_PRECISIONMEDIUM;
	 if ((strcmp(argv[c+1],"high")) == 0) precision = POSBB_PRECISIONHIGH;
	 if ((strcmp(argv[c+1],"higher")) == 0) precision = POSBB_PRECISIONHIGHER;
	 if ((strcmp(argv[c+1],"highest")) == 0) precision = POSBB_PRECISIONHIGHEST;
      }
   }

   if (fi == 0) fi = fopen(POSBB_DEFRESULTFILE,"w");
   IntuitionBase = (struct IntuitionBase *) OpenLibrary("intuition.library",0);
   GfxBase = (struct GfxBase *) OpenLibrary("graphics.library",0);
   msgport = (struct MsgPort *) CreatePort(NULL,NULL);
   timereq = (struct timerequest *) CreateExtIO( msgport,sizeof(struct timerequest));
   OpenDevice(TIMERNAME,UNIT_MICROHZ,timereq,NULL);
   TimerBase = timereq->tr_node.io_Device;
   window = (struct Window *) OpenWindowTags(NULL,WA_Left,0,WA_AutoAdjust,TRUE,WA_Top,0,WA_InnerWidth,320,WA_InnerHeight,256,WA_Title,"POSBB Test Window",WA_DragBar,TRUE,WA_DepthGadget,TRUE,NULL);

  /* Draw the logo in the window */
  /* Working in progress ! */
  #ifdef PRINTLOGOWORKS        /* this turns off logo section if PRINTLOGOWORKS isn't defined.  */
  posbb_logo_bm = (struct BitMap *) POSBB_AllocMem(sizeof(struct BitMap),MEMF_CHIP);  /* Allocate memory for the bitmap */
  posbb_logo_bm->BytesPerRow = 20;
  posbb_logo_bm->Rows = 257;
  posbb_logo_bm->Flags = BMF_DISPLAYABLE;
  posbb_logo_bm->Depth = 2;
  posbb_logo_bm->Planes[0] = (UBYTE *)posbb_logo_bm[0];
  posbb_logo_bm->Planes[1] = (UBYTE *)posbb_logo_bm[1];
  BltBitMapRastPort(posbb_logo_bm,0,0,window->RPort,0,0,320,256,0x0C0);
  Delay(50);
  #endif

   Perform_Test(tests,precision,freeze,mt);

  if (window) CloseWindow(window);
  if (IntuitionBase) CloseLibrary(IntuitionBase);
  if (GfxBase) CloseLibrary(GfxBase);
  if (timereq) CloseDevice(timereq);
  if (timereq) DeleteExtIO(timereq);
  if (msgport) DeleteMsgPort(msgport);

 if (fi) fclose( ( FILE * ) fi );
   exit(0);
}


