/************************* Main *********************/
/*                                                  */
/*  Hard Disk Backup  Usage Backup dhx:             */
/*  where x is the hard drive number                */
/*                                                  */
/****************************************************/

#include "exec/memory.h"
#include "libraries/dos.h"
#include "libraries/dosextens.h"
#include "stdio.h"
#define BLOCKS 1756
extern struct FileInfoBlock *AllocMem();
extern struct FileLock *Lock(),*CreateDir();
int back=0,size=0;
char *dest; /* global for convience */
int ind;
char temp[100];

main(argc,argv)
int argc;
char *argv[];
{
   char *src;

   src = argv[1];
   if(src[0]=='?'||argc!=2)
   {
      printf("c"); /* reset screen (clear) */
      center(3,"        [7mHard Disk Backup[m");
      center(5,"Written by");
      center(7,"Charles Jeffery Carter");
      center(8,"4601 Wilshire Cove    ");
      center(9,"Huntsville, Ala. 35816");
      center(11,"This software may be freely distributed");
      center(12,"If you like this program, Let me know  ");
      center(14,"Usage--> run Backup dhx:               ");
      center(16,"Where x is the hard drive number       ");
      center(18,"Format drive df?: name Back? noicons; as you go ");
      center(20,"To restore: copy Back?: to dhx: all; for each backup disk");
      center(22,"     [7mPress Return to Continue[m");
      getchar();
      printf("c"); /* reset screen (clear) */
      exit(0);
   }

   ind=strlen(src);
   if(src[ind-1]!=':') argc==0;
   CheckSize(src);
   search(src,0);
}

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

   if((FIB=
       AllocMem((long)sizeof(struct FileInfoBlock),MEMF_CHIP|MEMF_CLEAR))==0)
   {
      printf("Can not allocate memory for FIB\n");
      exit(0);
   }
   if((lock=(struct FileLock *)
            AllocMem((long)sizeof(struct FileLock),MEMF_CHIP|MEMF_CLEAR))==0)
   {
      printf("Can not allocate memory for Lock\n");
      goto out1;
   }

   if((lock=Lock(name,SHARED_LOCK))==0)
   {
      printf("Can not lock %s\n",name);
      goto out2;
   }

   if((Examine(lock,FIB))==0)
   {
      printf("Can not Examine %s\n",name);
      goto out3;
   }

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

      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);
out3:
   UnLock(lock);
out2:
   FreeMem(lock,(long)sizeof(struct FileLock));
out1:
   FreeMem(FIB,(long)sizeof(struct FileInfoBlock));
}   

/************************* CheckSize ***********************/
CheckSize(name)
char *name;
{
   int i;

   if(size>0) return(0);
   size=BLOCKS;
   back++;
   printf("Insert Back%d: into any drive and hit return",back);
   getchar();

   i=0;
   while(1)
   {
      for(;name[i]!='/';i++) if(name[i]==0) return(1);
      name[i]=0;
      DoDir(name);
      name[i]='/';
      i++;
   }
}

/************************* DoDevice ***********************/
DoDevice(FIB)
struct FileInfoBlock *FIB;
/* do whatever when a Device is found */
{
   printf("Backing up Volume %s:\n",FIB->fib_FileName);
}

/************************* DoDir ***********************/
DoDir(name)
char *name;
/* do whatever when a Dir is found */
{
   size--; /* takes a block for a directory */
   if (CheckSize(name)) size--;

   sprintf(temp,"Back%d:%s",back,&name[ind]);
   UnLock(CreateDir(temp));
}

/************************* DoFile ***********************/
DoFile(name,FIB)
char *name;
struct FileInfoBlock *FIB;
/* do whatever when a File is found */
{
   int s;
   s=((FIB->fib_Size/488)+2);
   s+=(s/70);
   size-=s;
   if (CheckSize(name)) size-=s;
   sprintf(temp,"Copy \"%s\" to \"Back%d:%s\"",name,back,&name[ind]);
   printf("size %d %s\n",size,temp);
   Execute(temp,0L,0L);
}

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