/*  Amiga Fortunes v 1.0.8 - Copyright © 2001 Kalle Räisänen.

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <stdio.h>
#include <time.h>
#include <exec/types.h>
#include <dos/dos.h>
#include <intuition/intuition.h>
#include <clib/dos_protos.h>
#include <clib/exec_protos.h>
#include <clib/intuition_protos.h>
#define MAXLENTH 5000
#define TRUE 1
#define FALSE 0
#define CR 13
#define COPYRIGHT "Amiga Fortunes v 1.0.8 - Copyright © 2001 Kalle Räisänen"
static UBYTE *VersTag = "\0 $VER: Amiga Fortunes 1.0.8";
struct IntuitionBase *IntuitionBase;
int procent, dialog = TRUE;
char text[MAXLENTH];

const char defaultfile[] = "sys:s/fortunes.txt";
const char gpl_text[] = {"This program is free software; you can redistribute it and/or modify\n"
                 "it under the terms of the GNU General Public License as published by\n"
                 "the Free Software Foundation; either version 2 of the License, or\n"
                 "(at your option) any later version.\n\n"
                 "This program is distributed in the hope that it will be useful,\n"
                 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
                 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n"
                 "GNU General Public License for more details.\n\n"
                 "You should have received a copy of the GNU General Public License\n"
                 "along with this program; if not, write to the Free Software\n"
                 "Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.\n\n"};

const char help_text[] = {"Usage:   afortunes [-t|--text] [filename(s)|option]\n"
                    "Where filename(s) is the path to the custom file(s) you wish"
                    "to use.\n\n"
                    "Options: \n"
                    "-c, --copyright:    Show copyright information.\n"
                    "-h, --help     :    Show this text.\n"
                    "The -t (or --text) switch makes AFortunes use CLI-output.\n"};

getprocs(BPTR fp)
{
  char c;
  int procs = 0, num = FALSE;

  while(c = FGetC(fp)){
     if(c == ']')
        return procs;
     if(num){
        procs *= 10;
        procs += (c - '0');
     }
     if(c == '[')
        num = TRUE;
  }
}

void putreq(char *s, char *t)
{
   struct EasyStruct fortreq;

   if(IntuitionBase=(struct IntuitionBase*) OpenLibrary("intuition.library",37L)){
      fortreq.es_StructSize = sizeof(struct EasyStruct);
      fortreq.es_Flags = 0;
      fortreq.es_Title = s;
      fortreq.es_TextFormat = t;
      fortreq.es_GadgetFormat = "Ok";

      EasyRequest(NULL, &fortreq, NULL);
   }
   else
      printf("Error opening intuition.library.\n");
}

void putquote(BPTR file)
{
   char c;
   int i = 0, procs = 0, quote_num, j = 0, maybe = 0;

   srand((unsigned int)time((time_t *)NULL));
   quote_num = rand() % procent + 1;

   while((c = FGetC(file)) && (procs <= quote_num) && i <= MAXLENTH){
      if(procs == quote_num && c != CR){
         text[i] = c;
         i++;
      }
      if(c == '\n' && maybe == 0){
         maybe++;
         j = 0;
      }
      else if(c == '%' && maybe == 1 && j == 1)
         procs++;
      else
         maybe = 0;
      j++;
   }
   text[i-2] = '\0';

   if(dialog)
      putreq(COPYRIGHT, text);
   else
      printf("%s\n\n",text);
}

int compare(char *s, char *t)
{
    int l = 0;

    for(; s[l] == t[l]; l++){
       if(s[l] == '\0')
          return 1;
    }
    return 0;
}

void notice(char *s)
{
   if(dialog)
      putreq(COPYRIGHT, s);
   else
      printf("\n%s\n\n%s\n\n", COPYRIGHT, s);
}

void openfile(char *fpath)
{
   BPTR myfile;
   if(myfile = Open(fpath, MODE_OLDFILE)) {
      procent = getprocs(myfile);
      Close(myfile);
      if(myfile = Open(fpath, MODE_OLDFILE)) {
         putquote(myfile);
         Close(myfile);
      }
   }
   else
      openfile(defaultfile);
}
main(int argc, char *argv[])
{
   srand((unsigned int)time((time_t *)NULL));

   if(argc == 2){
      if(compare(argv[1], "--copyright") || compare(argv[1], "-c"))
         notice(gpl_text);
      else if(compare(argv[1], "--help") || compare(argv[1], "-h"))
         notice(help_text);
      else if(compare(argv[1], "--text") || compare(argv[1], "-t")){
         dialog = FALSE;
         openfile(defaultfile);
      }
      else
         openfile(argv[1]);
   }
   else if(argc > 2){
      if(compare(argv[1], "--text") || compare(argv[1], "-t")){
         dialog = FALSE;
         if(compare(argv[2], "--copyright") || compare(argv[2], "-c"))
            notice(gpl_text);
         else if(compare(argv[2], "--help") || compare(argv[2], "-h"))
            notice(help_text);
         else
            openfile(argv[(rand()%(argc-2))+2]);
      }
      else
         openfile(argv[(rand()%(argc-1))+1]);
   }
   else
      openfile(defaultfile);
   return 0;
}
