/* Pro136C.c */
/* Automatic generation of a pair of DOS Batch Programs.               */
/* This 'Pro136C.c' has a couple of versions, history backs to
	  1st one is for Linux with DQ (double quote) (a few weeks old).
	  2nd one is this for Windows-&-C, automatic All fileNames [].     */
/* Read  'DIR.TXT'                                                     */
/*    This 'DIR-01.TXT' must be make Win95, or Win98.
	  Since it takes a full name of directory at Column 45.
	  There is nothing if I use Win3.1 .        */
/* Write 'SDWBatch.TXT' and
		 'ZIPBatch.TXT'                         */
/*
 *		   10        20        30        40        50
 * 1...5....0....5....0....5....0....5....0....5....0
 * KMAIL          <DIR>        04-27-04  1:07a KMAIL.003
 * TO-CD          <DIR>        04-29-04  6:13p TO-CD.005
 * Therefore, directory name Magic Number for memory is '44'.
 */
/* (2005-01-25) I introduced different numbers of line eating,
 *    switched by ROOT's or SUB-FOLDER's "DIR.TXT".
 *    Also cleaned up "mkdir SDW(ZIP)".   One more thing
 *    I can do is "Missing DIR.TXT" notice, as I did in Mac.
 *    since this is the first program in need of "DIR.TXT".
 */

#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <dos.h>
#include <stdlib.h>

char inline [256];
char id [128];

extern char *firstwd (char *to, char *from);   /* Recycling Object 'firstwd()' */
/* OR */
// extern char *firstwd (char *, char *);

void main(void){
   char str[64], strPause[16];
   FILE *infp1, *infp2, *outfp1, *outfp2;

/* The following codes infp=fopen() is out of place, but COPIED UP HERE   */
/* Because, missing notice "Dir.Txt" should be earlier than user's input. */
/* See below the Origirnal place of to read the codes.                    */
/* The following ";" is meant to continue.                */
   if (  (infp1=fopen("DIR.txt", "r"))!= NULL )
	  ;
   else{
	  printf("!!!  Please put a \"Dir.Txt\" file about your disk\'s directory information.\n");
	  printf("!!!  Please refer to an introductory .PDF about \"Dir.Txt\". \n");
	  printf("!!!  And do this again. \n\n");
	  exit (0);
   }



   printf("Did you creat a new temporary directory?  And runing this \n");
   printf("from there.  As far as a not-crowded directory, fine.  Please\n");
   printf("double check a set of all executable files are here.  They are,\n\n");
   printf("  1stBatch.bat - we did already, making a frame of directories.\n");
   printf("  2Batch.exe   - this one running, will make a pair of Batch files.\n");
   printf("  3Header.exe  - this one create two header files.\n");
   printf("  4Eraser.exe  - Date-Time Stamps are erased for easy comparison.\n");
   printf("  5Diff.exe    - finally, Dual System's contents are examined.\n\n");
   printf("Are you ready to go?   If not, exit by [Ctrl]-key + [C]-key.\n\n");
   printf("Type Your Temporary Directory Path:  C:\\");
   scanf("%s", &str);
   clrscr();
/* ======================================================= */
   printf("Your Temporary Directory is set to \"C:\\%s\"\n\n", str);
   printf("Now, This '2Batch.exe'will be making a pair of Batch files for you.\n");
   printf("They are, \n");
   printf("     SDWBatch.TXT \n");
   printf("     ZIPBatch.TXT \n");
   printf("Once they will be created, you can edit as you like.  For example, \n");
   printf("certain directory have a lot of folders and files, and at the \n");
   printf("first test run, you may want to skip that directory.  Then you will\n");
   printf("erase the relevant two lines about the directory.  Or at another \n");
   printf("directory, you hit an error due to a disk damage and stop there. \n");
   printf("Then delete that two lines, or all job done lines, and continue \n");
   printf("the rest, so on.   Please use these batch files as flexible as \n");
   printf("your own.\n");
//   sleep (20);
   printf("Type any character key and [ENTER] to continue: ");
   scanf("%s", &strPause);
   clrscr();
/* =======================================================*/
//   printf("You typed %s-key, here we go.\n\n", strPause);
   printf("These two DOS-Batch Programs must be run from the right places.\n");
   printf("Otherwise, they don't function.  For this reason, tell you the \n");
   printf("mechanics, then you can figure it out.  If you are not familiar with  \n");
   printf("MS-DOS command window (black screen), and you want to try a test, \n");
   printf("do the following.  Exit this progam now by ([Ctrl]+[C]-keys), and \n");
   printf("go [Start]-> [Program]-> [MS-DOS Prompt].  Then type 'dir' there,  \n");
   printf("   C:\>Windows\\dir    [ENTER-key] \n");
   printf("You will see all the contents listed up and rolled up.  After \n");
   printf("this test, please come back here by running this '2Batch.Exe' again.\n");
   printf("This 'dir' command is the most important one to view the contents \n");
   printf("of the current directory.  Another most important command is 'cd' \n");
   printf("(change directory).  By this 'cd' you can go upper directory \n");
   printf("[cd ..], or lower directory by its name, for example if there is  \n");
   printf("'DOCS' directory, you can go down there by [cd DOCS]. By using\n");
   printf("these two commands 'dir' and 'cd', this DOS-Batch program \n");
   printf("will make contents listing as files under \\SDW and \\ZIP.\n\n");
   printf("Type any character key and [ENTER] to continue: ");
   scanf("%s", &strPause);
   clrscr();
/* =======================================================*/
//   printf("You typed %s-key, here we go.\n\n", strPause);

/* Original Place, missing "Dir.Txt" notice               */
/* The following ";" is meant to continue.                */
//   if (  (infp1=fopen("DIR.txt", "r"))!= NULL )
//	  ;
//   else{
//	  printf("!!!  Please put a \"Dir.Txt\" file about your directory information.\n");
//	  printf("!!!  And do this again. \n\n");
//	  exit (0);
//   }

   outfp1=fopen("SDWBatch.TXT", "w");

   /* Here I eat first SEVEN lines, recognizing. */
   /* The following FIVE lines are just beeing eaten, because */
   /* When IF-loop is true (Continue); When IF-loop isn't true (Continue) */
   /* Thus, they are merely decorative effects. */
   fgets(inline, 256, infp1); if(strncmp(inline, "\n", 1)==0);
   fgets(inline, 256, infp1); if(strncmp(inline, " Volume in", 10)==0);
   fgets(inline, 256, infp1); if(strncmp(inline, " Volume Serial", 14)==0);
   fgets(inline, 256, infp1); if(strncmp(inline, " Directory of", 13)==0);
   fgets(inline, 256, infp1); if(strncmp(inline, "\n", 1)==0);
   /* ROOT or SUB-FOLDER "DIR.TXT" switch */
   fgets(inline, 256, infp1); if(strncmp(inline, ". ", 2)==0)
	  {
									   /* This is in the IF-loop of ".", ZIP's SUB-DIRECTORY "Dir.Txt" */
		  fgets(inline, 256, infp1);   /* Eat one more line of ".." */
		  fgets(inline, 256, infp1);   /* Since the following First one is commented out, this line becomes the first one in IF-true loop */
	  }else{                           /* ELSE implies NOT ".", likely ZIP's ROOT "Dir.Txt" */
		  ;
	  }

//   fgets(inline, 256, infp1);    /* The 1st one starts without ..(go up) */
   firstwd(id, &inline[44]);
   fprintf (outfp1, "cd %s\n", id);                /* id = directories in Master ZIP */
   fprintf (outfp1, "dir /s /on > C:\\%s\\SDW\\%s\n", str, id);
								 /* The 2nd one and later need ..(go up) */
   while(fgets(inline, 256, infp1)!=NULL){
		 if(strncmp(inline, "      ", 6)==0)break;      /* To deal the last two incoming lines */
		 firstwd(id, &inline[44]);
		 fprintf (outfp1, "cd ..\\%s\n", id);
		 fprintf (outfp1, "dir /s /on > C:\\%s\\SDW\\%s\n", str, id);
   }
   fclose (infp1);
   fclose (outfp1);

/* ===========   ANOTHER SET, ZIP  =================*/

   infp2=fopen("DIR.txt", "r");
   outfp2=fopen("ZIPBatch.TXT", "w");

   /* Here I eat first SEVEN lines, recognizing. */
   fgets(inline, 256, infp2); if(strncmp(inline, "\n", 1)==0);
   fgets(inline, 256, infp2); if(strncmp(inline, " Volume in", 10)==0);
   fgets(inline, 256, infp2); if(strncmp(inline, " Volume Serial", 14)==0);
   fgets(inline, 256, infp2); if(strncmp(inline, " Directory of", 13)==0);
   fgets(inline, 256, infp2); if(strncmp(inline, "\n", 1)==0);
   /* ROOT or SUB-FOLDER "DIR.TXT" switch */
   fgets(inline, 256, infp1); if(strncmp(inline, ". ", 2)==0)
	  {
									   /* This is in the IF-loop of ".", ZIP's SUB-DIRECTORY "Dir.Txt" */
		  fgets(inline, 256, infp1);   /* Eat one more line of ".." */
		  fgets(inline, 256, infp1);   /* Since the following First one is commented out, this line becomes the first one in IF-true loop */
	  }else{                           /* ELSE implies NOT ".", likely ZIP's ROOT "Dir.Txt" */
		  ;
	  }


//   fgets(inline, 256, infp2);    /* The 1st one starts without ..(go up) */
   firstwd(id, &inline[44]);
   fprintf (outfp2, "cd %s\n", id);
   fprintf (outfp2, "dir /s /on > C:\\%s\\ZIP\\%s\n", str, id);
								 /* The 2nd one and later need ..(go up) */
   while(fgets(inline, 256, infp2)!=NULL){
		 if(strncmp(inline, "      ", 6)==0)break;      /* To deal the last two incoming lines */
		 firstwd(id, &inline[44]);
		 fprintf (outfp2, "cd ..\\%s\n", id);
		 fprintf (outfp2, "dir /s /on > C:\\%s\\ZIP\\%s\n", str, id);
   }
   fclose (infp2);
   fclose (outfp2);




   printf("Now, you can exit this program by ([Ctrl]+[C]-keys) to look at \n");
   printf("inside of 'SDWBatch.TXT' and 'ZIPBatch.TXT'.  When exit now, \n");
   printf("please come back to read through one more page down below by \n");
   printf("running this '2Batch.exe' again.\n");
   printf("Suppose your Master ZIP disk have directories at root, for example \n");
   printf("'DOCS', 'EMAILS', 'GRAPHICS' so on.   Then the Batch files should\n");
   printf("look like, \n");
   printf("     cd DOCS \n");
   printf("     dir \/s \/on > C:\\1temp\\SDW\\DOCS \n");
   printf("     cd ..\\EMAILS \n");
   printf("     dir \/s \/on > C:\\1temp\\SDW\\EMAILS \n");
   printf("     cd ..\\GRAPHICS \n");
   printf("Type any character key and [ENTER] to continue: ");
   scanf("%s", &strPause);
   clrscr();
/* =======================================================*/
//   printf("You typed %s-key, here we go.\n\n", strPause);
   printf("Next, you need to run these Batch programs at the right places.\n");
   printf("The directories names appeared in the Batch file should match \n");
   printf("where these directories exist.  So, please do the below.\n");
   printf("Please bring 'SDWBatch.TXT' to Hard disk's Shadow(SDW)-directory \n");
   printf("and 'ZIPBatch.TXT' to the Master ZIP disk's directory accordingly.\n\n");
   printf("Once they are brought at the right place, change .TXT to .BAT.\n");
   printf("The looks of icons change.  Double clicks it.  Please go and \n");
   printf("see if any files are created at \\SDW and \\ZIP under your \n");
   printf("temporary working directory.  If not, a likely reason is the\n");
   printf("misplaced Batch files.  So, please read the above one more \n");
   printf("time by running this program again.\n");


//   printf("You typed %s-key, here we go.\n\n", strPause);
}