#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <exec/types.h>
#include <libraries/dosextens.h>
#include <devices/tpt.h>

#include <dlg/dlg.h>
#include <dlg/resman.h>
#include <dlg/user.h>

#include <link/io.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: Who " BUILDVER "." ObjRev " ©" COPYRIGHT " by Digerati Dreams "__AMIGADATE__;

#define STK RStruct.Command_Stack
#define HOT UserDat.Hot_Keys

void CleanUp(char *);


struct Library      *DLGBase = NULL;
struct SearchCookie *sc      = NULL;
struct LangStruct   *ls;
char               **SA;


void main(int argc,char **argv)

{char            *filename;
 int              retval;
 USHORT           pos    = 1;

 char             dbuffer[512];
 char             Port[4];
 char             Ext[4];

 struct tdev_info mydevinfo;
 struct USER_DATA UserDat;
 struct Ram_File  RStruct;
 struct Ram_File  TStruct;


 if (!(DLGBase = OpenLibrary(DLGNAME, DLGVERSION)))  exit(5);

 if (GetDevName(Ext)==-1)              CleanUp("Unable to get port");
 if (!ReadUser(&RStruct,&UserDat,Ext))  CleanUp("Unable to read user data");
 if (!(ls = GetLang(Ext)))             CleanUp("Unable to get language");
 SA = ls->strings;

 sc = SearchStart("T:", "*.User");
 if (!sc)  CleanUp(SA[2545]);

 while(filename=SearchNext(sc))
      {ASPrintf(NULL, Port, "%3.3s", filename);
       ReadRam(&TStruct, Port);
       TDevQuery(&mydevinfo, Port);

       ASPrintf(&UserDat, dbuffer,SA[2546],TStruct.Name,Port,mydevinfo.devname,mydevinfo.unit,TStruct.Baud_Rate,TStruct.Action);
       ASPrintf(&UserDat, dbuffer+strlen(dbuffer),SA[2547],TStruct.CLI);

       retval = DispBuffer(Output(), dbuffer, &pos, 0, NULL, 0, "\003", &UserDat);
       if (retval == -2)
           UserDat.More_Flag = 0;
         else
           if (retval)  break;
      }

 CleanUp(NULL);
}


void CleanUp(char *s)

{if (sc)      SearchEnd(sc);
 if (DLGBase) CloseLibrary(DLGBase);

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

 exit(s?5:0);
}
