#include <stdlib.h>
#include <string.h>
#include <exec/types.h>
#include <ctype.h>

#include <dlg/dlg.h>
#include <dlg/resman.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: ListPorts " BUILDVER "." ObjRev " ©" COPYRIGHT " by Digerati Dreams "__AMIGADATE__;

void main   (int, char **);
void Usage  (char *);
void CleanUp(char *);

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


void main(int argc, char **argv)

{long  retval;
 char  buf[256];
 char *passwd = "";
 char *s;

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

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

/// parse args
 while(--argc > 0)
      {s = *++argv;
       if (*s++=='-')
          {while(*s)
                {switch(toupper(*s))
                       {case 'K': if (!--argc)  break;
                                  passwd = *++argv;
                                  break;
                       }

                 s++;
                }
          }
      }
//-

 retval = ListPorts(buf, passwd);

 if (retval == RMNOERR)
    {s = buf;
     if (*s)
        {AFPrintf(NULL, sout, SA[2854]);
         for(; *s; s+=3)
             AFPrintf(NULL, sout, SA[2855], s);
        }
      else
         AFPrintf(NULL, sout, SA[2856]);
    }
  else
    if (retval == NORM)
        CleanUp(SA[2857]);
      else
        CleanUp(SA[2858]);

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


void Usage(char *string)

{
 AFPrintf(NULL, sout, SA[2859],string);
 AFPrintf(NULL, sout, SA[2860]);

 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);
}
