/* mcPj010B.c */

/* After the success of File Reading and Folder Structure Handling,          */

/* Now, get into the Real World Business, porting PC "2Batch" to Macintosh.  */

/* Early notice of missing file "Header" is provided in this 10B version.    */



/* "3_MPWBatch" reads the "HEADER" file, and writes "Batch" which is ready 

 *  to run inside of MPW Shell, which includes, both SDWBatch, ZIPBatch, and 

 *  Creator Stamp changes (SimpleText) of all catalogs and reports.  

 *  I used to generate two separate Batch files, but at here, I start 

 *  using all ABSOLUTE PATHs, so it doen't matter where the Batch file locates.

 *  It should run from anywhere.  Besides, an user have to copy them into MPW, 

 *  anyway, so, one file is fine.       

 */



/* (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-11, added note) This is true for TextEdit-> SimpleText (mcPj007C.c)

 *    but not fully tested in "MacDualWay".

 */

/* (2005-01-11)  I decided to modify to normally terminate when "Header" 

 *    file is missing by telling that "Please put the Header file and do it again".

 *    This is much better than a sudden Type 1 error closure.

 */

/* (2005-01-22)  On occasion of "Header" Picture Posting in the general 

 *    introduction (PURCHASE:ByVender), I tested Blank Space " " and Apostrophy 

 *    " ' " (Fry's Electronics).  It turned out that I'd better to Double-Quote

 *    All filenames in MPW.   So I did.   Then MPW errors are greatly reduced.

 *    A case which I am not sure is that a new folder(s) in ZIP (only) not yet

 *    in SDW .  Watch out how it(they) work(s).   This is a minor change, but a 

 *    great effect.  Notice introduction (2005-01-11) and Double Quote (2005-

 *    01-22) are new to version "10B.c"

 */

/* (2005-02-05)  MPW Shell command line for "Grand_Summary.Txt" is reversely 

 *    ported here from "3_MPWBatch,OSX".

 */



#include <stdio.h>

#include <stdlib.h>

#include <string.h>



char uType [16];

char lineSDW [256], lineZIP [256];

char lineSDWEr [256], lineZIPEr [256], lineDIFF [256];

/* Those above lines could be all 'line', but they are Global, so 

 *    I took an extra cautious step to avoid any mixed-up.

 */

char folderName [128];



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



main()

{

//    char str[64], strPause[16];

	char str1[64], str2[64], str3[64];

    

    FILE *infpSDW, *infpZIP, *outfp1, *outfp2;  /* These Pointers read fileNames list */

    FILE *infpSDWEr, *infpZIPEr, *infpDIFF;

    

    outfp1=fopen("Batch", "w");    /* outfp1 is general output for MPW Shell Batch. */

    outfp2=fopen("Path", "w");     /* outfp2 is Absolute Path output, for following programs. */





/*  ==========  This Block is out of place But COPY CAME UP HERE, for early notice === */

/*  ==========    of MISSING "Header  OR  Header.txt" file.   ======================== */

/*  ================================================================================== */

/*  The folllowing ";" are meant to continue, not for the end of sentence. 

 *   I use this, because I saw Five Time of opening, so I like the compact space.

 *   Maybe I replace to continue for clearness.

 */

    if      (  (infpSDW=fopen("Header",     "r"))!= NULL)      ; 

    else if (  (infpSDW=fopen("Header.Txt", "r"))!= NULL)      ; 

    else{

       printf("!!!!!   Please put a \"Header\" file about your Folder information.\n");

       printf("!!!!!   And do this again.\n\n");

       exit (0);

    }

//       infpSDW=fopen("Header", "r");

/*  ================================================================================== */







/*  Read string(s) from the standard input stream */

/*  The following User's Magic Variable Inputs work pretty well */

    printf("!!! Please type Folder Paths in Case Sensitive manner.  Thank you!\n\n");

	printf("Type Your Working Folder of this Dual-Way, \n");

	printf("     starting from Macintosh name. \n");

	printf("     For example,  Mac-3:Test :  ");

	scanf("%s", &str1);

	printf("Your Working Folder is set to :  %s\n\n", str1); 

	fprintf(outfp2, "Working Folder:   %s\n", str1);     

	fprintf(outfp2, "Working FolderP:  \'%s\n", str1);     

	

	printf("Type Your Shadow (or Mirror) location, \n");

	printf("     starting from Macintosh name, its folder\'s  \n");

	printf("     name which contains folder names in \'HEADER\'  \n");

	printf("     including parental folder names. \n");

	printf("     For example,  Mac-3:Shadow:Graphics  :  ");

	scanf("%s", &str2);

	printf("Your Shadow on the hard disk is set to    :  %s\n\n", str2);      

	fprintf(outfp2, "Shadow Folder:    %s\n", str2);     

	fprintf(outfp2, "Shadow FolderP:   \'%s\n", str2);     



	printf("Type Your ZIP disk name (or Floppy, Jaz ..), \n");

	printf("     (and, if any, include parental folder names too) :  \n");

	printf("     For example,  Graphics  :  ");

	scanf("%s", &str3);

	printf("Your ZIP disk name is set to :  %s\n\n", str3);      

	fprintf(outfp2, "ZIP Disk Label:   %s\n", str3);      /* I don't know, I need this */ 

	fprintf(outfp2, "ZIP Disk LabelP:  \'%s\n", str3);      /* I don't know, I need this */ 









    fprintf (outfp1, ":For Grand Summary Report:\n");

    fprintf (outfp1, "SetFile -c ttxt %s:Grand_Summary.Txt\n\n", str1); 



    fprintf (outfp1, ":FOLDERS:\n");

    fprintf (outfp1, "NewFolder %s:SDW\n", str1);

    fprintf (outfp1, "NewFolder %s:SDW:Erased\n", str1);

    fprintf (outfp1, "NewFolder %s:ZIP\n", str1);

    fprintf (outfp1, "NewFolder %s:ZIP:Erased\n", str1);

    fprintf (outfp1, "NewFolder %s:DIFF\n", str1);



   



/*  Read all lines from foldername listing in "HEADER" file */

/*  First SDW */

/* infpSDW reads Any and All Filenames at 'HEADER', the same as 'Dual-Way.Exe' */



/* I added, if( infp=fopen() != NULL ) check point in case an user forgets the "Header" file, 

 *   because I got many "Type 1 Error" due to this careless mistake.

 *   Since this codes opens "Header" file Five times by Five different Pointers,

 *   so, I am making sure that the file is provided.   I want to put this all the way first, 

 *   then, before typing the User's Path, the important reminder comes first.

 */

 

/*  ==========  Here is the ORIGINAL LOCATION of the notice       ======================== */

/*  ==========    about  MISSING "Header  OR  Header.txt" file.   ======================== */

//     if      (  (infpSDW=fopen("Header",     "r"))!= NULL)      ; 

//    else if (  (infpSDW=fopen("Header.Txt", "r"))!= NULL)      ; 

//    else{

//       printf("!!!!!   Please put a \"Header\" file about your Folder information.\n");

//       printf("!!!!!   And do this again.\n");

//       exit (0);

//    }

//       infpSDW=fopen("Header", "r");

    

    fprintf (outfp1, "\n:SDW-Catalog:\n");

	while( (fgets(lineSDW, 64, infpSDW))!=NULL )

	{

	    spacedwd (folderName, &lineSDW[0]);    /* Opening foldername, one-by-one */

        fprintf (outfp1, "Files -l -r \"%s:%s\" > \"%s:SDW:%s\"\n", str2, folderName, str1, folderName);

        fprintf (outfp1, "SetFile -c ttxt \"%s:SDW:%s\"\n", str1, folderName);

    }   /* closing While-loop */

    fclose (infpSDW);

    *folderName=0;  /* Reset, in case */

    



/*  Second ZIP */

    if      (  (infpZIP=fopen("Header",     "r"))!= NULL)      ; 

    else if (  (infpZIP=fopen("Header.Txt", "r"))!= NULL)      ; 

    else exit (0);

//    infpZIP=fopen("HEADER.Backup", "r");    /* infpSDW reads Any and All Filenames at 'HEADER.TXT', the same as 'Dual-Way.Exe' */



    fprintf (outfp1, "\n:ZIP-Catalog:\n");

	while( (fgets(lineZIP, 64, infpZIP))!=NULL )

	{

	    spacedwd (folderName, &lineZIP[0]);    /* Opening foldername, one-by-one */

        fprintf (outfp1, "Files -l -r \"%s:%s\" > \"%s:ZIP:%s\"\n", str3, folderName, str1, folderName);

        fprintf (outfp1, "SetFile -c ttxt \"%s:ZIP:%s\"\n", str1,folderName);

    }   /* closing While-loop */

    fclose (infpZIP);





/*  Third SDW Erased */

    if      (  (infpSDWEr=fopen("Header",     "r"))!= NULL)      ; 

    else if (  (infpSDWEr=fopen("Header.Txt", "r"))!= NULL)      ; 

    else exit (0);

//    infpSDWEr=fopen("HEADER.Backup", "r");    /* infpSDW reads Any and All Filenames at 'HEADER.TXT', the same as 'Dual-Way.Exe' */

    

    fprintf (outfp1, "\n:SDW:Erased:\n");

	while( (fgets(lineSDWEr, 64, infpSDWEr))!=NULL )

	{

	    spacedwd (folderName, &lineSDWEr[0]);    /* Opening foldername, one-by-one */

        fprintf (outfp1, "SetFile -c ttxt \"%s:SDW:Erased:%s\"\n", str1, folderName);

    }   /* closing While-loop */

    fclose (infpSDWEr);





/*  Fourth ZIP Erased */

    if      (  (infpZIPEr=fopen("Header",     "r"))!= NULL)      ; 

    else if (  (infpZIPEr=fopen("Header.Txt", "r"))!= NULL)      ; 

    else exit (0);

//    infpZIPEr=fopen("HEADER.Backup", "r");    /* infpSDW reads Any and All Filenames at 'HEADER.TXT', the same as 'Dual-Way.Exe' */

    

    fprintf (outfp1, "\n:ZIP:Erased:\n");

	while( (fgets(lineZIPEr, 64, infpZIPEr))!=NULL )

	{

	    spacedwd (folderName, &lineZIPEr[0]);    /* Opening foldername, one-by-one */

        fprintf (outfp1, "SetFile -c ttxt \"%s:ZIP:Erased:%s\"\n", str1, folderName);

    }   /* closing While-loop */

    fclose (infpSDWEr);





/*  Fifth DIFF */

    if      (  (infpDIFF=fopen("Header",     "r"))!= NULL)      ; 

    else if (  (infpDIFF=fopen("Header.Txt", "r"))!= NULL)      ; 

    else exit (0);

//    infpDIFF=fopen("HEADER.Backup", "r");    /* infpSDW reads Any and All Filenames at 'HEADER.TXT', the same as 'Dual-Way.Exe' */

    

    fprintf (outfp1, "\n:DIFF:\n");

	while( (fgets(lineDIFF, 64, infpDIFF))!=NULL )

	{

	    spacedwd (folderName, &lineDIFF[0]);    /* Opening foldername, one-by-one */

        fprintf (outfp1, "SetFile -c ttxt \"%s:DIFF:%s\"\n", str1, folderName);

    }   /* closing While-loop */

    fclose (infpDIFF);





/* This message is moved down, because it prompts to look at Batch file which 

 *    is empty, if this message is up before while-loop.

 */

    printf("Now, we are going to do a first practice of MPW.\n");

    printf("Please copy and paste the following line into at the \n");

    printf("bottom of MPW-Shell.  MPW-Shell shows a dozen of help commands \n");

    printf("list.  Don't mind anything about them, and just scroll down \n");

    printf("all the way, and at the bottom after the line, paste the line \n");

    printf("below.\n\n");

    printf("  SetFile -c ttxt %s:Batch\n\n", str1); 

    printf("then, put the cursor at the end of command line, and press \n");

    printf("the right-most [enter]-key on the keyboard (making sure not \n");

    printf("the regular [return]-key).\n\n");

    printf("After this, examine \"Batch\" file in this working folder is \n");

    printf("changed from purple-&-yellow-bar icon to white-&-text icon. \n");

    printf("If it changed, you can open it by simple double clicks. \n");

    printf("If you find many command lines there, Conglaturations!  We are\n");

    printf("ready to go.  If not, carefully re-type your macintosh name, \n");

    printf("your working folder name(s), delimited by colons, without space. \n");

    printf("Those names are case-senstive, so please type accurately. \n");

    printf("Now, copy all the lines in \"Batch\" file into the bottom of \n");

    printf("MPW Shell.\n\n");

	printf("Type any key then [return] :  ");

	scanf( "%s", &uType);

    

    printf("\n\nIf you get a trouble in this first MPW practice, read a note \n");

    printf("of \"Mac_Note\" under introductory page of this DualWay, at \n");

    printf("TuneFind -> Community.   After practice, come back here for one \n");

    printf("more page to read. \n");

	printf("Type any key then [return] :  ");

	scanf( "%s", &uType);









/* This message is good, but too late, becuase this program is already 4_Eraser, 

    which is assuming all catalogs files exist in folders.   Move into 3_Batch. */  

    printf("\n\n\nDid you success in the first MPW practice?   Now, this paragraph explains \n");

    printf("what to do next in advance here, after finish this \"3_MPWBatch\". \n");                                

    printf("Please copy entire content of \"Batch\" file into MPW Shell bottom.\n");

    printf("It has six groups: to make folders, to make catalogs of SDW and ZIP, \n");

    printf("to erase Size-Stamps from all catalogs, to make them viewable by \n");

    printf("simple Double-clicks.\n\n");  

    printf("Select five command lines in the first group :FOLDERS: by mouse, and \n");

    printf("execute those command lines by as always pressing the right-most \n");

    printf("[enter]-key (making sure not the regular [return]-key) at MPW Shell. \n");

    printf("Double check \"SDW\", \"ZIP\", \"DIFF\" folders are made in your \n");

    printf("working folder.   If they are, the MPW Shell is working, so do two \n");

    printf("more things.   Select all command lines under :SDW-Catalog: and \n");

    printf("execute them by right-most [enter], then do the same thing to \n");

    printf(":ZIP-Catalog:.   While making ZIP-Catalog, you must insert your Master \n");

    printf("ZIP disk.  Double check many catalogs of each folder are created, \n");

    printf("both under \"SDW\" and \"ZIP\" folders.\n\n\n\n\n");







    fclose (outfp1);

}