/*
** FILE_ID_DIZ Reprocesser for CNet/4
** by Kelly Cochran
** Last Updated: Sun Nov 16 15:36:59 1997
*/

struct CNetCContext *context   = NULL;
struct Library      *CNetBase  = NULL;
struct Library      *CNetCBase = NULL;
struct Library      *CNet4Base = NULL;
struct PortData     *z         = NULL;

void fix_diz_main(void);
void process_diz(int i);

void main(void)
{
   struct args
   {
      ULONG Port;
   };

   struct RDArgs *rda;
   struct args    myargs;

   if(rda = ReadArgs("PORT/A", (LONG *)&myargs, NULL))
   {
      if(myargs.Port)
      {
         if(CNetBase = OpenLibrary("cnet.library", 4))
         {
            if(CNetCBase = OpenLibrary("cnetc.library", 4))
            {
               if(context = CNC_InitContext((char *)myargs.Port))
               {
                  if(CNet4Base = OpenLibrary("cnet4.library", 4))
                  {
                     z = context->z;

                     if(z->sb && (z->sb->Marker < 2))
                     {
                        fix_diz_main();
                     }
                     else
                     {
                        CNC_PutText("You need to be in a subboard to use this function!n1");
                     }

                     CloseLibrary(CNet4Base);
                  }
                  CNC_ShutDown(NULL);
                  CNC_DisposeContext();
               }
               CloseLibrary(CNetCBase);
            }
            CloseLibrary(CNetBase);
         }
      }

      FreeArgs(rda);
   }
}

void fix_diz_main(void)
{
   struct RangeContext rc;

   char *args;

   if(z->npitems < 2)
   {
      CNC_EnterLine(30, ELINE_NONE, "Update DIZ on which items?: ");
      if(!*z->InBuffer)
         return;
      else
         args = z->InBuffer;
   }
   else if(*z->pitem[1])
   {
      args = z->pitem[1];
   }
   else
   {
      return;
   }

   if(CNetFindRange(args, 1, z->sb->rn, &rc))   /* process items */
   {
      int i,
          x = 0;

      while((i = CNetNextRange(&rc)) > 0)
      {
         process_diz(i-1);
         x++;
      }

      if(x > 30)  /* alot of changes... */
      {
         CNC_PutText("You may wish to run repair_sub to repack subboard datafilesn1.");
      }
   }

   return;
}

void process_diz(int i)
{
   char *file            = z->ip[i]->Title;  /* Title == Filename for files */
   char filename [ 256 ] = "";


   if(!z->ih[i]->Size)  /* is it a file? */
   {
      sprintf(z->ABuffer, "n1Skipping \"%s\"; not a file.n1", file);
      CNC_PutA();
      return;
   }

   sprintf(z->ABuffer, "n1Looking for file %s..", file);
   CNC_PutA();

   strcpy(filename, z->sb->ZeroPath);  /* make file directory */
   AddPart(filename, file, sizeof(filename));

   if(!FileExists(filename))  /* does it exist? */
   {
      sprintf(z->ABuffer, "n1File %s does not exist!n1", file);
      CNC_PutA();

      return;
   }
   else
   {
      BPTR fpshort;

      strcpy(z->ABuffer, z->sb->DataPath);
      AddPart(z->ABuffer, "data/_Short", z->cnp->gc.ABufferSize);

      if(fpshort = Open(z->ABuffer, MODE_OLDFILE))
      {
         Seek(fpshort, z->ip[i]->InfoX, OFFSET_BEGINING);
         Read(fpshort, &z->FileNote, z->ip[i]->InfoLen);
         Close(fpshort);
      }
   }
}
