#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <devices/tpt.h>

#include <dlg/dlg.h>

#include <link/io.h>
#include <link/lang.h>

#include <proto/exec.h>
#include <proto/dos.h>
#include <proto/dlg.h>

#include <pragmas/dlg.h>

#include <private/Version.h>
#define  ObjRev "1"
const UBYTE version[]="\0$VER: TStat " BUILDVER "." ObjRev " ©" COPYRIGHT " by Digerati Dreams "__AMIGADATE__;

void Usage(char *);
void CleanUp(char *);

BPTR            sout;
char          **SA;
struct Library *DLGBase = NULL;

char desc[32][20] = {"T_ECHO",
                     "T_CRLF",
                     "T_RAW",
                     "T_RPEND",
                     "T_WAIT_FOR",
                     "T_TYPEAHEAD_FULL",
                     "T_BREAK",
                     "T_WINDOW",
                     "T_KILL_ENABLE",
                     "T_DO_PEND",
                     "T_KILL_PEND",
                     "T_SER_TIMEOUT",
                     "T_DO_TIMEOUT",
                     "T_CTLD",
                     "T_PAUSE",
                     "T_PAUSED",
                     "T_KILLED",
                     "T_SCREEN",
                     "T_PASS_THRU",
                     "T_VERB_PAUSE",
                     "T_CWRITE_PEND",
                     "T_LINEFREEZE",
                     "T_FROZEN",
                     "T_WRITE_PEND",
                     "T_BUFFER",
                     "T_ANSIPOS",
                     "UnUsed",
                     "UnUsed",
                     "UnUsed",
                     "UnUsed",
                     "UnUsed",
                     "UnUsed"
                    };

void main(int argc, char **argv)

{long  retval;
 long  pos;
 char *s;
 char  devname[4] = "";

 sout    = Output();
 DLGBase = OpenLibrary(DLGNAME, DLGVERSION);
 if (!DLGBase)               CleanUp("Unable to open dlg.library");
 if (!(SA = getlang(NULL)))  CleanUp("Can't read language file");

 AFPrintf(NULL, sout, "\n");

 while(--argc > 0)
      {s = *++argv;

       if (*s++ == '-')
          {while(*s)
                {switch(*s++)
                       {case 'P':
                        case 'p':  if (!--argc)  break;
                                   strncpy(devname,*++argv,3);
                                   devname[3] = 0;
                                   Upper(devname);
                                   break;
                       }
                }
          }
      }
 if (!devname[0])  Usage(SA[3189]);

 retval=TSetFlags(0, devname);
 if (retval==ACTIVERR)
     AFPrintf(NULL, sout, SA[3190]);
   else
     if (retval==GENERALERR)
         AFPrintf(NULL, sout, SA[3191]);
       else
        {AFPrintf(NULL, sout, SA[3192],retval);
         for(pos=31; pos >= 0; pos--)
             if (retval & (1L<<pos))  AFPrintf(NULL, sout, SA[3193],(1L<<pos),desc[pos]);
  }

 AFPrintf(NULL, sout, "\n");
 CleanUp(NULL);
}


void Usage(char *string)

{
 AFPrintf(NULL, sout, SA[3194],string);
 AFPrintf(NULL, sout, SA[3195]);

 CleanUp(NULL);
}


void CleanUp(char *s)

{if (DLGBase)  CloseLibrary(DLGBase);

 if (s)
    {Write(sout, "\n Error: ", 9);
     Write(sout,  s,    strlen(s));
     Write(sout, "\n\n",       2);
    }

 exit(s?5:0);
}
