/*-------------------------------------------------------------------------*/
/*      Program:  incr                                                     */
/*   Programmer:  George Kerber                                            */
/*      Written:  07/24/89                                                 */
/*     Compiler:  Lattice 5.04                                             */
/*-------------------------------------------------------------------------*/

#include <stdio.h>
#include <string.h>
#include <dos.h>
#include <stdlib.h>

#define WRITTEN "07/24/89 - 01/01/90"
#define VERSION "v1.04b"

void mistake();
void helpscreen();
void exitcode();
void main();
  
void main(int argc, char *argv[])
{
FILE *tfile;
char node1[31];
char option, filename[80] = "Invalid Option Combination";
char values[80] = "Multiple Color Options", opts[] = "";
char drive[FNSIZE],path[FMSIZE],node[FNSIZE],ext[FESIZE];
long value1,value2;
int i, next, oflag = 0, nflag = 0, cflag = 0;

stcgfn(node1,argv[0]);
if(argc == 1 || (argc == 2 && argv[1][0] == '?')) helpscreen(node1);

for( next = 1 ; (argopt(argc,argv,opts,&next,&option)) != NULL ; ) {
   switch(option) {
      case 'a':   if(oflag) mistake(filename,node1);       /*  after       */
                  oflag = 'a' ; break ;              
      case 'b':   if(oflag) mistake(filename,node1);       /*  before      */
                  oflag = 'b' ; break ;             
      case 's':
      case 'l':   if(oflag) mistake(filename,node1);
                  oflag = 'l' ; break ;                  /*  show value  */
      case 'd':   if(oflag) mistake(filename,node1);     /*  delete file */
                  oflag = 'd';  break ;
      case 'n':   nflag = 1; break ;                     /*  noline      */
      case '3':   if(cflag) mistake(values,node1);
                  printf("[33m"); cflag = 1; break;    /*  orange output */
      case '2':   if(cflag) mistake(values,node1);
                  printf("[32m"); cflag = 1; break;    /*  black output  */
      case '1':   if(cflag) mistake(values,node1);
                  printf("[31m"); cflag = 1; break;    /*  white output */
      case '5':   printf("[1m");  break ;              /*  bold output   */
      case '6':   printf("[4m");  break ;              /*  underline     */
      case '7':   printf("[3m");  break ;              /*  italics       */
       default:  mistake("Invalid Option",node1);
      }
   }

/*----- Begin error & option checking.-------------------------------------*/
if(next >= argc) mistake("Missing INCR name",node1);
if(argv[next][0] == '-') mistake("Missing INCR Name",node1);
if(argc > (next + 1)) mistake("Invalid Option Count",node1);
/*----- End error checking ------------------------------------------------*/

strsfn(argv[next],drive,path,node,ext);

if(strcmp(ext,"") && strcmp(ext,"incr")) 
   mistake("Filename extension defaults to \".incr\"",node1);

strcpy(ext,"incr");
if(drive[0] == '\0' && path[0] == '\0') { 
   strcpy(drive,"s:");
   strcpy(path,"incr");
   if(access("s:incr",0)) mkdir("s:incr"); }

if(drive[0] == '\0' && path[0] != '\0')
   mistake("Full path must be specified",node1);

strmfn(filename,drive,path,node,ext);   /*  create destination file name   */

if(strcmp(ext,"") && strcmp(ext,"incr")) {
   mistake("Filename extension defaults to \".incr\"",node1);
   }

strcpy(ext,"incr");

strmfn(filename,drive,path,node,ext);

if(oflag == 'd') {
   if(remove(filename)) {
      exitcode(5);
      }
      else {
      exitcode(0);
      }
   }

if(!access(filename,0)) {
   tfile = fopen(filename,"r+");
   if(tfile == (FILE *)NULL) mistake("Can't open file for reading",node1);
   fgets(values,80,tfile);
   fclose(tfile);
   for(i = (strlen(values) - 2 ) ; i >= 0 ; i--) {    
      if(!isdigit(values[i])) mistake("Incorrect file format",node1);
      } 
   value1 = atol(values);
   if(oflag != 'l') {
      value2 = value1 + 1;
      if(strcmp(argv[1],"-d")) {
         remove(filename);
         tfile = fopen(filename,"a"); 
         if(tfile == (FILE *)NULL)
            mistake("Can't write incremented value",node1);
         stcl_d(values,value2);
         fprintf(tfile,"%s",values);
         fprintf(tfile,"\n");  
         fflush(tfile);
         fclose(tfile);
         }
      }
   }
   else {
   tfile = fopen(filename,"w");
   if(tfile == (FILE *)NULL) mistake("Can't create new file",node1);
   fprintf(tfile,"1");
   fflush(tfile);
   fclose(tfile);
   value1 = 0;
   value2 = 1;
   }



if(argc > 2) {
   switch(oflag) {
      case 'a':  printf("%ld",value2) ; break ;
      case 'b':  printf("%ld",value1) ; break ;
      case 'l':  printf("%-6ld",value1); break ;

       default:  break ;
      }
   }

if(!nflag && (oflag == 'a' || oflag == 'b' || oflag == 'l')) printf("\n");
exitcode(0);

}

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

/*  HELPSCREEN FUNCTION  */

void helpscreen(char node1[])
{
printf("\x0c\n\n  [33m[1m%s[0m          George Kerber  %10s  %25s\n\n",
   node1,VERSION, WRITTEN);
printf("  SYNTAX:  [33m%s [[-a|-b|-l -n -d -#] [drive:path/]filename][0m\n\n",node1);
printf("           -a  Value is displayed after it is incremented.\n");
printf("           -b  Value is displayed before it is incremented.\n");
printf("           -l  Value is only displayed.\n");
printf("           -d  INCR file is unconditionally deleted.\n");
printf("           -n  Newline is not output after value is displayed.\n");
printf("           -#  Color, bold, etc....\n\n");
printf("  [33m%s[0m will increment the value in filename by 1 and write the new value\n",node1);
printf("  back the file.\n\n");
printf("  If no path is specifed, [33m%s[0m will use [33ms:incr[0m for all files\n",node1);
printf("  and create the [33ms:incr[0m directory if necessary.\n");
printf("  If [33m%s[0m is executed with a non-existent file, [33m%s[0m will create the\n",node1,node1);
printf("  file with an initial value of 1.\n\n");
exitcode(0);
}

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

/*  MISTAKE FUNCTION  */

void mistake(char description[],char node1[])
{
fprintf(stderr,"\n\07[0m   ERROR:  %s --> [33m%s.[0m\07\n\n",
   node1,description);
exitcode(5);
}

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

/*  EXITCODE FUNCTION  */

void exitcode(int x)
{
printf("[0m");
exit(x);
}

/*-------------------------------------------------------------------------*\

11/01/89 v1.04a:  Re-compiled with Lattice 5.04
01/01/90 v1.04b:  removed all global variables

\*-------------------------------------------------------------------------*/
