/* Pro136i.c */
/* Automatic generation of a pair of DOS Batch Programs.               */
/* Read  'DIR.TXT'                                                     */
/* Write 'SDWBatch.TXT', 'ZIPBatch.TXT', and  'LookUp.Txt'             */

/* Since introduction of "/b (brief)" switch, no OS difference. */
/*    dir /b /on > C:\1Dual\DIR.txt                             */
/* Now,
	  1. Win3.1        [0]
	  2. Win95/98      [0]
	  3. WinXP         [0]
	  4. Win2000       [0]                      */
/* Regarding OS switch, Win95/98/2000/XP needs double quotes
   "directory name" for blank space, while Win3.1 shouldn't use
   double quotes.   But I haven't impremented Win3.1 here.  */


#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, JUMP_NO;
   /* i is "catlg000 couting up       */
   /* Jump_No used to use as 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;
   JUMP_NO=0;

/* 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("\nPlease type this working directory\'s drive name, colon(:), \n");
   printf("backslash(\\) and path. \n\n");
   printf("For example, \n");
   printf("           C:\\1temp [Enter] \n");
   printf("           D:\\Dual [Enter] \n");
   printf("           f:\\1dual [Enter] \n");
   printf("Type here: ");
   scanf("%s", &str);
   clrscr();
   printf("\nWorking Directory is set to \"%s\"\n\n", str);
/* ======================================================= */


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

   fgets(inline, 256, infp);    /* The 1st one starts without ..(go up) */
   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_FLASH  =================*/

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

   fgets(inline, 256, infp);    /* The 1st one starts without ..(go up) */
   spacedwd(id, &inline[JUMP_NO]);
   fprintf (outfp2, "cd \"%s\"\n", id);
   fprintf (outfp2, "dir /s /on > %s\\ZIP_FL\\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_FL\\catlg00%d.Txt\n", str, i);
		 if (i>=10 && i<=99)   fprintf (outfp2, "dir /s /on > %s\\ZIP_FL\\catlg0%2d.Txt\n", str, i);
		 if (i>=100 && i<=999) fprintf (outfp2, "dir /s /on > %s\\ZIP_FL\\catlg%3d.Txt\n",  str, i);
		 i++;
   }
   fclose (infp);
   fclose (outfp2);
   i=0; /* reset */


/* =====   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;

   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);



   printf("\n\nType any character key and [ENTER] to close this message: ");
   scanf("%s", &strPause);

}