/*
* =====================================================================
*
*   Program : MKDBLDIRS or MakeDoubleDirectories.    
*
*   Author : J. Van Houtven.                     Date : Sept 12th 1987.
*
* =====================================================================
*/


#include "libraries/dosextens.h"

# define MAX_NR_OF_DOUBLESUBDIRS  100

/* Only UnLock() locks that are obtained with Lock() or DupLock() (?) !!!! */

extern struct FileLock *CreateDir(), *CurrentDir();

struct FileLock *next_lock[MAX_NR_OF_DOUBLESUBDIRS*2+1],
                *current_lock = 0L,
                *first_lock;

int  i, nr_of_doublesubdirs;


main(argc,argv)
int argc;
char *argv[];
{
 char  *dirname, *dirname2, *drivename;
 struct FileLock *create_lock;
void die(), CloseALL();

 if(argc>1)
  drivename = argv[1];
 else
 {
  puts("MKDBLDIRS v2.0 : (c) 1987 by AMY PRODUCTIONS.\nSpecially developed for [EMD].\n");
  puts("Usage : MKDBLDIRS <drive> <nr_of_doublesubdirs> <subdir1_name> <subdir2_name>\n");
  puts("<drive>               : 'df1:', 'df2:' or 'df3:'");
  puts("<nr_of_doublesubdirs> : INTEGER NUMBER between [1-100], DEFAULT = 10");
  puts("<subdir1_name>        : CHAR STRING, DEFAULT = \"  * BAMIGA SECTOR ONE *  \"");
  puts("<subdir2_name>        : CHAR STRING, DEFAULT = \"  [BS ONE]  \"");
  puts("Note : All subdirs will be automagically protected !\n");
  exit(0);
 } 

 if(argc>2)
  {
   nr_of_doublesubdirs = atoi(argv[2]);
   if((nr_of_doublesubdirs <1) || (nr_of_doublesubdirs>MAX_NR_OF_DOUBLESUBDIRS))
   {
    puts("<nr_of_doublesubdirs> must be within [1-100] !\n");
    exit(0);
   }
  }
 else
  nr_of_doublesubdirs = 10;

 if(argc>3)
  dirname = argv[3];
 else
  dirname = "  * BAMIGA SECTOR ONE *  ";

 if(argc>4)
  dirname2 = argv[4];
 else
  dirname2 = "  [BS ONE]  ";

 /* ====== mkdirs routine ======  */

 /* init next_lock[] */
 for(i=0;i<=nr_of_doublesubdirs*2;i++) next_lock[i] = 0;

 first_lock = CurrentDir(0);
 CurrentDir(first_lock);

 /* lock drive df? */
 if( (current_lock = Lock(drivename,ACCESS_WRITE) ) == 0)
 {
  die("Couldn't get a lock on specified drive !\n");
 }

 /* next_lock (nl. next_lock[0]) word gelijkgesteld aan de lock op df1: */
 next_lock[0] = current_lock; /* let op next_lock[0] is een copy v.e. pointer
                                 en moet niet ge_unlocked worden */ 

 for(i=0;i<nr_of_doublesubdirs*2;i+=2)
 {
  CurrentDir(next_lock[i]);

  /* Test if "dirname" already exists ... */
  /* BUG : We test this by trying to obtain a lock on "dirname"
     but "dirname" could still be a normal file ! */
  if((create_lock = Lock(dirname,ACCESS_WRITE)) !=0)
  {
   UnLock(create_lock);
   die("Subdirectory already exists !\n");
  }

  if((create_lock = CreateDir(dirname)) == 0)
   die("Couldn't create subdirectory !\n");
  UnLock(create_lock);

  SetProtection(dirname,1);

  if((next_lock[i+1] = Lock(dirname,ACCESS_WRITE)) == 0)
   die("Couldn't obtain a lock on subdirectory\n");

 
  CurrentDir(next_lock[i+1]);

  /* Test if "dirname2" already exists ... */
  /* BUG : We test this by trying to obtain a lock on "dirname2"
     but "dirname2" could still be a normal file ! */
  if((create_lock = Lock(dirname2,ACCESS_WRITE)) !=0)
  {
   UnLock(create_lock);
   die("Subdirectory already exists !\n");
  }

  if((create_lock = CreateDir(dirname2)) == 0)
   die("Couldn't create subdirectory !\n");
  UnLock(create_lock);

  SetProtection(dirname2,1);

  if((next_lock[i+2] = Lock(dirname2,ACCESS_WRITE)) == 0)
   die("Couldn't obtain a lock on subdirectory\n");
   

 }

 CloseALL();

} /* ====== end main ====== */

void CloseALL()
{

 for(i=nr_of_doublesubdirs*2;i>=1;i--) if(next_lock[i]) UnLock(next_lock[i]);

 CurrentDir(first_lock);

 /* Lock op drive df? */
 if(current_lock) UnLock(current_lock);

} 

void die(errormsg)
char *errormsg;
{
 puts(errormsg);
 CloseALL();
 exit(0);
}
