/* gccPj001.c */
/* Generating a Batch.txt file.   Internal messages are revised slightly in 2008. */
/* 1Bat -  executable filename for posting */

/* History:  PC "2Batch"  =>   Classic Macintosh CodeWarriors 3  =>   MacOSX  Apple's version of GCC-3.3 or similar. */

/* Instead of MPW, "1Bat" use 'Terminal'.  Read 'Header.Txt', Write 'Batch.txt'. 
 *    Path difference easer is not necessary at MacOSX Dual.
 */

/* (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)  Double Quote is good.
 *    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.
 *    This is a minor change, but has a great effect.   
 *    Notice introduction (2005-01-11) and Double Quote (2005-
 *    01-22) are new to version "10B.c"
 */
/* (2005-02-25)  GCC:  I STARTED PORTING TO GCC OF MacOSX ================
 *    Luckily, I can get started Direct Porting of CodeWarrior Pro 3 C-codes 
 *    into Mac_OSX GCC (/user/bin/gcc).  This "3MPWBatch.c" is compiled with 
 *    Object "spacedwd.o", and runs well.  At Mac_OSX, I use Terminal interface 
 *    to make catalogs instead of MPW.
 *
 *    [Programming note]  At Mac_OSX GCC-3.3, C-compiler takes directly the regular 
 *    slash ("/") in 'printf()', and it is no longer Escape Character, so be
 *    aware of this change.   I don't know this is general to All GCC-3.3 OR 
 *    Mac_OSX adopted GCC-3.3, though.
 */
/* (2005-03-01) MacOSX all filenames (both reading and writing) would have 
 *    .txt
 *    This makes them clear what they are: "Text files".
 *    Also, OSX Catalogs don't need a Eraser of Path Difference, so I deleted 
 *    "outfp2" for "Path.txt" file.
 */

#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;  /* These Pointers read fileNames list */
    
    outfp1=fopen("Batch.txt", "w");    /* outfp1 is for generic output Batch file. */


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


	printf("\n\nThis MacOSX DUAL is updated recently in March 2008.       \n");
	printf("Please read explanatory PDF document(s) to get an idea of mechanism.   \n");
	printf("Customization of 1Bat, 2Er, 3Diff at source codes is also easy.        \n");
	printf("With MacOSX purchase, Xcode Tools for development comes together.      \n");
	printf("Install it.   That't it.    Apple version of GCC (Compiler) works      \n");
	printf("almost the same as the other GCC in Linux_Unix.                        \n");
	printf("This is for PowerPC-CPU.  For Intel-CPU, please compile by yourself.   \n\n");
	

/*  Read user's input from the standard input */
/*  The following User's Magic Variable Inputs work pretty well */
	printf("\nType Your Working Folder Name of this DUAL, \n");
	printf("    For example, if you use this in \"1dual\" folder at your home,    \n\n");
	printf("        /Users/YourUserName/1dual/     --- (absolute PATH).           \n");
	printf("        ~/1dual/                       --- (shortcut PATH).           \n\n");
	printf("    In this example case, just type \"1dual\".                        \n");
	printf("    Type now, the Working Folder Name      :  ");
	scanf("%s", &str1);
	printf("    Your Working Folder is set to          :  %s\n\n\n\n", str1); 

	printf("Type Your ZIP_FLASH disk/drive name, \n");
	printf("    For example, if your FLASH drive is mounted onto your MacOSX machine as,   \n\n");
	printf("        /Volumes/MyDessins/       -----    (absolute PATH).                    \n");
	printf("                           (here \"MyDessins\" is the volume name of FLASH drive.)  \n\n");
	printf("    Then type :  MyDessins                                                     \n");
	printf("    If you want to scan deeper subfolder of a FLASH drive, you can set the     \n");
	printf("    PATH there.     \n");
	printf("    Type now, ZIP_FLASH disk/drive\'s name       :  ");
	scanf("%s", &str3);
	printf("    Your ZIP_FLASH disk\'s scanning is set to    :  %s\n\n\n\n", str3);      

	printf("Type Your Shadow (or Mirror) location of DUAL Copy on your hard disk.          \n");
	printf("     For example your shadow's absolute PATH is,                               \n\n");
	printf("        /Users/YourUserName/sdw/MyDessins/                                     \n\n");
	printf("     Then type :  sdw/MyDessins                                                \n");
	printf("     Type now, Shadow location and ZIP_FLASH corresponding subfoldername   :  ");
	scanf("%s", &str2);
	printf("     Your Scanning Shadow on the hard disk is set to                       :  %s\n\n", str2);      

    fprintf (outfp1, "#! /bin/bash\n");
    fprintf (outfp1, "#! /bin/tcsh\n\n");

    fprintf (outfp1, "mkdir SDW\n");
    fprintf (outfp1, "mkdir SDW/Erased\n");
    fprintf (outfp1, "mkdir ZIP_FLASH\n");
    fprintf (outfp1, "mkdir ZIP_FLASH/Erased\n");
    fprintf (outfp1, "mkdir DIFF\n\n");


   

/*  Read all foldernames listing in "HEADER" file */
/*  First SDW */
/* infpSDW reads All Foldernames at 'HEADER'. */

/* I added, if( infp=fopen() != NULL ) in case an user forgets the "Header" file. 
 *   Since I myself forget it often, I am making sure that the file is provided.  
 *   I wanted to put this message all the way first, so this went up.  Also, in OSX
 *   File Association with application became blur by mixing of Extention and Signature 
 *   Stamp mechanisms.   So I added "Header" and "Header.txt" both works.
 */
 
/*  ==========  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");
    
      /*=====  SDW-Catalog  =====*/
	while( (fgets(lineSDW, 64, infpSDW))!=NULL )
	{
	    spacedwd (folderName, &lineSDW[0]);    /* Opening foldername, one-by-one */
        fprintf (outfp1, "cd ~/%s/\"%s\"\n", str2, folderName);
        fprintf (outfp1, "ls -lFR > ~/%s/SDW/\"%s.txt\"\n", str1, folderName);
    }   /* closing While-loop */
    fclose (infpSDW);
    *folderName=0;  /* Reset, in case */

    fprintf (outfp1, "\n");   /* One Spacer line to see SWD, ZIP command lines clearly */

    

/*  Second ZIP_FLASH */
    if      (  (infpZIP=fopen("Header",     "r"))!= NULL)      ; 
    else if (  (infpZIP=fopen("Header.Txt", "r"))!= NULL)      ; 
    else exit (0);

      /*=====  ZIP-Catalog  =====*/
	while( (fgets(lineZIP, 64, infpZIP))!=NULL )
	{
	    spacedwd (folderName, &lineZIP[0]);    /* Opening foldername, one-by-one */
        fprintf (outfp1, "cd /Volumes/%s/\"%s\"\n", str3, folderName);
        fprintf (outfp1, "ls -lFR > ~/%s/ZIP_FLASH/\"%s.txt\"\n", str1, folderName);
    }   /* closing While-loop */
    fclose (infpZIP);
    *folderName=0;  /* Reset, in case */


/* 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("\n\n\nPlease examine \"Batch.txt\" file in this working folder after you \n");
    printf("will finish this \"1Bat\".  You will see something like below. \n\n");
    printf("#! /bin/bash \n");                                
    printf("#! /bin/tcsh \n\n");                                
    printf("mkdir SDW\n");
    printf("mkdir SDW/Erased \n\n");
    printf("cd ~/sdw/MyDessins/\"Hippo_Owen_at_Wildlife_Park_Kenya\"        \n");
    printf("ls -lFR > ~/SDW/\"Hippo_Owen_at_Wildlife_Park_Kenya.txt\"       \n");  
    printf("cd ~/sdw/MyDessins/\"Owen_s_friend_Tortoise_Mzee\"              \n");
    printf("ls -lFR > ~/SDW/\"Owen_s_friend_Tortoise_Mzee.txt\"             \n");
    printf("cd ~/sdw/MyDessins/\"Please_find_them_LATimes_Feb_20_2005_A3\"  \n");
    printf("ls -lFR > ~/SDW/\"Please_find_them_LATimes_Feb_20_2005_A3.txt\" \n\n\n");
    printf("If you see these lines, we are ready to run this Batch script file.  \n");
    printf("Please read carefully how to run this UNIX/Linux/MacOSX generic \n");
    printf("Batch Shell scripts at \"MacOSX-Unix-Linux.PDF\".  -> \"How to set \'Batch.Txt\' \n");
    printf("as an executable file.\" \n\n\n");
 	printf("Type any key then [return] to read through this note :  ");
	scanf( "%s", &uType);
    
/* ===Message===  If you didn't get the Batch file, a likely problem is that
 *  the current working folder has not set in executable Path.  =====END Message ===
 *  This message must go in .PDF trouble shooting file, not in a running Program.
 *  Because, if Path is not set, no Program runs, including this Program.
 */
/* ===Another Message===   I should cautiously 
 *  mention a couple of times that whenever I make a new EXE & Batch files, I needed to 
 *  Re-new the Executable Path, according to my experience.   Tedious though.
 *  Well, for an user, only twice: before running OSX_DualWay and before running Batch.txt.
 *  so, may not be so bad.   For me, many times, because I compile many EXEs.
 */
    printf("\n\n\nIf your Batch file is messy, a likely problem is that \"Header.(txt)\" \n" );
    printf("file, so read carefully about trouble shooting about \"Header.(txt)\"     \n" );
    printf("file at the trouble shooting in \"MacOSX-Unix-Linux.PDF\"                 \n" );
    printf("If your Batch file has the wrong Path, or missing                         \n" );
    printf("parental Path etc., you can run this \"1Bat\" again, OR                   \n");
    printf("SimpleText -> Edit -> Replace... -> and use \"Replace ALL\" conveniently  \n");
    printf("to fix the Paths.  Bas error usually points to Path error.  \n\n");

    printf("By a brief test, under sophisticated network setting of your MacOSX  \n");
    printf("machine, sometimes you may need to Re-new the Executable Path before \n");
    printf("running the Batch file. \n");
    printf("Don\'t forget to change the Batch file permission from Read-only mode  \n");
    printf("to Read and Executable mode, such as \'chmod 777 Batch.txt\'. \n\n");

/* This message is about right time, but until running the Batch file, I can't say 
 *    "Please check whether folders are made".
 */
    printf("When you will successfully run the Batch script, you should be able \n");
    printf("to see \"SDW\", \"ZIP_FLASH\", \"DIFF\" folders in this working folder. \n");
    printf("And those two folders (\"SDW\", \"ZIP_FLASH\") are filled with catalogs, \n");
    printf("which have all content information of each scanning folder. \n");
    printf("If you will see them, then we can go to the final easy two steps. \n\n\n");
    printf("END of \"1Bat\" \n\n");

    fclose (outfp1);
}