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



int 
main(int argc, char **argv ){
  
  FILE *fp = NULL;  
  char *filename = argv[argc - 1];
  
 switch (argc)
   {
   case (1):
     getCharacters(stdin, 0);
     break;
   
   case (2):
     {
       char arg1[strlen(argv[1])];
       strcpy(arg1, argv[1]);
             
       if (!strcmp(arg1, "-"))
       	 getCharacters(stdin, 0);
       
       else if (!strcmp(arg1, "-c") || !strcmp(arg1, "--chars"))
	 {
	   getCharacters(stdin, 1);
	 }
       
       else if (!strcmp(arg1, "-l") || !strcmp(arg1, "--lines"))
	 {
	   getCharacters(stdin, 2);
	 }
       else
	 {
	   openFile(fp ,filename, 0);
	 }
     }
     break;
     
   case (3):
     {
       char option[strlen(argv[1])];
       strcpy(option, argv[1]);
       
       if (!strcmp(option, "-c") || !strcmp(option, "--chars"))
	 {
	   if (*filename == '-')
	     {
	       getCharacters(stdin, 1);
	     }
	   else
	     {
	       openFile(fp, filename, 1);
	     }
	 }
       
       else if (!strcmp(option, "-l") || !strcmp(option, "--lines"))
	 {
	   if (*filename == '-')
	     {
	       getCharacters(stdin, 2);
	     }
	   else
	     {
	       openFile(fp, filename, 2);
	     }
	 }
       
       else
	 {
	   fprintf(stderr, "Invalid option\n");
	   return EXIT_FAILURE;
	 }
     }
     break;
     
   default:
     fprintf(stderr, "Too many arguments\n");
   }
 return 0;
}






  /* ************************************************************************/




/*   if (argc > 1 ) */
/*     { */
/*       char option[strlen(argv[1])]; */

/*       /\* if (*argv[1] == '-m' || *argv[1] == '--chars') *\/ */
/*     /\* 	printf("char count"); *\/ */
/*       strcpy(option, argv[1]); */
/*       if (!strcmp(option, "-m") || !strcmp(option, "--chars")) */
/* 	{ */
/* 	  getCharacters(fp, 1); */
/* 	  /\* printf("great success"); *\/ */
/* 	  /\* exit(0); *\/ */
/* 	} */

/*       if (!strcmp(option, "-l") || !strcmp(option, "--lines")) */
/* 	{ */
/* 	  getCharacters(fp, 2); */
/* 	  /\*   printf("great chars"); *\/ */
/* 	  /\*   exit(0); *\/ */
/* 	} */
/*     } */
  
  
/*   if (*filename == '-' || argc == 1) */
/*     { */
/*       return getCharacters(stdin, 0); */
/*     } */
  
  
  
/*   fp = fopen(filename, "r"); */
/*   if (fp == NULL) */
/*     { */
/*       fprintf(stderr, "File not found.\n"); */
/*       exit(EXIT_FAILURE); */
/*     } */
/*   else */
/*     { */
/*       getCharacters(fp, 0); */
/*     } */
/*   return EXIT_SUCCESS; */
/* } */

