               /*****************************************a*\
                *                                        *
                *  ArcPrepare V2.1  © 1988 G.Glendown    *
                *                                        *
                *  This program is placed in the public  *
                *  domain, no fee may be charged except  *
                *  for media and copying. Distribution   *
                *  is only permitted via electronic mail *
                *  and similar and by Fred Fish's PD     *
                *  Disks. No other PD series may include *
                *  ArcPrep on one of their disks.        *
                *                                        *
                * If you really like it, just send me $5 *
                * $10, $50, cars or whatever you think   *
                * it is worth to this address:           *
                *                                        *
                * Garry Glendown                         *
                * Gueldene Kammer 35                     *
                * 6430 Bad Hersfeld                      *
                * West Germany                           *
                *                                        *
                * Too, I'm happy about any new ideas or  *
                * most anything you write. Try to reach  *
                * me under:                              *
                *                                        *
                * Garry@DGIHRZ01.BITNET                  *
                *                                        *
               \******************************************/

/* ...and a voice from the chaos said:
         "Smile and be happy, it could come worse!"
     I smiled and was happy, and it came worse... */
/* (That's where I was a few days ago, trying to get this #@#@$% program
    to do RENAMEing and DELETEing itself. Good that my HD-Backup was only
    two days old! Can you imagine the program getting out of the start-path
    into the main directory and starting to rename *ALL* files ...? [without
    creating the EXECUTE_ME-file because I pressed CTRL-C])      */


#define FIBLEN   (long)sizeof(struct FileInfoBlock)
VOID   *AllocMem();         /* I don't like warnings...*/
struct   FileLock   *Lock();
char   startpath[256];         /* the path I started at */
struct   FileHandle   *fh,*Open(),*fh1;
                        /* Filehandles for the two Output-Files */
struct   FileHandle   *fhi;      /* and this for the inventory */

long   cnt=0;               /* File-Counter. 'Word' would have been  */
                        /* enough, but... */
int      inv=0;               /* inventory-flag */

/* This routine scans directories recursively and writes the data
   to the 2 files.                                       */

ScanDir(name)
char   *name;
{
char path[1000],p2[1000],p3[1000];
               /* Probably don't need 'em all */

struct FileLock         *lock;   /* local file/dir lock */
struct FileInfoBlock   *fib;

   strcpy(p3,startpath);
   strcat(p3,name);         /* Tadah! That's where we are! */

   if (lock=Lock(p3,SHARED_LOCK)) {   /* Can we lock in? */

      if (fib=AllocMem(FIBLEN,MEMF_PUBLIC)) {   /* Some memory, please */

         if (Examine(lock,fib)) {   /* Let's have a look ... */

            while (ExNext(lock,fib)) {   /* Anybody there ? */

               if (fib->fib_DirEntryType>0) {   /* wow, another room */
                        strcpy(path,name);
                        strcat(path,fib->fib_FileName); /* room-number... */
                  sprintf(p2,"makedir \"%s\"\n",path);
                     /* build Dir */
                  Write(fh,p2,(long)strlen(p2));   /* write it down */
                  if (path[0]!='\0')
                     strcat(path,"/");
                  ScanDir(path);            /* enter the room */
                  sprintf(p2,"delete \"%s%s\"\n",name,fib->fib_FileName);
                      /* get rid of it */
                  Write(fh1,p2,(long)strlen(p2));
               }
               else {   /* hey, a file! */
                  if ((strcmp(fib->fib_FileName,"EXECUTE_ME"))&&
                     (strcmp(fib->fib_FileName,"INVENTORY"))&&
                     (strcmp(fib->fib_FileName,"PREP_FILES"))) {
                     /* (phew, that was long.) Is the file from us?*/

                     sprintf(p2,"Rename \"%s%s\" Arc-%05ld\n",name,fib->fib_FileName,cnt);
                     /* What's in a name? Gimme a new one! */

                     Write(fh1,p2,(long)strlen(p2));
                     sprintf(p2,"Rename Arc-%05ld \"%s%s\"\n",cnt,name,fib->fib_FileName);
                     /* If you don't like it, bring it back... */
                     /* But, while you use it, */
                     /*        DON'T GIVE IT A BAD NAME! */
                     /* (Hey, d'you know Rodney Dangerfield ?) */
                     Write(fh,p2,(long)strlen(p2));
                     if (inv) {
                        sprintf(p2,"Arc-%05ld   %s%s\n",cnt,name,fib->fib_FileName);
                        Write(fhi,p2,(long)strlen(p2));
                     }
                     cnt++;   /* Prepare for files to come... */
                  }
               }
            }
         }
         else printf("Examine...\n");
         FreeMem(fib,FIBLEN);
      }
      else printf("Memory!\n");
      UnLock(lock);
   }
   else printf("Lock!\n");
}


main(argc,argv)
int      argc;
char   *argv[];
/* Hey, you wanna argue with me? Oh, just an argument, o.k. */
{
char   path[256];

   if (argc<2)
      puts("What do you want to prepare?"),
      puts("ArcPrep <Path> <-i>"),
      Exit();
   if (argc==3)
      if ((argv[2][0]=='-')&&(argv[2][1]=='i')) inv=1;
   strcpy(path,argv[1]);
   if (path[strlen(path)-1]!=':')
      strcat(path,"/");
   strcpy(startpath,path);
   strcat(path,"EXECUTE_ME");   /* Here's what to do after 'arc x' */
   fh=Open(path,MODE_NEWFILE);
   if (fh) {
      strcpy(path,startpath);
      strcat(path,"PREP_FILES"); /* And do this to make good names for
                             your files */
      fh1=Open(path,MODE_NEWFILE);
      if (fh1) {
         if (inv) {
            strcpy(path,startpath);
            strcat(path,"INVENTORY");   /* Here's what we got in here */
            if (!(fhi=Open(path,MODE_NEWFILE))) inv=0;
            else {
               Write(fhi,"»»» Inventory created with 'ArcPrep' by G.Glendown «««\n",55L);
               Write(fhi,"    ==============================================\n",51L);
               Write(fhi,"The following files are included in this archive:\n\n",51L);
            }
         }
         path[0]='\0';
         Write(fh,"; Script created with 'ArcPrep' by G.Glendown\n",46L);
         Write(fh,"failat 21\n",10L);
         Write(fh1,"failat 21\n",10L);
         ScanDir(path);
         Write(fh1,"echo \"Please delete 'PREP_FILES' now!\"\n",39L);
         Write(fh,"echo \"Please delete 'EXECUTE_ME' now!\"\n",39L);
         if (inv) Close(fhi);
         Close(fh1);
      }
      Close(fh);
   }
}
/* That's all, friends. No more. Nothing. NIL. Or, as Harpo would say:

                   "             "

   Capice ? */




































/*

But, if you really want some more, here are the greetings:

   'normal' greetings go to all Amiga-Users on the whole world
   special greetings to all the programmers and my friends
   very special greetings and many thanks to:
      Commo      for their Amiga
      Leo Schwab   for the fine hacks and his 'Iconify'-routine
               (you should receive my 'ViroTrack'-program soon)
      Fred Fish   (did anybody tell you that you have started a fine
                collection?) Thanks for it!
      My mother   I wouldn't make it through all them hours on my Amy
               without the food, sandwiches, drinks ...
      Thorsten Budesheim   for always stopping my best ideas from coming
                     by calling me...

O.K., but this is really it. Even *YOU* should notice that...

*/


