/*-------------------------------------------------------------------------*/
/*      Program:  fortune                                                  */
/*   Programmer:  George Kerber                                            */
/*      Written:  06/19/89                                                 */
/*      Purpose:  Similar to the UNIX Fortune, but centers the lines.      */
/*     Compiler:  Lattice 5.04                                             */
/*-------------------------------------------------------------------------*/

#include <stdio.h>
#include <string.h>
#include <dos.h>
#include <ctype.h>
#include <stdlib.h>
#define VERSION "v2.04c"
#define WRITTEN "06/19/89 - 01/07/90"

void main();
void helpscreen();
void mistake();
void cleanup();
void makeline();
void print_rule();

void main(int argc, char *argv[])
{
char officialrule[256]  = "Multiple color options", option, opts[] = "";
char *tempy, node[35], pathtorules[80] = "s:fortunes";
int next, error, character, counter = 0, linesflag = 1, sflag = 0;
int escape_char = '*', cflag = 2, cflag1 = 0;
unsigned char clock[8];
long linecount, rndnumber;
FILE *fp, *voice;

/*----- Sets escape value -------- default is * ---------------------------*/
tempy = getenv("ESCAPE");
if(tempy != NULL) escape_char = *tempy;
/*-------------------------------------------------------------------------*/
      
getclk(clock);
stcgfn(node,argv[0]);
if(strcmp(argv[1],"?") == 0 && argc > 1) helpscreen(escape_char,node,cflag);

/*----  Error Checking and Option Check  ----------------------------------*/
for( next = 1 ; (argopt(argc,argv,opts,&next,&option)) != NULL ; ) {
   switch(toupper(option)) {
      case '1':  if(cflag1) mistake(officialrule,node); cflag = 1; 
                 cflag1 = 1;
                 break;
      case '2':  if(cflag1) mistake(officialrule,node); cflag = 2; 
                 cflag1 = 1;
                 break;
      case '3':  if(cflag1) mistake(officialrule,node); cflag = 3; 
                 cflag = 1;
                 break;
      case 'S':  sflag = 1;
                 voice = fopen("SPEAK:OPT/d0", "w");
                 if(voice == (FILE *)NULL)
                    mistake("Can't open SPEAK:    Is it MOUNTED?",node);
                 break;
      case 'L':  linesflag = 0; break;
       default:  mistake("Invalid Option",node);
      }
   } 
if(sflag) linesflag = 0;
if(argc > next) {
   strcpy(pathtorules,(argv[next]));
   }
   else {
   if(!access("ram:fortunes",4)) strcpy(pathtorules,"ram:fortunes");
   }

if(argc > (next + 1)) mistake("Invalid Option Count",node);

if(access(pathtorules,4)) mistake("Can't Locate File",node);

/*----  Open "fortunes" file    -------------------------------------------*/
fp = fopen(pathtorules,"r");
if(fp == (FILE *)NULL) mistake("Can't Open File",node);

srand(clock[7] + clock[6] + clock[3] + clock[4]);
fseek(fp,-1L,2);
linecount = ftell(fp);
counter = 0;
while(1) {
   rndnumber = ((rand() % linecount) + 1) ;
   error = fseek(fp,rndnumber,0);
   if(error != 0) {
      if(counter++ > 25) mistake("#1",node);  
      }

   counter = 0;
   while((character = fgetc(fp)) != EOF) {
      if(character == '\n') break; 
      if(counter++ > 100) mistake("#2",node); 
      continue; 
      }

   if(fgets(officialrule,256,fp) == (char *)NULL) {
      continue; 
      }
      else {
      if(officialrule[0] == ' ' || officialrule[0] == '#') continue;
      if(linesflag == 1) makeline(cflag);
      if(!sflag) printf("\n");
      print_rule(81,officialrule,escape_char,sflag,voice);
      }

   counter = 0;
   while(1) {
      if(fgets(officialrule,256,fp) == (char *)NULL) {
         cleanup(linesflag,sflag,cflag);
         }
         else {
         if(officialrule[0] != ' ' || officialrule[0] == '\n') {  
            cleanup(linesflag,sflag,cflag); }
         print_rule(80,officialrule,escape_char,sflag,voice);
         if(counter++ >= 10) cleanup(linesflag,sflag,cflag);
         }
      continue; 
      }
   if(linesflag == 1) makeline(cflag);
   }
}

/*-------------------------------------------------------------------------*/
/*      End of main program, functions are below                           */
/*-------------------------------------------------------------------------*/

/*  CLEANUP FUNCTION  */

void cleanup(int linesflag,int sflag,int cflag)
{
if(linesflag == 1) makeline(cflag);
if(!sflag) printf("\n");
fcloseall();
exit(0);
}

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

/*  HELPSCREEN FUNCTION  */

void helpscreen(char escape_char, char node[],int cflag)
{
printf("\x0c");
makeline(cflag);
printf("\n  [33m[1m%s[0m    George Kerber %10s %25s\n\n",node,VERSION,WRITTEN);
printf("  [33m%s[0m randomly selects a [33mfortune[0m from the [33ms:fortunes file.[0m\n\n",node);
printf("  Escape Character is currently set to \"[1m[33m%c[0m\"\n\n",escape_char);
printf("  SYNTAX:  [33m%s[0m [-l | -s | [-1 | -2 | -3]] [alternative path/name][0m\n\n",node);
printf("           -s  fortune is spoken using the Amiga SPEAK: device.\n");
printf("           -l  fortune is displayed with no outline.\n");
printf("           -1  fortune is displayed with color 1  (white) outline.\n");
printf("           -2  fortune is displayed with [32mcolor 2  (black) [0moutline (default)\n");
printf("           -3  fortune is displayed with [33mcolor 3 (orange) [0moutline.\n\n");
printf("  The [33mfortunes[0m file should be located in the [33ms:[0m directory or provide\n");
printf("  an alternative path/name as an option.\n");
printf("  See program documentation for [33mfortunes[0m file syntax.\n");
makeline(cflag);
printf("\n");
exit(0);
}

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

/*  MAKELINE FUNCTION  */

void makeline(int cflag)
{

printf("[1m");
if(cflag == 1) printf("[31m");
if(cflag == 2) printf("[32m");
if(cflag == 3) printf("[33m");

printf("  ___________________________________________________________________________[0m\n");
}

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

/*  MISTAKE FUNCTION  */

void mistake(char description[],char node[35])
{
printf("\n\n\07     [0mERROR: %s -->[33m> %s.[0m\n\n",
   node,description);
exit(5);
}

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

/*  PRINT_RULE FUNCTION  */

void print_rule(int number, char rule[],int escape_char,int sflag,FILE *voice)
{
int counter = -1, offset = 0, i;
char fortune[256];

fortune[0] = '\0';
while(rule[++counter] != (char *)NULL) {
   if(rule[counter] == escape_char) {
      if(rule[++counter] == escape_char) { 
         i = strlen(fortune) - 1;
         fortune[i + 1] = escape_char;
         fortune[i + 2] = '\0';
         offset++;
         continue; 
         }
      if(!sflag) {
      switch(rule[counter]) {
         case '0':  strcat(fortune,"[30m"); break; /* color 0 char       */
         case '1':  strcat(fortune,"[31m"); break; /* color 1 char       */
         case '2':  strcat(fortune,"[32m"); break; /* color 2 char       */
         case '3':  strcat(fortune,"[33m"); break; /* color 3 char       */
         case '4':  strcat(fortune,"[0m");  break; /* default attr       */
         case '5':  strcat(fortune,"[1m");  break; /* bold char          */
         case '6':  strcat(fortune,"[4m");  break; /* underline char     */
         case '7':  strcat(fortune,"[3m");  break; /* italics char       */
         case ')':  strcat(fortune,"[40m"); break; /* color 0 bkgnd      */
         case '!':  strcat(fortune,"[41m"); break; /* color 1 bkgnd      */
         case '@':  strcat(fortune,"[42m"); break; /* color 2 bkgnd      */
         case '#':  strcat(fortune,"[43m"); break; /* color 3 bkgnd[0m */
         }
      }
   }
      else { 
      i = strlen(fortune) - 1;
      fortune[i + 1] = rule[counter];
      fortune[i + 2] = '\0';
      offset++;
      }
   continue;
   }

offset = ((number - offset) / 2);
for( ; offset > 0 ; strins(fortune," "),offset--);
if(!sflag) {
   printf("%s[0m",fortune); 
   }
   else {
   fprintf(voice,"%s",fortune); 
   }
}

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

10/07/89 v2.03:  Added escape and color options
11/01/89 v2.03b: Recompiled with Lattice 5.04
01/07/90 v2.03c: Removed all global variables

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