/* Pro136C.c */
/*    Automatic generation of a pair of DOS Batch Programs.            */
/* This 'Pro136C.c' has two versions,
	  1st one is for Linux with DQ (double quote) (a few weeks old).
	  2nd one is this for Windows-&-C, automatic All fileNames [].
		The 2nd version also has fine-tuning for the First Line of
		'DIR-01.TXT'.                                                  */
/* Read  'DIR-01.TXT'                           */
/*    This 'DIR-01.TXT' must be make Win95, Win98 or later.
	  Check there is a full name of directory at Column 45.
	  There is nothing if I use Win3.1 .        */
/* Write 'SDWBatch.TXT' and
		 'ZIPBatch.TXT'                         */
/* This C program replaces the previous Two Steps,
	  PERL 'scr38.pl'    AND
	  'Pro136.c'.    (Much easier now, because 'Dir-01.txt' is in PC.) */
/*
 *		   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'.
 */
/* 2004-09-12 Note:  The Improvement in this 'Pro137.c'.
	  For convenience of Global Replacement (later at Linux),
	  to be Double Quoted all filenames in @fileNames (
	  Character String Array in PERL Header file 'scr37.ph'), all
	  filenames should have the same character at first and at end.
	  So, I decided to use DQ. (double quote) at first, and
	  .txt at the end.   Practically speaking, during import from
	  PC to Linux, all filenames are appended Asterisk (*), so
	  that, the end of filenames don't have to ajust.   I can
	  replace Asterisk (*) to Double Quote Comma(",).  Just I keep
	  the convention of .TXT as a text file in PC.                     */
/* As I am getting into 'Asterisk(*)' and 'Ampersand(&)' in C,
 *    I am gradually understanding that I should address more
 *    on MEMORY, location and value.  Then, I can write a gabage
 *    free C-programs.   Here is the combination from Dr. Aota's
 *    code of 'firstwd'.   This 'firstwd' routine doesn't look
 *    elegant, but works so well.
 */
/* I saw here that I can take care the directories PATHs
 *    at here too, such as \SDW\, \ZIP\,  and
 *    \SDW\Result\,  \ZIP\Result\.
 *    This 'Pro136C.exe' is going to revise to capture the
 *    user's desired Directory Path, and the automation at here
 *    is easier.  But I want a user to have an empty
 *    temporary working folder, for this reason, not too much
 *    automation, but a lot of comments like '1stBatch.bat'
 *    I did this morning are a good idea to make a user
 *    familiarize about this.  Especially, directory
 *    structure is the frame to review the results.
 */

#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <dos.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;


   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 directories.\n");
   printf("Otherwise, they don't function.  For this reason, tell you the \n");
   printf("mechanics, then you can figure out.  Exit this program now to \n");
   printf("try MS-DOS command window (black screen), if you want ([Ctrl]+[C]-keys). \n");
   printf("And then type 'dir' there.  And come back here again.\n");
   printf("   C:\>Windows\\dir  [then ENTER-key] \n");
   printf("You will see all the contents listed up and rolled up. \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);



   infp1=fopen("DIR.txt", "r");
   outfp1=fopen("SDWBatch.TXT", "w");

   /* Here I eat first SEVEN lines, recognizing. */
   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);
   fgets(inline, 256, infp1); if(strncmp(inline, ". ", 2)==0);
   fgets(inline, 256, infp1); if(strncmp(inline, "..", 2)==0);


   fgets(inline, 256, infp1);    /* The 1st one starts without ..(go up) */
   firstwd(id, &inline[44]);
   fprintf (outfp1, "mkdir C:\\%s\\SDW\n\n", str);     /* Duplcated with '1stBatch.bat, but it's O.K. */
   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);
   fgets(inline, 256, infp2); if(strncmp(inline, ". ", 2)==0);
   fgets(inline, 256, infp2); if(strncmp(inline, "..", 2)==0);


   fgets(inline, 256, infp2);    /* The 1st one starts without ..(go up) */
   firstwd(id, &inline[44]);
   fprintf (outfp2, "mkdir C:\\%s\\ZIP\n\n", str);
   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 here 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("The directories names appeared in the Batch file should match \n");
   printf("the location in which these directories exist.  At there you \n");
   printf("you can run these Batch program.\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");


//   sleep (20);


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