/* gccPj003.c */
/* Originally, 5Diff.3.c  */
/* 3Diff  -  executable file posted   */

/* History:   The same as the other;  PC -> Classic Mac -> ApGCC-3.3 here.   */

/* (2004-11-30)  For better handling of Macintosh foldernames, filenames, 
 *    I modified 'firstwd.c'(First Word), to 'spacedwd.c'(Spaced Words).
 *    Right now, most of space including foldernames are properly handled.
 */
/* (2005-01-31)  I ported "Grand_Summary.Txt" codes here from PC version.    */
/* (2005-03-10)  I found a bug when I tested this "5Diff.2.c" at AFOC.
 *    The 5Diff.2.c did write Diff reports at USC, but not at AFOC.   When I look at 
 *    today, I saw "openedOutputFN [64]" for output filenames.  I think this comes 
 *    from old days of PC codes, when I couldn't figure out a way of lnog 
 *    filenames.   Now, all PC codes use 'catlg00x.Txt' and Rename Batch file, 
 *    so I don't need any special handling of output filenames.   
 *      In PC  -> 'catlg00x.Txt' and Rename
 *      In MAC ->  use all the way Same one;  better fix MacOS7/8/9 too.
 *                  since it may have this bad code.
 *    Sill I did't find any wrong code regarding "openedOutputFN [64]"
 *    as it is just redundant, but let me see how it runs.
 *    One possibility is how to address the current folder in MacOSX.
 *    I used, 
 *      ./SDW/Erased/
 *      ./ZIP/Erased/
 *      ./DIFF/
 *    but at AFOC, it may like 
 *      SDW/Erased/
 *      ZIP/Erased/
 *      DIFF/
 *    If the problem persists, try which way goes better to many OSX.
 *    For this moment, I haven't decided which way is better, so I keep 
 *    the mixed content as seen in below.
 */
/* (2005-03-15)   Regarding ABSOLUTE and RELATIVE:
 *    Batch.Txt          ->  ABSOLUTE is better, then I can use CD, DVD.
 *    /Erased/;  /DIFF/  ->  RELATIVE is better, because flexible.
 */
/* (2005-03-10)   Now, I want to do a little Adventure on MISSING ONE LINE 
 *    of SDW catalog.   As I planned, still while Eating The LINE, 
 *      print (SDWline); count up.
 *    Not completed yet.
 */


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

char lineSDW [256];
char lineZIP [256];

char FNToOpen [64];  /* = filenamesToOpen */
char openedFN [64];  /* = opendFilename */
char openedSDW_FNP [64], openedZIP_FNP [64];   /* = openedSDW_FilenameWithPath,  = openedZIP_FilenameWithPath */
char writingFile [64];




extern char *spacedwd (char *to, char *from);    /* = spaced words, to capture blank-containing foldername */

main()
{
    int lengthSDW, iFlag, iSDW, iZIP;           /* iFlag is counting output lines in a file to see which folders doesn't change. */
    
    FILE *infpSDW, *infpOUT;        /* These Pointers read fileNames list */
    FILE *infp1, *infp2, *outfp, *outfp2;   /* infp1->SDW_files; infp2->ZIP_files; outfp->Diff_Output; outfp2->Grand_Summary */


     /* infpSDW reads Foldernames at 'HEADER.(TXT)' to compare the two files contents */
    if      (  (infpSDW=fopen("Header",     "r"))!= NULL)      ; 
    else if (  (infpSDW=fopen("Header.Txt", "r"))!= NULL)      ; 
    else{
       exit (0);
    }

    outfp2=fopen("Grand_Summary.Txt", "w");
    
    printf("\n\n\nMaking final reports at each folder level at ./DIFF/.\n");
    printf("From the size of each report file, you can estimate which folder \n");
    printf("likely have a big change.  No change folder appears as 2 lines.\n");
    printf("Any file change should appear.\n\n");
    printf("The mechanism of this \'diff\' report is based on reading two files \n");
    printf("at the same time from top of both files.  So, in case there is a \n");
    printf("line shift near the top, then all lines followed are reported. \n");
    printf("So, in a sense, this is not a sophisticated program, but just to \n");
    printf("quickly review whether any change is there at given folders. \n");
    printf("The good thing is that you always have the Master ZIP disk safely.\n");
    printf("And this dual way is somehow different from backup-&-restoration. \n\n");
    printf("\"Grand_Summary.Txt\" is included for a quick review.\n\n\n");
    

    iFlag=iSDW=iZIP=0;

/*  Read all lines from the file stream pointing to "Ref.txt"             */
	while( (fgets(FNToOpen, 64, infpSDW))!=NULL )
	{
	    spacedwd (openedFN, &FNToOpen[0]);    /* Opening filename, one-by-one */
	    
	    sprintf (openedSDW_FNP, "./SDW/Erased/%s.txt", openedFN);
	    sprintf (openedZIP_FNP, "./ZIP_FLASH/Erased/%s.txt", openedFN);
	    sprintf (writingFile,   "DIFF/%s.txt",       openedFN);
	   
	    infp1=fopen(openedSDW_FNP, "r");
	    infp2=fopen(openedZIP_FNP, "r");
	    outfp=fopen(writingFile, "w");
	    lengthSDW=iFlag=0;
	
	    /* Reading both lines from two files simultaneously, the pair */
		while( (fgets(lineSDW, 256, infp1))!=NULL && (fgets(lineZIP, 256, infp2))!=NULL ){     
		   lengthSDW = strlen(lineSDW);
		   if ( strncmp(lineSDW, lineZIP, (lengthSDW-1) )==0 )
		      ;
		   else{     /* reporting difference */
		       fprintf (outfp, "SDW      :%s", lineSDW);
		       fprintf (outfp, "ZIP_FLASH:%s", lineZIP);
		       iFlag++;          /* Counter for Grand_Summary.Txt */
           }
        }     /* closing Two Lines Comparison WHILE-LOOP */
        
        /* In case ZIP Catalog is longer */
        if(infp2!=NULL){
           while( (fgets(lineZIP, 256, infp2))!=NULL ){
              fprintf(outfp, "ZIP: %s", lineZIP);
		      iFlag++; iZIP++;
           }
        }
        *lineZIP=0;


       /* ***** WORKING ON MISSING LINE **** */
       /*  This approach didn't work, since although the same catalog size, it printed extra SDW line */
       /*  I'd better think later. */
       /*  I thought about it, and I want to introduce an control Flag, OR else, but it 
        *    it isn't the final conclusive good idea.  So still goes on (2005-03-15).
        */
//        if (*lineSDW != 0 && *lineZIP ==0 ){
//              fprintf (outfp, "SDW:%s", lineSDW);
//              iSDW++;
//        }
           
        /* In case SDW Catalog is longer */
        if(infp1!=NULL){
           while( (fgets(lineSDW, 256, infp1))!=NULL ){
              fprintf(outfp, "SDW: %s", lineSDW);
		      iFlag++; iSDW++;
           }
        }
        *lineSDW=0;

        /* Since iFlag counter is still counting up when either catalog is longer,    */
        /*   so, the following Grand_Sum.Txt must be after them.                      */
        /* The Grand Summary report only needs to know iFlag count for each one set of comparison. */
        if (iFlag==1)            fprintf (outfp2, "  No change at :  %s\n", openedFN);
        else if ( iFlag>=1 && iSDW==0 && iZIP==0 ){   
                                 fprintf (outfp2, "  May be O.K.  :  - %s\n", openedFN);
        }else                    fprintf (outfp2, "  Watch out at :  ****  %s\n", openedFN);
        
        iFlag=iSDW=iZIP=0;  /* Reset, in case */

        *FNToOpen=*openedFN=0;   /* Reset, in case */
        *openedSDW_FNP=0;
        *openedZIP_FNP=0;
        fclose (infp1);
        fclose (infp2);
        fclose (outfp);
    }   /* closing, filenames opened by While-loop */
    fclose (infpSDW);
}