/*
             CREATOR: DAn williams
            FILENAME: hello.c
             STARTED: Nov 28
           PLATFORMS: Linux 
         DESCRIPTION: Using hello world to show off my primative 
             command line parser.
  
 credit for this code?  blame this guy => dan_williams@sunshine.net

*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>

#include "cmnCmdOptions.h"

int stringCase; // flag for the case of the string
char* fileName; // filename for output
int verbose;    // verbosity flag for credits
char* mainString; // string to print out
int justDie;     // This means that we did help, so exit

void setupParams(void);
int doWork(void);

int UpperCase (char* unused);
int LowerCase (char* unused);
int setFileName (char* newFileName);
int clearVerbose (char* unused);
int setOutputString (char* string);
int doHelp (char* unused);


int main(int argc, char** argv)
{
  /*
   What kind of options can you give hello world?, lets see...
  
   (-c or --lower)            print it in lower case
   (-C or --upper)            print it in upper case
   (-O or --output) foo       Put the result in file foo
   (-v or --shutup or --shh)  don't print credits
   (-? or --help or ?)        print help screen
   -S foo                     override the string "hello world" with "foo"   

   ENOUGH OPTIONS!
   
   */

  char message[] = {"Hello World."};

  // init our defaults
  fileName   = NULL;   // no output filename
  verbose    = 1;    //default = be verbose
  mainString = message; // default "hello world string"
  justDie    = 0; // do the work

  setupParams(); // define the arguments

  parseOptions(argc, argv);   // then parse the command line
  
  if (justDie == 0) {
    if (doWork() != 0) { // run the code that does the work; (uses the flags)
      return -1;
    }
  }

 return 0;
}

/***********************************************************
   These are the main work routine(s) :-)
***********************************************************/

void setupParams(void) {

  // this is outright ugly, but we have to because of limits to C
  static char* optsLowercase[2] = {"-c","--lower"};
  static char* optsUppercase[2] = {"-C","--upper"};
  static char* optsoutputfile[2] = {"-O","--output"};
  static char* optsShutup[3] = {"-v","--shutup","--ssh"};
  static char* optsHelp[3] = {"-?","--help","?"};
  static char* optsString[1] = {"-S"};

  // first init 
  cmdLineOptionInit();
  // then add your options (note I ignore any errors (BAD! BAD!))
  addCmdLineOption(optsLowercase, 2, LowerCase);
  addCmdLineOption(optsUppercase, 2, UpperCase);
  addCmdLineOption(optsoutputfile, 2, setFileName);
  addCmdLineOption(optsShutup, 3, clearVerbose);
  addCmdLineOption(optsHelp, 3, doHelp);
  addCmdLineOption(optsString, 1, setOutputString);


}


int doWork(void) {
 
  FILE* output;

  if (verbose == 1) {
    printf("Hello World with options, by DAn Williams\n");
  }

  if (fileName != NULL) {
    // open and output to a file
   if ((output = fopen(fileName, "wt")) == NULL) {  //open text file w/ err chk
     if (verbose == 1) {
       printf("Unable to open %s for output.\n", fileName);
     }// end of if verbose
     return -1;
   }// end of if open
   fprintf(output, "%s\n", mainString); // print the string
   fclose(output); // close the file
  } else {
    printf("%s\n", mainString); //print the string to the screen
  }// end of if filename <> null

  return 0;
 
}

/***********************************************************
    These routines handle calls from the options parser
***********************************************************/

// upperCase the string
int UpperCase (char* unused) {

  char* tempString;

  tempString = mainString;

  while (*tempString != '\0') {
    *tempString = toupper(*tempString);
    tempString++;
  }
  return 0; // we didn't use any paramiters
}

// lowerCase the string
int LowerCase (char* unused) {
  char* tempString;

  tempString = mainString;

  while (*tempString != '\0') {
    *tempString = tolower(*tempString);
    tempString++;
  }
  return 0; // we didn't use any paramiters
}

// set an ouput filename
int setFileName (char* newFileName) {
  fileName = newFileName;
  return 1; // we used one paramiter
}

// shut up!
int clearVerbose (char* unused) {
  verbose = 0;
  return 0; // you get it!!!
}

// override the output string
int setOutputString (char* string) {
  mainString = string;
  return 1;
}

// help the poor user
int doHelp (char* unused) {
  printf("This is DAn Williams fanciest hello world as of Nov 27/0002000\n\n");
  printf("   The options are:\n");
  printf("     -c or --lower            specify lower case\n");
  printf("     -C or --upper            specify upper case\n");
  printf("     (-O or --output) foo     put the string into file foo\n");
  printf("     -v or --shutup or --shh  dont print non string to console\n");
  printf("     -? or --help or ?        print this help\n");
  printf("     -S foo                   override the hello world string with foo\n\n");
  printf("   The upper and lowercase options are realTime, thus if you \n");
  printf("    specify uppercase and then a custom string option, your string \n");
  printf("    will not be effected. This is a feature, not a bug. As a \n");
  printf("    warning, the -v series of options will also kill error reports, \n");
  printf("    but not help, again a feature, not oversite.\n");
  printf("   examples of use:\n");
  printf("    hello -C  (prints HELLO WORLD)\n");
  printf("    hello -S \"this is uppercase\" --upper --output true.txt (try it)\n\n");
  printf("  Remember to stay Y1M complient!\n");
   
  justDie = 1;
}


// EOF!















