/**************************************************************************/
/*                                                                        */
/*      program: incr                                                     */
/*   programmer: George Kerber                                            */
/*      written:  07/24/89                                                */
/*                                                                        */
/**************************************************************************/

#include <stdio.h>

#define WRITTEN "07/24/89 - 07/29/89"
#define VERSION "v1.01"
FILE *tfile;
char node[31];
void mistake();
void makeline();
void helpscreen();
  
main(argc,argv)
int argc;
char *argv[];
{
int optionsallowed = 3 , filenumber = 1;
char values[80];
char filename[80];
long value1,value2;

stcgfn(node,argv[0]);

if(argc == 1) { printf("\x0c\n"); helpscreen(0); }
 
if(argc > 1 && argv[1][0] == '-') 
   if(argv[1][1] != 'a' && argv[1][1] != 'b' && argv[1][1] != 'd') mistake("Invalid Option"); 

if(argc > 1) {
   if(argv[1][0] == '-') optionsallowed = 4;
   if(argc > optionsallowed) mistake("Invalid option count");
   if(!strcmp(argv[1],"-a")  || !strcmp(argv[1],"-b") || !strcmp(argv[1],"-d")) {
      if(argc == 2) mistake("Missing file name"); 
      filenumber = 2;
      }
   }

if(stricmp(argv[filenumber],"NOLINE") == 0) 
   mistake("filename can't be NOLINE");

if(stricmp(argv[argc - 1],"NOLINE") && (argc >= filenumber + 2))
   mistake("Invalid final arguement, can only be NOLINE");
   
if(argc > 4) mistake("Invalid Option Count");

if(argc == 2) {
   strcpy(filename,argv[1]);
   }
   else {
   strcpy(filename,argv[2]);
   }
if(access(filename,0) == 0) {
   tfile = fopen(filename,"r+");
   if(tfile == (FILE *)NULL) mistake("Can't open file for reading");
   fgets(values,80,tfile);
   fclose(tfile);
/*   for(i = 0 ; values[i] != '\0' ; i++) {    
      if(isdigit(values[i]) == 0) mistake("Incorrect file format");
      }  */
   value1 = atol(values);
   value2 = value1 + 1;
   if(strcmp(argv[1],"-d")) {
      remove(filename);
      tfile = fopen(filename,"a"); 
      if(tfile == (FILE *)NULL) mistake("Can't write incremented value");
      fprintf(tfile,"%ld",value2);
      fprintf(tfile,"\n");          /*  this makes a smaller file than putc???  */
      fflush(tfile);
      fclose(tfile);
      }
   }
   else {
   tfile = fopen(filename,"w");
   if(tfile == (FILE *)NULL) mistake("Can't create new file");
   fprintf(tfile,"0");
   fflush(tfile);
   fclose(tfile);
   value1 = 0;
   value2 = 0;
   }

if(argc > 2) {
   switch(argv[1][1]) {
      case 'a':  printf("%ld",value2) ; break ;
      case 'b':  printf("%ld",value1) ; break ;
      case 'd':  values[strlen(values) - 1] = '\0';
                 printf("%-6s",values); break ;
       default:  break ;
   }
}

if(argv[1][0] = '-') {
   if(stricmp(argv[argc - 1],"NOLINE") && argc > 2) printf("\n");
   }
exit(0);

}

/*----------------------------------------------------------------------------*/

/*  HELPSCREEN FUNCTION  */

void helpscreen(exitcode)
int exitcode;
{
printf("\n\n  [33m[1m%s[0m            George Kerber  %10s  %25s\n\n",node,VERSION,WRITTEN);
printf("  SYNTAX:  [33m%s [[-a|-b|-d] filename [NOLINE]][0m\n\n",node);
printf("  [33m%s[0m will increment the value in filename by 1 and write the new value\n",node);
printf("  back the file.\n\n");
printf("  Using the -a or -b option will display the value either\n");
printf("  before or after the value is incremented.\n");  
printf("  Use the -d option to simply display the value as it is stored.\n");
printf("  A newline will not be output if the NOLINE option is used.\n");
printf("  If [33m%s[0m is executed with a non-existent file, [33m%s[0m will create the\n",node,node);
printf("  file with an initial value of 0.\n\n");
exit(exitcode);
}

/*----------------------------------------------------------------------------*/

/*  MISTAKE FUNCTION  */

void mistake(description)
char description[80];
{
printf("\x0c");
makeline();
fprintf(stderr,"\n\n\07       [0mERROR:  [33m%s.[0m\07\n\n",description);
makeline();
helpscreen(5);
}

/*----------------------------------------------------------------------------*/
 
/* MAKELINE FUNCTION  */

void makeline()
{
fprintf(stderr,"[32m[1m  ___________________________________________________________________[0m\n");
}

/*----------------------------------------------------------------------------*/
