/************************* Main **************************/
/*  Disk Cataloguer                                      */
/*  Usage dcat VolumeName: [TO] [MasterCat:]             */
/*  Will Makedir("MasterCat:VolumeName")                 */
/*  Will copy all files onto this dir including comment  */
/*  BUT excluding any data                               */
/*                                                       */
/*  Known Bugs:  Does not handle imbedded spaces in path */
/*********************************************************/

#include "libraries/dos.h"
#include "stdio.h"
char catname[30] = "MasterCat:";
char catbuf[200];
int index;

main(argc,argv)
int argc;
char *argv[];
{
   struct FileInfoBlock *FIB, *AllocMem();
   long lock;
   char *temp;
   int badarg;

   badarg = FALSE;
   if ((argc==1) || (argc>4)) badarg = TRUE;

   temp = argv[1];
   if(temp[0]=='?')
   {
      printf("c"); /* reset screen (clear) */
      center(3,"The Disk Cataloger");
      center(5,"Written by");
      center(7,"Charles Jeffery Carter");
      center( 9,"This software may be freely distributed      ");
      center(10,"If you like this program, please send $10 to:");
      center(12,"4601 Wilshire Cove    ");
      center(13,"Huntsville, Ala. 35816");
      center(15,"Usage--> dcat VolumeName: [TO] [MasterCat:](default)      ");
      center(16,"Requires Two Drive System and A Formatted Disk.           ");
      center(17,"This program will copy the volume name of the disk to be  ");
      center(18,"cataloged, then all directory and file names with comments");
      center(19,"to the master catalog.  All AmigaDos commands can be used ");
      center(20,"with the catalog.  Additional utilities will be available.");
      center(22,"     [7mPress RETURN to Continue[m");
      getchar();
      printf("c"); /* reset screen (clear) */
      exit(0);
   }

   if (temp[strlen(temp)-1]!=':') badarg = TRUE;

   if (argc>2) strcpy(catname,argv[argc-1]);

   if (catname[strlen(catname)-1]!=':') badarg = TRUE;

   if (badarg)
   {
      printf("Usage--> %s VolumeName: [TO] [MasterCat:](default)\n",argv[0]);
      exit(1);
   }

   if((FIB=AllocMem((long)sizeof(struct FileInfoBlock),(long)0))==0)
   {
      printf("Can not allocate memory for FIB\n");
      return(0);
   }

   if((lock=Lock(temp,(long)SHARED_LOCK))==0)
   {
      printf("Can not lock %s\n",temp);
      FreeMem(FIB,(long)sizeof(struct FileInfoBlock));
      return(0);
   }

   if((Examine(lock,FIB))==0)
   {
      printf("Can not Examine %s\n",temp);
      UnLock(lock);
      FreeMem(FIB,(long)sizeof(struct FileInfoBlock));
      return(0);
   }

   strcpy(catbuf,catname);
   strcat(catbuf,&FIB->fib_FileName[0]);
   index = strlen(catbuf);
   strcat(&FIB->fib_FileName[0],":");
   printf("Cataloging Volume %s to %s\n",&FIB->fib_FileName[0],catname);
   search(&FIB->fib_FileName[0]);

   UnLock(lock);
   FreeMem(FIB,(long)sizeof(struct FileInfoBlock));
}   

/************************* Search ************************/
search(name)
char *name;
/* recursively search "name" for files */
{
   char subdir[80];
   struct FileInfoBlock *FIB, *AllocMem();
   long lock;

   if((FIB=AllocMem((long)sizeof(struct FileInfoBlock),(long)0))==0)
   {
      printf("Can not allocate memory for FIB\n");
      return(0);
   }

   if((lock=Lock(name,(long)SHARED_LOCK))==0)
   {
      printf("Can not lock %s\n",name);
      FreeMem(FIB,(long)sizeof(struct FileInfoBlock));
      return(0);
   }

   if((Examine(lock,FIB))==0)
   {
      printf("Can not Examine %s\n",name);
      UnLock(lock);
      FreeMem(FIB,(long)sizeof(struct FileInfoBlock));
      return(0);
   }

   if(FIB->fib_DirEntryType > 0)
   {
      if (name[strlen(name)-1]!=':')
         DoDir(name,FIB);
      else
         DoDevice(name,FIB);

      while(ExNext(lock,FIB))
      {
         strcpy(subdir,name);
         if (name[strlen(name)-1]!=':') strcat(subdir,"/");
         strcat(subdir,&FIB->fib_FileName[0]);
         if(FIB->fib_DirEntryType>0)
            search(subdir);
         else
            DoFile(subdir,FIB);
      }
   }
   else
      DoFile(subdir,FIB);

   UnLock(lock);
   FreeMem(FIB,(long)sizeof(struct FileInfoBlock));
}   

/************************* DoDevice ***********************/
DoDevice(name,FIB)
char name[];
struct FileInfoBlock *FIB;
/* do whatever when a Device is found */
{
long lock;
   printf("%s (device)\n",name);
   Common(name,FIB);
   catbuf[index]=NULL;
   if ((lock=CreateDir(catbuf))==0)
   {
      printf("Could not create volume directory %s\n",catbuf);
      exit(1);
   }
   UnLock(lock);
   DoComment(FIB);
}

/************************* DoDir ***********************/
DoDir(name,FIB)
char name[];
struct FileInfoBlock *FIB;
/* do whatever when a Dir is found */
{
long lock;
   printf("%s (dir)\n",name);
   Common(name,FIB);
   catbuf[index]='/';
   if ((lock=CreateDir(catbuf))==0)
   {
      printf("Could not create directory %s\n",catbuf);
      exit(1);
   }
   UnLock(lock);
   DoComment(FIB);
}

/************************* DoFile ***********************/
DoFile(name,FIB)
char name[];
struct FileInfoBlock *FIB;
/* do whatever when a File is found */
{
long file;

   printf("%s\n",name);
   Common(name,FIB);
   catbuf[index]='/';
   if((file=Open(catbuf,MODE_NEWFILE))==0)
   {
      printf("Could not Open file %s\n",catbuf);
      exit(1);
   }
   Close(file);
   DoComment(FIB);
}

/************************* Common ***********************/
Common(name,FIB)
char name[];
struct FileInfoBlock *FIB;
/* do common code */
{
   if (*(&FIB->fib_Comment[0]) != NULL) printf(": %s\n",&FIB->fib_Comment[0]);
   strcpy(catbuf,catname);
   strcat(catbuf,name);
}

/************************* DoComment ***********************/
DoComment(FIB)
struct FileInfoBlock *FIB;
/* Copy Comment from original to catalogue */
{
   if (*(&FIB->fib_Comment[0]) != NULL)
   {
      if (!(SetComment(catbuf,&FIB->fib_Comment[0])))
      {
         printf("\n Could not set comment on %s\n",catbuf);
         exit(1);
      }
   }
}

/************************* center ***********************/
center(row,string)
int row;
char *string;
/* printf the string centered on given row */
{
int col;
   col = (76-strlen(string))/2;
   printf("[%d;%dH%s",row,col,string);
}
