/* Pro136F.c */
/* "6Rename.Exe" */
/* Automatic Rename Batch generator as a Finalizing polish.
	  The Batch will return Original Long foldernames.        */
/* Read  'LOOKUP.TXT'                           */
/* Write 'RENAME.BAT'                           */
/* This C program is a variation of 'Pro136C.c' which automatically
	  generate DOS-Batch program after it captures Directory names.  */
/* (2005-02-06) I realized that I can't make the RENAME.BAT
 *  directly from "DIR.TXT", but I should read from "LOOKUP.TXT"
 *  which tells,
 *    catlg001.txt  <=>  The First Long filename folder.
 *    catlg002.txt  <=>  The second Foldername Original.
 *    catlg003.txt  <=>  The Third Foldername of Scanning.
 *  Then, I can grab both the Catalog number and its Original name
 *  simultaneously, so that I can write "RENAME.Bat".
 *  So, I will re-structure this to,
 *    1. Eliminate Reading "DIR.Txt", to write "Header.Txt"
 *    2. Eliminate Reading "DIR.Txt", to write "OUT-Head.Txt"
 *    3. Read "LOOPUP.Txt", write "RENAME.Bat".
 */
/* However, making "LOOKUP.TXT" will be a little complicated, because
 *  I want to introduce the Switch of filename column positions.
 *    1.  Win3.1                   [0]
 *    2.  Win95/98, WinNT likely  [44]
 *    3.  WinXP                   [39]
 *  Right now, I am thinking optional 1, 2, 3 switch.
 *  Also I need to keep the ROOT and SUB-DIRECTORY difference switch.
 */

#include <stdio.h>
#include <string.h>

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

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

void main(void){
   char strPause[16];
   FILE *infp, *outfp3;
   /*  Actually, "LOOKUP.TXT" is simpler */
   /*  to deal with, becuase it should have been already processed */
   /*  and designed to be read here. */

   printf("This makes Rename Batch files \"Rename.Bat\".  While this \n");
   printf("program is written in an old C-Compiler, and couldn't handle \n");
   printf("long filenames, by the help of \"LoopUp.Txt\" and DOS-Batch \n");
   printf("executable file, we can rewrite all cataglos and DIFF reports back \n");
   printf("to orignal folder names, if you wish.  This is an optional step \n");
   printf("for our convenience.  To do this, double click \"Rename.Bat\".\n\n");
   printf("If it is not created, check if 'LookUp.Txt' is in this temporary \n");
   printf("directory.\n\n");
   printf("Type any character key and [ENTER] to close this message: ");
   scanf("%s", &strPause);

/* ========  THIS WAS THE THIRD SET, SUDDENLY THE FIRST OUTPUT FILE ============= */

   infp=fopen("LOOKUP.Txt", "r");
   outfp3=fopen("RENAME.BAT", "w");

/* At here, I need a switch of Five, SDW, ZIP, SDW\Erased, ZIP\Erased, DIFF  */
/* If I make a Sub-routine, only Five times of directory changes, and        */
/*  doing many Renamings.                                                    */
/* Alternatively, I can write a set of Five RENAME here at different locations, */
/*  while RENAME command lines are jumping around Five folders all the time. */
/*  by "Change Directory (CD)".  I tried the latter way and it is just fine. */
/*  Without Changing Directory, the DOS-Batch command didn't work.           */

   /* Only the First Line, it is current directory, no "CD .." at first. */
   fgets(inline, 256, infp);
   firstwd(id, &inline[2]);  spacedwd(originalName, &inline[18]);
   fprintf (outfp3, "CD SDW\n");
   fprintf (outfp3, "RENAME %s.Txt   \"%s.Txt\"\n", id, originalName);
   fprintf (outfp3, "CD ..\\ZIP\n");
   fprintf (outfp3, "RENAME %s.Txt   \"%s.Txt\"\n", id, originalName);
   fprintf (outfp3, "CD ..\\SDW\\Erased\n");
   fprintf (outfp3, "RENAME %s.Txt   \"%s.Txt\"\n", id, originalName);
   fprintf (outfp3, "CD ..\\..\\ZIP\\Erased\n");
   fprintf (outfp3, "RENAME %s.Txt   \"%s.Txt\"\n", id, originalName);
   fprintf (outfp3, "CD ..\\..\\DIFF\n");
   fprintf (outfp3, "RENAME %s.Txt   \"%s.Txt\"\n", id, originalName);

   /* The Second Line and Later */
   while(fgets(inline, 256, infp)!=NULL){
		 firstwd(id, &inline[2]);  spacedwd(originalName, &inline[18]);
		 fprintf (outfp3, "CD ..\\SDW\n");
		 fprintf (outfp3, "RENAME %s.Txt   \"%s.Txt\"\n", id, originalName);
		 fprintf (outfp3, "CD ..\\ZIP\n");
		 fprintf (outfp3, "RENAME %s.Txt   \"%s.Txt\"\n", id, originalName);
		 fprintf (outfp3, "CD ..\\SDW\\Erased\n");
		 fprintf (outfp3, "RENAME %s.Txt   \"%s.Txt\"\n", id, originalName);
		 fprintf (outfp3, "CD ..\\..\\ZIP\\Erased\n");
		 fprintf (outfp3, "RENAME %s.Txt   \"%s.Txt\"\n", id, originalName);
		 fprintf (outfp3, "CD ..\\..\\DIFF\n");
		 fprintf (outfp3, "RENAME %s.Txt   \"%s.Txt\"\n", id, originalName);
   }
   fclose (infp);
   fclose (outfp3);

}