/* gccPj002_4.c */
/* = Kdo.c + gccPj002.c (MacOSX) */
/* 2Er - executable file posted  */

/*  (2005-03-29)  Now, I am getting into Kdo_0[1-4].c codes.   
 *  The file pointer "Stream" is local within OpenAnyFile 
 *  subroutine, and it is renamed in main.c as the file pointer "file".
 *
 *  It was originally, 
 *   main (int argc, char **argv){
 *       file = OpenAnyFile (agrv[1]);
 *   }
 *  to take the argv[1] as the user's input file name.   I only 
 *  need to get the user's fixed filename "HEADER.TXT" or 
 *  "header.txt" so on, thus, it becomes here, 
 *       file = OpenAnyFile ("Header.Txt");
 *  And this works well. 
 */

/* (2005-04-01)  The whole "gccPj002.c" inserted into Kdo's main.c, and 
 *    then "Header.Txt" is Case-Insensitive.   File Pointer "file" 
 *    is adjusted to "infpSDW", "infpZIP", that's all.
 */
/* (2005-04-05)  Importing into the whole codes into "Kdo.c" is easy, 
 *    but multiple switch about Different Field(s) Erasing needs a 
 *    a careful writing in the ELSE-LOOP of Erasing Process.
 *    In that ELSE-LOOP, I need
 *      if (Opt 1) - Fld 1     :  Objs  fild_1up.o  fild_1dw.o 
 *      if (Opt 2) - Fld 1+4   :  Objs  fild_2up.o  fild_2dw.o
 *      if (Opt 3) - Fld 1+3+4 :  Objs  fild_3up.o  fild_3dw.o
 *    etcetra.
 *    Thus, the Erasing Process becomes multiple branching options.
 *    It goes to a bunch of Recycling Objects, so write the Makefile
 *    and do the compilation by "make".
 */

/* Let me test, OPTION NO 1, if works, fill the rest */
/* It's working, so go for the rest.                 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <dirent.h>

char line [256];
char upField [128], downField [128];
char FNToOpen [64];  /* = filenamesToOpen */
char openedFN [64];  /* = opendFilename */
char openedFNP [64]; /* = opendFilenameWithPath */
char writingFile [64];
int  OPTION, ERASE_NO;  /* ERASE_No is a MagicNumber */


/* Following Two(2) Subroutines from Kdo.c */
FILE *OpenAnyFile (char *FileName);
void *strupr (void *buffer);   

extern char *spacedwd (char *to, char *from);   
 /* = spaced words, to capture blank-containing foldername         */
 /* In UNIX_Linux, an user prefers no blank file and folder names. */
 /* But let me see for a little while.                             */

/* Following Two(2) Objects are in "gccPj002.c", then here modified.  */
// extern char *fild_no1  (char *to, char *from);   /* Recycling Object */
// extern char *fild_no2  (char *to, char *from);   /* Recycling Object */

extern char *fild_1up  (char *to, char *from);   /* Target Feild_No 1 */
extern char *fild_1dw  (char *to, char *from);   /*   "   */
extern char *fild_4up  (char *to, char *from);   /* Target No. 4 */
extern char *fild_4dw  (char *to, char *from);   /*   "   */
extern char *fild_7up  (char *to, char *from);   /* Target No. 3+4 */
extern char *fild_7dw  (char *to, char *from);   /*   "   */


main ()
{
  int JUMP_NO;
//  FILE *file;
  FILE *infpSDW, *infpZIP;
  JUMP_NO=0;

  infpSDW = OpenAnyFile ("header.txt");
  if (infpSDW)
     printf ("\nReading Header.txt...\n");
  else{
//     perror ("Open failed");
       printf("\n!!!!!   Please put a \"Header.txt\" file about your \n");
       printf("!!!!!   Folder information.  And do this again.\n\n");
       exit (0);
  }

  /* Reading Header.txt... 2nd time is silent. */
  infpZIP = OpenAnyFile ("header.txt");
  if (infpZIP){
     printf ("     \n");
  }else{
       exit (0);
  }


  /*  Now, let's get started. */

   printf("Since different UNIX_Linux OS makes slightly different \n");
   printf("\"Catalog.txt\", please examine your catalogs at \"SDW\" \n");
   printf("and \"ZIP\" folders, and find out which columns are \n");
   printf("different among total nine(9) columns(fields), and come \n");
   printf("back here to continue. \n\n");
   printf("Please select which combination of column(s)(fields) you \n");
   printf("want to erase for easy comparison. \n\n");

   printf("  1. Field No.1 Only \n");
   printf("  2. Field No.4 Only \n");
   printf("  3. Field No.3+4    \n");
   printf("  4. Field No.1+3+4  \n\n");

   printf("Select Your Field_Erase_Combo by Number [1, 2, 3 or 4] : ");
   scanf("%d", &OPTION);
   printf(" \n\n\n");
   if (OPTION==1) ERASE_NO=1;
   if (OPTION==2) ERASE_NO=4;
   if (OPTION==3) ERASE_NO=7;
   if (OPTION==4) ERASE_NO=8;


/* ****** Here starts the body of "gccPj002.c" import ******** */
                                  
	printf("\n\n\nOne moment.  Then, go to see if a part of OWNER stamps erased \n");
	printf("files are there at SDW/Erased/ and ZIP/Erased/.  If there, we \n");
	printf("can go to the last step \"3Diff\".  \n\n\n");


/*  Read all lines in each catalogs in the SDW folder, and apply Eraser  =====  */
	while( (fgets(FNToOpen, 64, infpSDW))!=NULL )
	{
	    FILE *infp1, *outfp1;             /* These fp1s are for SDW */
	    
	    spacedwd (openedFN, &FNToOpen[0]);    
                                       /* Opening filename, one-by-one */
	    
	    sprintf (openedFNP, "./SDW/%s.txt", openedFN);
	    sprintf (writingFile, "./SDW/Erased/%s.txt", openedFN);
	   
	    infp1=fopen(openedFNP, "r");
	    outfp1=fopen(writingFile, "w");
	
		fprintf (outfp1, "SDW\n");                
		while( fgets(line, 256, infp1)!=NULL ){
		   if ( strncmp(line, "total", 5)==0 )
		      fprintf (outfp1, "total\n");
		   else if ( strncmp(line, ".:", 2)==0 )
		      fprintf (outfp1, "%s", line);
		   else if ( strncmp(line, "./", 2)==0 )
		      fprintf (outfp1, "%s", line);
		   else if ( strncmp(line, "\n", 1)==0 )
		      fprintf (outfp1, "\n");
		   else{     /* At here, I need to apply OWNER Stamp Erasing */

/* ** For my Reading convenience, Indent was removed in this section */
/* ***************************************************************** */
/*  All Field Sandwitch Eraser Objects starts from &line[0]     */
/*   The [0] is reminiscence of fixed column control prototype. */

   if (ERASE_NO==1) {
       fild_1up (upField, &line[0]);     /* Field No.1 Only*/
       fild_1dw (downField, &line[0]);
       fprintf (outfp1, "%s          %s", upField, downField);
       /* 10 Blanks for "  -rwxrwxr-x" */
   }
   if (ERASE_NO==4) {
       fild_4up (upField, &line[0]);     /* Field No.4 Only*/
       fild_4dw (downField, &line[0]);
       fprintf (outfp1, "%s     %s", upField, downField);
       /* 5 Blanks for "mkido" OR else */
   }
   if (ERASE_NO==7) {
       fild_7up (upField, &line[0]);     /* Field No.3+4 = 7 */
       fild_7dw (downField, &line[0]);
       fprintf (outfp1, "%s              %s", upField, downField);
       /* 14 + Blanks for "mkido  mkido"  OR else */
   }
   if (ERASE_NO==8) {
       fild_8up (upField, &line[0]);     /* Field No.1+3+4 = 8*/
       fild_8dw (downField, &line[0]);
       fprintf (outfp1, "          %s              %s", upField,
downField);
       /* 10+14 Blanks for "-rwxrwxr-x  mkido mkido" OR else */
   }
/* ** End of Reading convenience, return to normal Indent ************ */

                   }  /* closing Else-loop */                
                }     /* closing While-loop of SDW catalog Reading*/
            *openedFN=0;   /* Reset, in case */
            *openedFNP=0;
            fclose (infp1);
            fclose (outfp1);
         }   /* closing, one-by-one file opening While-loop */
         fclose (infpSDW);
    
/*  Read all lines in each catalogs in the ZIP folder, and apply Eraser */
	while( (fgets(FNToOpen, 64, infpZIP))!=NULL )
	{
	    FILE *infp2, *outfp2;             /* These fp2s are for ZIP */
	    
	    spacedwd (openedFN, &FNToOpen[0]);    
                                       /* Opening filename, one-by-one */
	    
	    sprintf (openedFNP, "./ZIP/%s.txt", openedFN);
	    sprintf (writingFile, "./ZIP/Erased/%s.txt", openedFN);
	   
	    infp2=fopen(openedFNP, "r");
	    outfp2=fopen(writingFile, "w");
	
		fprintf (outfp2, "ZIP\n");
		while( fgets(line, 256, infp2)!=NULL ){
		   if ( strncmp(line, "total", 5)==0 )
		      fprintf (outfp2, "total\n");
		   else if ( strncmp(line, ".:", 2)==0 )
		      fprintf (outfp2, "%s", line);
		   else if ( strncmp(line, "./", 2)==0 )
		      fprintf (outfp2, "%s", line);
		   else if ( strncmp(line, "\n", 1)==0 )
		      fprintf (outfp2, "\n");
		   else{     /* At here, I need to apply OWNER Stamp Erasing */

/* ** For my Reading convenience, Indent was removed in this section */
/* ***************************************************************** */

   if (ERASE_NO==1) {
       fild_1up (upField, &line[0]);     /* Field No.1 Only*/
       fild_1dw (downField, &line[0]);
       fprintf (outfp2, "%s          %s", upField, downField);
       /* 10 Blanks for "  -rwxrwxr-x" */
   }
   if (ERASE_NO==4) {
       fild_4up (upField, &line[0]);     /* Field No.4 Only*/
       fild_4dw (downField, &line[0]);
       fprintf (outfp2, "%s     %s", upField, downField);
       /* 5 Blanks for "root" (in case of CD) OR else */
   }
   if (ERASE_NO==7) {
       fild_7up (upField, &line[0]);     /* Field No.3+4 = 7 */
       fild_7dw (downField, &line[0]);
       fprintf (outfp2, "%s              %s", upField, downField);
       /* 14 Blanks for "root  root" OR else */
   }
   if (ERASE_NO==8) {
       fild_8up (upField, &line[0]);     /* Field No.1+3+4 = 8*/
       fild_8dw (downField, &line[0]);
       fprintf (outfp2, "          %s              %s", upField, downField);
       /* 10+14 Blanks for "  -rwxrwxr-x  root  root" OR else */
   }
/* ** End of Reading convenience, return to normal Indent ************ */
                   	       
                   }  /* closing Else-loop */                
                }     /* closing While-loop of ZIP catalog Reading */
            *openedFN=0;   /* Reset, in case */
            *openedFNP=0;
            fclose (infp2);
            fclose (outfp2);
        }   /* closing, one-by-one file opening While-loop */
        fclose (infpZIP);
}

/* ******** END of "gccPj002.c" import ******************** */




/* Here are two SUBROUTINEs by Kdo. */
/* ******************************** */

// FILE *OpenAnyFile (char *FileName);
// void *strupr (void *buffer);   

/* Find any file with same spelling regardless of capitalization */
/* Return a stream to the file, opened for read access           */

FILE *OpenAnyFile (char *FileName) 
{
  DIR *Dir;
  struct dirent *DirEnt;
  char *Target;
  char *Temp;
  FILE *Stream = NULL;

  Target = strdup (FileName);   // make an upper case version of the name.
  strupr (Target);

   Dir = opendir (".");  /* any suitable directory name  */

  while (DirEnt = readdir (Dir))
  {
    Temp = strdup (DirEnt->d_name);
    strupr (Temp); // convert the found name to upper case
    if (strcmp (Temp, Target) == 0)  // names match
    {
      Stream = fopen (DirEnt->d_name, "r");   // open the file for read

      free (Temp);
      break;
    }
    free (Temp);
  }
  free (Target);
  closedir (Dir);
  return (Stream);
}


/*  strupr() isn't universal.  You can make one very easily. */
/*  Pass void so you can pass either char or unsigned char  */
/*    void *strupr (void *buffer);    */  


void *strupr (void *Buffer)
{
  unsigned char *ptr;

  ptr = (unsigned char *)Buffer;
  while (*ptr)
  {
    if (*ptr >= 'a' && *ptr <= 'z')
      *(ptr++) = (*ptr) - 'a' + 'A';
    else
      ptr++;
  }
  return (Buffer);
}