/* Pro136H.c */
/* "2Batch.Exe" + "3Lookup.Exe" (formerly "3Header.Exe") */
/* I combined these two, for one time OS swtich of "DIR.TXT".          */
/* Automatic generation of a pair of DOS Batch Programs.               */
/* The History of this predecessor 'Pro136C.c' goes back all the way
   to Linux-PERL proramming.  PERL is so easy, but it's Shell and Script
   so that Portability is poor than compiled C.  I developed All
   filenames access protocol there in PERL.
	  1st by DQ (double quote).
	  2nd All fileNames Array [].
	  Then "Header.Txt" file, OR "Lookup.Txt"
	  They are preprocessed according to "DIR.TXT"                     */
/* Read  'DIR.TXT'                                                     */
/* Write 'SDWBatch.TXT', 'ZIPBatch.TXT' and
		 'LookUp.Txt'                         					*/

/* Automatic generation of "LOOKUP.Txt"     	*/
/* Read  'DIR.TXT'                              */
/* Depening on where 'DIR.TXT' is made, there is a switch.
	  1. Win3.1        [0]
	  2. Win95/98     [44]
	  3. WinXP        [39]                      */
/* I tried the 1, 2, 3 Switch here in this Pro136G.c, and it truns
	out excellent.                              */
/* Write 'LOOKUP.TXT' 			                */
/* Regarding the begining part of "DIR.TXT":
   Both 2.(Win95/98) and 3.(WinXP) have Five Lead lines to be eaten.
   while 1.(Win3.1) have only Four Lead lines.
   Right now this program looks like recognizing Five Lead lines,
   but actually not, this is eating whatever First Five lines.
   So, it practically only matters the line numbers, not the
   content.   Thus, Win95/98/XP are completely compatible, while
   Win3.1 have a trouble (one data line eating).    */

/* I found another problem in Win3.1 during making Batch files.
   Since I need to change to the folder by CD ..\folder_name.
   but I can't grab the folder_name directly from "Dir.Txt"
   in the case of Win3.1.  I have to parse the name from
	  FoldName Abc  -> to   FoldName.Abc
   then, I can apply to it to Change Directory.  This is
   too much, because it requires too many switches all around, besides,
   the only user of Win3.1 is Me, and I am not running "PCDualWay.Exe"
   in Win3.1 except during Programming.   So, let me drop Win3.1
   from the option.                                  */

/* (2005-01-25) I introduced different numbers of line eating,
 *    switched by ROOT's or SUB-FOLDER's "DIR.TXT".
 *    However, this is about Win95/98.  I haven't examined at
 *    Win3.1;  WinXP.   Better check out.
 */
/* (2005-02-08) I combined this "2Batch.Exe" with the next "3Lookup.Exe"
 *    Otherwise I have to ask twice the OS version to switch here and there.
 *    This is stupid, because both this "2Batch.Exe" and that
 *    "3Lookup.Exe" are simple in its structure, so just
 *    arrage both output tandem.   I did.
 */
/* (2005-06-04) I increased 1-99 folders to 1-999 folders. */

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

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

extern char *spacedwd (char *to, char *from);   /* Recycling Object  */

void main(void){
   char str[64], strPause[16];
   int i, OS, JUMP_NO;
   /* i is "catlg00x couting up;  OS is OS switch;
	  Jump_No is a kind of MagicNumber depend on OS version.      */
   FILE *infp, *outfp1, *outfp2, *outfp3;
   /* infp reads "Dir.Txt" (three times)                          */
   /* outfp1 writes "SDWBatch.Txt", outfp2 writes "ZIPBatch.Txt"  */
   /* outpf3 writes "LookUp.Txt"                                  */
   i=1;

/* 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 (  (infp=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");
	  printf("Type any character key and [ENTER] to close this message: ");
	  scanf("%s", &strPause);
	  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("  3Eraser.exe  - Date-Time Stamps are erased for easy comparison.\n");
   printf("  4Diff.exe    - finally, Dual System's contents are examined.\n");
   printf("  5Rename.exe  - to review catalogs by directory name, use this.\n\n");
   printf("Are you ready to go?   If not, exit by [Ctrl]-key + [C]-key.\n\n");
   printf("Please type your temporary directory path below, starting from \n");
   printf("Drive Name, such as C:\\test, D:\\DualWay ..etc., and [Enter]. \n");
   printf(" ");
   scanf("%s", &str);
   clrscr();
   printf("Your Temporary Directory is set to \"%s\"\n\n", str);
/* ======================================================= */
   printf("Since different OS makes slightly different \"DIR.TXT\", \n");
   printf("please select the OS version where you made \"DIR.TXT\". \n\n");
   printf("  1. Win95\/Win98\n");
   printf("  2. WinXP\n\n");
   printf("Select Your OS verions by Number [1 or 2] : ");
   scanf("%d", &OS);
   clrscr();
   if (OS==1) JUMP_NO=44;
   if (OS==2) JUMP_NO=39;
/* ======================================================= */


   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 (  (infp=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, infp); if(strncmp(inline, "\n", 1)==0);
   fgets(inline, 256, infp); if(strncmp(inline, " Volume in", 10)==0);
   fgets(inline, 256, infp); if(strncmp(inline, " Volume Serial", 14)==0);
   fgets(inline, 256, infp); if(strncmp(inline, " Directory of", 13)==0);
   fgets(inline, 256, infp); if(strncmp(inline, "\n", 1)==0);
   /* ROOT or SUB-FOLDER "DIR.TXT" switch */
   fgets(inline, 256, infp); if(strncmp(inline, ". ", 2)==0)
	  {
									   /* This is in the IF-loop of ".", ZIP's SUB-DIRECTORY "Dir.Txt" */
		  fgets(inline, 256, infp);   /* Eat one more line of ".." */
		  fgets(inline, 256, infp);   /* 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, infp);    /* The 1st one starts without ..(go up) */
								   /* Now, the 1st one is taken over by ROOT or SUB-FOLDER switch above. */
   spacedwd(id, &inline[JUMP_NO]);
   fprintf (outfp1, "cd \"%s\"\n", id);                /* id = directories in Master ZIP */
   fprintf (outfp1, "dir /s /on > %s\\SDW\\catlg00%d.Txt\n", str, i++);
								 /* The 2nd one and later need ..(go up) */
   while(fgets(inline, 256, infp)!=NULL){
		 if(strncmp(inline, "      ", 6)==0)break;      /* To deal the last two incoming lines */
		 spacedwd(id, &inline[JUMP_NO]);
		 fprintf (outfp1, "cd ..\\\"%s\"\n", id);
		 if (i<=9)             fprintf (outfp1, "dir /s /on > %s\\SDW\\catlg00%d.Txt\n", str, i);
		 if (i>=10 && i<=99)   fprintf (outfp1, "dir /s /on > %s\\SDW\\catlg0%2d.Txt\n", str, i);
		 if (i>=100 && i<=999) fprintf (outfp1, "dir /s /on > %s\\SDW\\catlg%3d.Txt\n",  str, i);
		 i++;
   }
   fclose (infp);
   fclose (outfp1);

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

   i=1;
   infp=fopen("DIR.txt", "r");
   outfp2=fopen("ZIPBatch.TXT", "w");

   /* Here I eat first SEVEN lines, as if recognizing. */
   fgets(inline, 256, infp); if(strncmp(inline, "\n", 1)==0);
   fgets(inline, 256, infp); if(strncmp(inline, " Volume in", 10)==0);
   fgets(inline, 256, infp); if(strncmp(inline, " Volume Serial", 14)==0);
   fgets(inline, 256, infp); if(strncmp(inline, " Directory of", 13)==0);
   fgets(inline, 256, infp); if(strncmp(inline, "\n", 1)==0);
   /* ROOT or SUB-FOLDER "DIR.TXT" switch */
   fgets(inline, 256, infp); if(strncmp(inline, ". ", 2)==0)
	  {
									   /* This is in the IF-loop of ".", ZIP's SUB-DIRECTORY "Dir.Txt" */
		  fgets(inline, 256, infp);   /* Eat one more line of ".." */
		  fgets(inline, 256, infp);   /* 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, infp);    /* The 1st one starts without ..(go up) */
								   /* Now, the 1st one is taken over by ROOT or SUB-FOLDER switch above. */
   spacedwd(id, &inline[JUMP_NO]);
   fprintf (outfp2, "cd \"%s\"\n", id);
   fprintf (outfp2, "dir /s /on > %s\\ZIP\\catlg00%d.Txt\n", str, i++);
								 /* The 2nd one and later need ..(go up) */
   while(fgets(inline, 256, infp)!=NULL){
		 if(strncmp(inline, "      ", 6)==0)break;      /* To deal the last two incoming lines */
		 spacedwd(id, &inline[JUMP_NO]);
		 fprintf (outfp2, "cd ..\\\"%s\"\n", id);
		 if (i<=9)             fprintf (outfp2, "dir /s /on > %s\\ZIP\\catlg00%d.Txt\n", str, i);
		 if (i>=10 && i<=99)   fprintf (outfp2, "dir /s /on > %s\\ZIP\\catlg0%2d.Txt\n", str, i);
		 if (i>=100 && i<=999) fprintf (outfp2, "dir /s /on > %s\\ZIP\\catlg%3d.Txt\n",  str, i);
		 i++;
   }
   fclose (infp);
   fclose (outfp2);
   i=0; /* reset */




   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\\catlg001.Txt \n");
   printf("     cd ..\\EMAILS \n");
   printf("     dir \/s \/on > C:\\1temp\\SDW\\catlg002.Txt \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\n");
   printf("Type any character key and [ENTER] to close this message: ");
   scanf("%s", &strPause);
//   printf("You typed %s-key, here we go.\n\n", strPause);



/* =====   ANOTHER OUTPUT, NOT BATCH FILE BUT LOOKUP.TXT  =============*/
/* =====   Making "LOOKUP.TXT" file for '4Eraser.exe' and
			'5Diff.exe', behind the scene.  ===========================*/


   infp=fopen("DIR.txt", "r");
   outfp3=fopen("LOOKUP.TXT", "w");
   i=1;

   /* Here I eat first SEVEN lines, just lightly viewing, and eating. */
   /* The following FIVE lines are just beening eaten, because */
   /* When IF-loop is true (Continue); When IF-loop isn't true (Continue) */
   /* Thus, they are merely decorative effects. */
   fgets(inline, 256, infp); if(strncmp(inline, "\n", 1)==0);
   fgets(inline, 256, infp); if(strncmp(inline, " Volume in", 10)==0);
   fgets(inline, 256, infp); if(strncmp(inline, " Volume Serial", 14)==0);
   fgets(inline, 256, infp); if(strncmp(inline, " Directory of", 13)==0);
   fgets(inline, 256, infp); if(strncmp(inline, "\n", 1)==0);
   /* The ROOT or SUB-FOLDER "DIR.TXT" Switch. */
   fgets(inline, 256, infp); if(strncmp(inline, ". ", 2)==0)  /* ROOT: This one is the actual first inline data (or in ELSE-loop as I indicated) */
	  {
									   /* This is in the IF-loop of ".", ZIP's SUB-DIRECTORY "Dir.Txt" */
		  fgets(inline, 256, infp);   /* Eat one more line of ".." */
		  fgets(inline, 256, infp);   /* SUB-FOLDER:  This line is actual the first data line in IF-true loop, SUB-FOLDER case */
	  }else{                           /* ELSE implies NOT ".", likely ZIP's ROOT "Dir.Txt", so the before IF-loop's inline is still active. */
		  ;
	  }
   /* Now, DO the process immidiately since the first inline data is   */
   /* alread in the Pointer infp=>inline.                              */
   /* The following two lines are for the first data inline            */
   spacedwd(id, &inline[JUMP_NO]);     /* Jump Swtich for Win95/98/XP  */
   fprintf (outfp3, "   catlg00%d  <=>  %s\n", i++, id);

   while(fgets(inline, 256, infp)!=NULL){
		 if(strncmp(inline, "      ", 6)==0)break;      /* To deal the last two incoming lines */
		 spacedwd(id, &inline[JUMP_NO]);
		 if (i<=9)             fprintf (outfp3, "   catlg00%d  <=>  %s\n", i, id);
		 if (i>=10 && i<=99)   fprintf (outfp3, "   catlg0%2d  <=>  %s\n", i, id);
		 if (i>=100 && i<=999) fprintf (outfp3, "   catlg%3d   <=>  %s\n", i, id);
		 i++;
   }
   i=0; /* reset */
   fclose (infp);
   fclose (outfp3);

}