/* The Insult Construction Kit - TICK.
 * By Simon Champion - Pegasus Software
 * Started : 25/09/93.
 * Version : 1.0
 * Finished: 26/09/93.
 */

/* includes */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

/* defines */
#define NAME "The Insult Construction Kit - TICK!\n"
#define TRUE 1
#define FALSE 0

/* prototypes */
void help(void);
void error(int);

/* global variables */

/* main progam */
void main(int argc, char *argv[])
{
   int count, count2;  /* General purpose loop counters */
   FILE *fptr; /* The file pointer */
   char filename[40]="::"; /* Optional specifiable file to load from */
   char nameoff=FALSE; /* Toggle to stop the progname echoing on run */
   char test; /* Misc func return vaules testing */
   char *testptr; /* Also for returning funcion values. */
   char temp[30];      /* holds the data on loading */
   char pronoun[30];   /* <-+ these are the three   */
   char adjective[30]; /* <-+-three strings holding */
   char noun[30];      /* <-+ actual insult data.   */
   int numdat[3];      /* holds the number of items in each data segment */

   testptr=&test;      /* gives it an address to point to. */

   /* check arguments for options: */
   for(count=1; count<argc; count++)
   {
      if(argv[count-1][1]!='f' && argv[count-1][1]!='F' && argv[count][0]!='-')
      {
	 argv[count][1]=argv[count][0];
      }
      switch(argv[count][1])
      {
	 case '?' :
	 case 'h' :
	 case 'H' : help();
		    break;
	 case 'q' :
	 case 'Q' : nameoff=TRUE;
		    break;  /* Don't echo the title on opt Q */
	 case 'f' :
	 case 'F' : strcpy(filename,argv[count+1]);
		    break;
      }
      if(argv[count][0]=='?') help(); /* Allows for ? without a dash for opt */
   }
   if(nameoff==FALSE)
   {
      printf(NAME);
   }

   /* Now that the preliminaries are over, lets open that file... */
   /* There are a number of places it could be, so lets check them all. */
   /* First, check if -f has beenn specified. */
   if(filename!="::")
   {
      /* If not -f then check in all the preset places: */
      if( (fptr=fopen("Insult.data","r"))==NULL)     /* Curr Dir */
      {
	 if( (fptr=fopen("s:Insult.data","r"))==NULL)  /* S: */
	 {
	    if( (fptr=fopen("C:Insult.data","r"))==NULL) /* C: */
	       error(1);
	 }
      }
   } else /* If opt -f then load that file. */
   {
      if( (fptr=fopen(filename,"r"))==NULL)
	 error(1);
   }
   /* Right. File should now be open, or the prog shouldn't still be running
      so we can now get data from it. */
   /* First, let's seed our random number generator... */
   /* the rand() inside the srand() doesn't enhance the seeding of the
      random number, but it makes the same seed less likely when the
      program is run twice in quick succession. */
   srand(time(NULL)+rand());
   /* Next, we need to know how much data is in the file. */
   /* We'll do that by reading the whole file, to find out the length. */
   numdat[0]=0;
   numdat[1]=0;
   numdat[2]=0;
   count=0;
   while(count<3)
   {
     testptr=fgets(temp, 30,fptr); /* get the next item... */
     numdat[count]++;		   /* update num items in section */
     strcpy(noun,temp);
     noun[strlen(noun)-1]='';
     if(noun[1]=='$')              /* check for end of section */
	count++;		   /* if end of section, then next section */
     if(testptr==NULL)		   /* check for error/eof... */
     {
	test=feof(fptr);
	if(test==0)
	{
	   error(3);		   /* file read error! Unlikely, I hope.*/
	}else
	   error(2);		   /* or just a premature eof. */
     }
   }
   rewind(fptr); /* Get it back to the beginning again. */
   /* Now, let's get the data into the file... */
   count2=(rand()%(numdat[0]-1));
   for(count=0; count<numdat[0]; count++) /* First the pronoun section */
   {
      testptr=fgets(temp,30,fptr);
      if(count==count2)
      {
	 strcpy(pronoun,temp);
	 pronoun[strlen(pronoun)-1]='';
      }
   }
   count2=(rand()%(numdat[1]-1));
   for(count=0; count<numdat[1]; count++)/* Next the adjective section... */
   {
      testptr=fgets(temp,30,fptr);
      if(count==count2)
      {
	 strcpy(adjective,temp);
	 adjective[strlen(adjective)-1]='';
	 /* Turn A into AN if next to a vowel */
	 if(adjective[0]=='a' || adjective[0]=='e' || adjective[0]=='i'
	 || adjective[0]=='o' || adjective[0]=='u')
	 {
	    if(pronoun[strlen(pronoun)-1]=='a' && pronoun[strlen(pronoun)-2]==' ')
		strcat(pronoun,"n"); /* we have to replace to A too. */
	 }
      }
   }
   count2=(rand()%(numdat[2]-1));
   for(count=0; count<numdat[2]; count++)   /* And finally, the noun. */
   {
      testptr=fgets(temp,30,fptr);
      if(count==count2)
      {
	 strcpy(noun,temp);
	 noun[strlen(noun)-1]='';
      }
   }

   /* We should now, finally, have the data. Let's display it on screen */
   printf("%s %s %s.\n",pronoun,adjective,noun);

   /* and finally, we can quit. */
   close(fptr);
   exit(0);
}/* End of main(). */

void error(int ern)  /* reports errors and quits on a high dos error code */
{
   printf("Error: ");
   switch(ern)
   {
      case 1 : printf("Could not open data file.");
	       break;
      case 2 : printf("Invalid data file.");
	       break;
      case 3 : printf("File error.");
	       break;
      default: printf("Program error, code %d.",ern);
	       break;
   }
   printf("\n");
   exit(20);
}

void help(void)  /* This func shows the usage template & quits. */
{
   printf(NAME);
   printf("Pegasus Software, July 1994.\n");
   printf("Usage: Insult [HELP] [QUIET] [FILE <filename>]\n");
   printf("   or: Insult [-?|-h] [-q] [-f <filename>]\n");
   printf("       HELP or -h or -? : Usage template,\n");
   printf("       QUIET or -q      : Don't show title when run,\n");
   printf("       FILE or -f + name: User defined insult file.\n");
   exit(0);
}

