/***************************************************************************/
/*                                                                         */
/*      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.04a"
#define WRITTEN "06/19/89 - 12/08/89"

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

int escape_char = '*' ;
int error, character, offset;
int cflag = 2, cflag1, counter = 0, linesflag = 1, fflag = 0, sflag=0;
char officialrule[256]  = "Multiple color options" , node[35], *tempy;
char pathtorules[80] = "s:fortunes";
long randomseed, linecount, rndnumber;
unsigned char clock[8];
FILE *fp, *voice;

void main(int argc, char *argv[])
{
char option, opts[] = "";
int next;

/*----- 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();

/*----  Error Checking and Option Check  -------------------------------------*/

for( next = 1 ; (argopt(argc,argv,opts,&next,&option)) != NULL ; ) {
   switch(toupper(option)) {
      case '1':  if(cflag1) mistake(officialrule); cflag = 1; cflag1 = 1; break;
      case '2':  if(cflag1) mistake(officialrule); cflag = 2; cflag1 = 1; break;
      case '3':  if(cflag1) mistake(officialrule); cflag = 3; cflag1 = 1; break;
      case 'S':  sflag = 1;
                 voice = fopen("SPEAK:OPT/d0", "w");
                 if(voice == (FILE *)NULL)
                    mistake("Can't open SPEAK:    Is it MOUNTED?");
                 break;
      case 'L':  linesflag = 0; break;
       default:  mistake("Invalid Option");
      }
   } 
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");

if(access(pathtorules,4)) mistake("Can't find that file");

/*----  Open "fortunes" file    ----------------------------------------------*/

fp = fopen(pathtorules,"r");
if(fp == (FILE *)NULL) mistake("Can't open that file");

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("Program failure, terminal loop #1");  
      }

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

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

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

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

/*  CLEANUP FUNCTION  */

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

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

/*  HELPSCREEN FUNCTION  */

void helpscreen()
{
printf("\x0c");
makeline();
printf("\n  [33m[1m%s[0m    George Kerber %10s %25s\n\n",node,VERSION,WRITTEN);
printf("  [33m%s[0m will randomly select a [33mfortune[0m from the [33ms:fortunes[0m\n",node);
printf("  file and display it centered on the screen\n\n");
printf("  Escape Character is currently set to \"[1m[33m%c[0m\"\n\n",escape_char);
printf("  SYNTAX:  [33m%s[0m [-l | [-1 | -2 | -3]] [alternative path/name to data file][0m\n\n",node);
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();
printf("\n");
exit(0);
}

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

/*  MAKELINE FUNCTION  */

void makeline()
{

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[])
{
makeline();
printf("\n\07     [0mERROR: %s >[33m> %s.[0m\n",node,description);
makeline();
printf("\n");
exit(5);
}

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

/*  PRINT_RULE FUNCTION  */

void print_rule(int number, char rule[])
{
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

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