#include "exec/types.h" 
#include "exec/memory.h" 
#include "exec/io.h"
#include "exec/libraries.h"
#include "exec/execbase.h"
#include "libraries/dos.h" 
#include "libraries/dosextens.h"
#include "workbench/startup.h"
#include "workbench/workbench.h"
#include <ctype.h>
#include <functions.h>

#define NOBUF 512

char Q1TEXT[80] =	"q1text.dat";
extern int q1text_dat;

struct FileHandle *tty;

char    obuf[NOBUF];    /* Output buffer        */
static char *ob = obuf;
int nobuf;              /* # of bytes in above      */

extern struct WBStartup *WBenchMsg;

struct Library *IconBase = NULL;

char WindowName[256] = "NEWCON:0/0/640/200/   World   ";

AmigaInit(argc,argv)
char *argv[]; 
{
   
   if(argc==0)
     getWbArgs(WBenchMsg);
   else
     getCLIargs(argc,argv);
}

getWbArgs(wbMsg)
struct WBStartup *wbMsg;
{
   struct WBArg  *wbArg;
   struct DiskObject *diskobj;
   char **toolarray;
   char *s;
   
   struct Lock *olddir;
   

   /* Defaults */

   wbArg = wbMsg->sm_ArgList;
   
   if((IconBase = OpenLibrary("icon.library", 0L)))
   {
      diskobj=(struct DiskObject *)GetDiskObject(wbArg->wa_Name);
      if(diskobj)
      {
         toolarray = (char **)diskobj->do_ToolTypes;

         if(s=(char *)FindToolType(toolarray,"WIN"))  strcpy(WindowName,s);
         FreeDiskObject(diskobj);
      }
      CloseLibrary(IconBase);
   }
   
   ttopen();

   if(wbMsg->sm_NumArgs > 1L)       /* use second arg as file instead */
   {                               /* of "q1text.dat" */
     wbArg++;
     
     if(wbArg->wa_Lock==NULL)
     {
       printf("Could not open file %s!\n",Q1TEXT);
       ttclose();
       _exit(20L);
     }
     
     olddir=CurrentDir(wbArg->wa_Lock);
     
     q1text_dat = open(Q1TEXT,0);    /* open data file */
     
     if(q1text_dat<0)
     {
       printf("Could not open file %s!\n",Q1TEXT);
       ttclose();
       _exit(20L);
     }
     
     CurrentDir(olddir);
     
   }
   else
   {
     q1text_dat = open(Q1TEXT,0);    /* open data file */
     
     if(q1text_dat<0)
     {
       printf("Could not open file %s!\n",Q1TEXT);
       ttclose();
       _exit(20L);
     }
   }
     
}

getCLIargs(argc,argv)
int argc;
char *argv[];
{
  int arg;
  int lace, newcon;
  
  lace=FALSE;
  newcon=TRUE;
  for(arg=1;arg<argc;arg++)
  {
    if(argv[arg][0]=='-')
    {
      switch(argv[arg][1])
      {
        case 'n':     /* use NEWCON: window */
          newcon=TRUE;
          break;
     
        case 'c':    /* use CON: window */
          newcon=FALSE;
          break;
           
        case 'i':    /* use 400 line interlaced window */
          lace=TRUE;
          break;
          
		case 'N':   /* use 200 line (non-interlaced) window */
		  lace=FALSE;
		  break;
             
        default:     /* ignore */
          ;

      }
    }
    else
    {
      strcpy(Q1TEXT,argv[arg]);        /* alternate name for data file */
    }
  }
     
   
  if(newcon && lace)
    strcpy(WindowName,"NEWCON:0/0/640/400/   WORLD   ");
  if(newcon && !lace)
    strcpy(WindowName,"NEWCON:0/0/640/200/   WORLD   ");
  if(!newcon && lace)
    strcpy(WindowName,"CON:0/0/640/400/   WORLD   ");
  if(!newcon && !lace)
    strcpy(WindowName,"CON:0/0/640/200/   WORLD   ");
  ttopen();
  
  q1text_dat = open(Q1TEXT,0);    /* open data file */
     
  if(q1text_dat<0)
  {
    printf("Could not open file %s!\n",Q1TEXT);
    ttclose();
    _exit(20L);
  }
}   

ttopen()
{
    
    nobuf=0;

    tty = Open(WindowName, MODE_NEWFILE);
    if (tty == NULL)
        exit(20);
}


ttclose()
{
    char t[256];
    
    printf("Press Return...");
    gets(t);
    if (tty != NULL) {
        ttflush();
        Close(tty);
    }
    tty = NULL;
}


getc()
{
    char c;
    int result;

    do
    {
        flush();
        result = Read(tty, &c, 1L);
    } while (result != 1);
    return (c & 0177);
}


flush()
{

    ttflush();
    Write(tty, obuf, (long) ob-obuf);
    ob = obuf;
}
/*
 * This function does the real work of
 * flushing out buffered I/O on the Amiga. All
 * we do is blast out the block with a write call.
 */


ttflush()
{
    if (nobuf > 0) {
        Write(tty, obuf, (long) nobuf);
        nobuf = 0;
    }
}




/*
 * Output a character.
 */
putc(c)
int c;
{
    if (ob >= &obuf[sizeof(obuf)])
        flush();

    if(c=='\n')
    {
      *ob++ = c;
      flush();
    }
    else
      *ob++ = c;
}
