
#include <exec/types.h>
#include <exec/libraries.h>
#include <proto/exec.h>
#include <string.h>
#include <stdio.h>

#ifdef TRAPLIST
#include "trap.h"
#define NODELIST_NAME TRAPLIST_NAME
#define NODELIST_VER TRAPLIST_VER
#endif

#ifdef IGEN
#include "igen.h"
#endif

struct Library *NodelistBase;
char *VerString="VER: JamTool 0.13";

const char *const nl_keys[] =
    { "Node", "Host", "Hold", "Hub", "Pvt", "Point", "Zone", "Region", "Down" };

int main(int argc, char **argv)
{
    void writevar(char stem[], char info[]);

    NodeList the_nodelist;
    NodeDesc *node_desc;

    FILE *datevar;
    FILE *lsys;

    int wazoocap = 0;
    int error = 0;

    int i;

    char temp[255], tmp[255], t[50], phone[50];
    char stem[255];
    char nodelistdir[255];

    Addr find_me;

    strcpy(nodelistdir, "Nodelist:");

    for (i = 0; i < argc; i++)
        printf("%s ", argv[i]);
    printf("\n");

    if (argc > 1) argv[1][1] = tolower(argv[1][1]);

    if ((argc == 1)||(argv[1][0] != '-')||(argv[1][1] != 'f')&&(argv[1][1] != 'd')
       &&(argv[1][1] != 'v') && (argv[1][1] != 'u') && (argv[1][1] != 'l'))
    {
       
       printf("\nUsage: %s -command options\n\n", argv[0]);
       printf("       %s -f Fido-Address line.stem {nodelistpath}\n", argv[0]);
       printf("       %s -u UUCP-Address line.stem\n", argv[0]);
       printf("       %s -l Fido-Address\n", argv[0]);
       printf("       %s -d wazoo.cap line\n", argv[0]);
       printf("       %s -v line\n", argv[0]);

       exit(0);
    }

   switch(argv[1][1])
   {
      case 'l':
      case 'f':
      {
          if (argv[1][1] == 'l')
          {
             if (argc != 3)
             {
                printf("Usage: %s -l Address\n\n", argv[0]);
                printf("       This option takes a Fido address, and does a nodelist lookup\n");
                printf("       printing results on the output console.\n");
                exit(5);
             }
          }
          else
          {
              if (argc == 5)
              {
                  strcpy(nodelistdir, argv[4]);
                  argc = 4;
              }
              if (argc != 4)
              {
                  printf("Usage: %s -f Address $(line).stem {Nodelistpath}\n\n", argv[0]);
                  printf("       This option takes an address, a stem value, and an optional\n");
                  printf("       path to the nodelist.  It fills up a number of env: variables\n");
                  printf("       which can be used with JamMail.\n");
                  exit(5);
              }
          }
          {
             sprintf(stem, "delete >nil: env:%s.#?", argv[3]);
             system(stem);

             if(!(NodelistBase = OpenLibrary(NODELIST_NAME, NODELIST_VER)))
             {
                 printf("Can't open library\n");
                 exit(5);
             }

             if((the_nodelist = NLOpen(nodelistdir, 0)))
             {

                 Addr def;

                 def.Zone=1;
                 def.Net=163;
                 def.Node=0;
                 def.Point=0;
#ifdef IGEN
                 def.Domain="FidoNet";
#endif
                 strcpy(temp, argv[2]);

#ifdef TRAPLIST
                 {
                   int j;

                   for (i=0; !(isdigit(argv[2][i])); i++);

                   for (j = 0; j < strlen(argv[2])-i+1; j++)
                   {
                      temp[j]=argv[2][i+j];
                   }
		 }
#endif

                 if (NLParseAddr(&find_me, temp, &def))
                 {
                    printf("Invalid address %s\n", temp);
                 }
                 else
                 {
                   
                     if(node_desc = NLFind(the_nodelist, &find_me, 0))
                     {

                         if ((argv[1][2]) || (argv[1][1]=='l'))
                         {
                              sprintf(tmp, "FidoNet#%ld:%ld/%ld.%ld", node_desc->Node.Zone,
                                node_desc->Node.Net, node_desc->Node.Node, node_desc->Node.Point);
 
                              printf("Address: %s (%s)\n", tmp, node_desc->Passwd);
                              printf("Sysop  : %s (%s)\n", node_desc->Sysop, node_desc->City);
                              printf("System : %s\n", node_desc->System);
                              printf("Flags  : %s\n", node_desc->Flags);
                         }

                         if (argv[1][1] == 'l')
                         {
                              printf("Number : %s\n\n", node_desc->Phone);
                         }
                         else
                         {
                              sprintf(temp, "%s", node_desc->Sysop);
                              sprintf(stem, "%s.Sysop", argv[3]);
                              writevar(stem, temp);

                              sprintf(temp, "%ld", node_desc->BaudRate);
                              sprintf(stem, "%s.MaxBaud", argv[3]);
                              writevar(stem, temp);

                              sprintf(temp, "%ld", node_desc->Cost);
                              sprintf(stem, "%s.Cost", argv[3]);
                              writevar(stem, temp);

                              sprintf(temp, "%s", node_desc->Phone);
                              sprintf(stem, "%s.Number", argv[3]);
                              if (temp[0]=='-')
                                 sprintf(temp, "");
                              writevar(stem, temp);
 
                              sprintf(temp, "%s", node_desc->Flags);
                              sprintf(stem, "%s.Flags", argv[3]);
                              writevar(stem, temp);

                              sprintf(temp, "%s", node_desc->Passwd);
                              sprintf(stem, "%s.Password", argv[3]);
                              writevar(stem, temp);

                              sprintf(temp, "%s", node_desc->City);
                              sprintf(stem, "%s.City", argv[3]);
                              writevar(stem, temp);
 
                              sprintf(temp, "%s", node_desc->System);
                              sprintf(stem, "%s.SiteName", argv[3]);
                              writevar(stem, temp);

                              NLFreeNode(node_desc);
                         }
                     }
                     else
                     {
                          printf("%s Not Found!\n", argv[2]);
                          error = 5;
                     }
                 }
                 NLClose(the_nodelist);
             }
             else
             {    
                 puts("Nodelist won't open");
                 error = 10;
             }
             CloseLibrary(NodelistBase);
          }
          break;
      }
      
      case 'd':
      {
          if (argc != 4)
          {
              printf("Usage: %s -d Wazoo.Cap line\n\n", argv[0]);
              printf("       This option takes a decimal value and returns an ascii string\n");
              printf("       in Env:{line}.wazoo.\n");
              error=1;
          }
          else
          {
              strcpy(tmp, "");
              wazoocap = atol(argv[2]);
              if (wazoocap &     1) strcat(tmp, " DietIFNA");
              if (wazoocap &     2) strcat(tmp, " Opus");
              if (wazoocap &     4) strcat(tmp, " ZedZip");
              if (wazoocap &     8) strcat(tmp, " ZedZap");
              if (wazoocap &    16) strcat(tmp, " Janus");
              if (wazoocap &    32) strcat(tmp, " 32");
              if (wazoocap &    64) strcat(tmp, " 64");
              if (wazoocap &   128) strcat(tmp, " 128");
              if (wazoocap & 16384) strcat(tmp, " +Domain");
              if (wazoocap & 32768) strcat(tmp, " +Freq");
              sprintf(temp, "%s (%ld)", tmp, wazoocap);
              sprintf(stem, "%d.WaZoo", atol(argv[3]));
              writevar(stem, temp);
              error = 0;
          }
          break;
      }

      case 'v':
      {
          if (argc != 3)
          {
              printf("Usage: %s -v [line]\n\n", argv[0]);
              printf("       This option takes a single parameter of the line number, and\n");
              printf("       creates two env: variables, {line}.version and {line}.date.\n");
              error = 1;
          }
          else
          {
              sprintf(temp,"date >env:%s.Date", argv[2]);
              system(temp);
              sprintf(temp,"env:%s.Date", argv[2]);
              datevar = fopen(temp, "r");
              fgets(tmp, 80, datevar);
              fclose(datevar);
              tmp[strlen(tmp)-10] = 0;
              datevar = fopen(temp, "w");
              fputs(tmp, datevar);
              fclose(datevar);
              sprintf(temp,"version >env:%s.Version", argv[2]);
              system(temp);
              sprintf(temp,"env:%s.Version", argv[2]);
              datevar = fopen(temp, "r");
              fgets(tmp, 80, datevar);
              fclose(datevar);
              tmp[strlen(tmp)-1] = 0;
              datevar = fopen(temp, "w");
              fputs(tmp, datevar);
              fclose(datevar);
              error = 0;
          }
          exit(error);
          break;
      }
  
      default:
      {
          if (argc != 4)
          {
              printf("Usage: %s -U Address $(line).stem {Nodelistpath}\n\n", argv[0]);
              printf("       This option takes a UUCP address, and a stem value.  It\n");
              printf("       fills up the stem.number variable with the sites phone\n");
              printf("       number which Jammail.wpl uses to dial.\n");
              exit(5);
          }
          else
          {


              for (i=0;i<strlen(argv[2]);i++)
                 argv[2][i]=toupper(argv[2][i]);

              printf("Address: %s\n", argv[2]);

              sprintf(stem, "%s.Number", argv[3]);

              lsys = fopen("uulib:l.sys", "r");
              while (fgets(tmp, 250, lsys) != NULL)
              {
                 sscanf(tmp, "%s %s %s %s %s", temp, t, t, t, phone);
                 for (i=0;i<strlen(temp);i++)
                    temp[i]=toupper(temp[i]);

                 temp[i]=0;

                 if (!strcmp(temp, argv[2]))
                 {
                    writevar(stem, phone);
                    printf("Phone  : %s\n", phone);
                    break;
                 }
              }
              fclose(lsys);          

          }
       }
    }
    exit(error);
}

void writevar(char *name, char *info)
{

   FILE *env;
   char temp[255];
   sprintf(temp, "env:%s", name);
   env = fopen(temp, "w");
   fputs(info, env);
   fclose(env);
   return;

}

