#define __USE_SYSBASE
#include <proto/exec.h>
#include <exec/memory.h>
#include <proto/dos.h>
#include <proto/intuition.h>
#include <proto/icon.h>
#include <intuition/intuitionbase.h>
#include <string.h>

#define BLEU "\033[33m"
#define NORM "\033[0m"
#define TEMPLATE "FILE/A,NUMBER=NUM/N/K,NONUM/S,SIZE=SZ/N/K,HELP/S"

#define MSG BLEU"DSPP 3.04 (c) Thomas Pimmel 93-94 Allenbrand Brice 97\n"NORM
#define Malloc(x) AllocVec(x,MEMF_PUBLIC|MEMF_CLEAR)

#define ERR2 "Fichier de proverbe ou d'index introuvable\n"
#define ERR3 "Fichier de proverbe ou d'index corrompu\n"
#define ERR4 "Mémoire insuffisante\n"
#define HELP "Mauvais arguments\n"MSG"FILE/A   : Nom du fichier proverbe (sans extension)\nNUMBER/K : Numéro du proverbe à afficher\nNONUM/S  : Pas d'affichage du numéro\nSIZE/K : Largeur de la fenêtre SHELL\nHELP/S   : Afficher ce message\n\0$VER: DSPP 3.04 (24.4.97)"

ULONG Pipo(void)
{
 struct DosLibrary *DOSBase;
 struct ExecBase *SysBase;
 struct Library *IconBase;
 struct IntuitionBase *IntuitionBase;
 struct RDArgs *rdargs;
 struct DateStamp dt;
 struct Window *w;
 struct Process *PipoProc;
 struct Message *PipoMess;
 struct DiskObject *ic;
 unsigned int *buffer,l,num,cnt=0,fl=0;
 char *nom,*tn,*tnn="CON:50/50/500/100/DSPP";
 long opts[5],del;
 BPTR f,g;

 ic=NULL;
 SysBase=*((struct ExecBase **)4);
 if(!(nom=Malloc(100+100000)))
  return(0);
 PipoProc=(struct Process *)FindTask(NULL);
 if(!PipoProc->pr_CLI)
 {
  WaitPort(&PipoProc->pr_MsgPort);
  PipoMess=GetMsg(&PipoProc->pr_MsgPort);
  strcpy(nom,"ProgDir:");
  strcat(nom,PipoProc->pr_Task.tc_Node.ln_Name);
  if((!(IconBase=OpenLibrary("icon.library",0)))||(!(ic=GetDiskObject(nom))))
   goto a;
 }
 if(DOSBase=(struct DosLibrary *)OpenLibrary("dos.library",0))
 {
  if(IntuitionBase=(struct IntuitionBase *)OpenLibrary("intuition.library",0))
  {
   if(ic)
   {
    if(!(tn=FindToolType(ic->do_ToolTypes,"FENETRE")))
     tn=tnn;
    if(!(g=Open(tn,MODE_NEWFILE)))
     goto b;
    if(tn=FindToolType(ic->do_ToolTypes,"DELAI"))
     StrToLong(tn,&del);
    else
     del=8;
    del*=50;
   }
   else
    g=Output();
   num=LockIBase(0);
   w=IntuitionBase->ActiveWindow;
   UnlockIBase(num);
   num=-1;
   memset(opts,0,sizeof(opts));
   if(ic||(rdargs=ReadArgs(TEMPLATE,opts,NULL)))
   {
    if(opts[4])
     PutStr(HELP);
    else
    {
     if(opts[1])
      num=*((unsigned int *)opts[1]);
     if(ic)
      if(tn=FindToolType(ic->do_ToolTypes,"FICHIER"))
       strcpy(nom,tn);
      else
       strcpy(nom,"ProgDir:Proverbes");
     else
      strcpy(nom,((char **)opts)[0]);
     strcat(nom,".INDEX");
     if(f=Open(nom,MODE_OLDFILE))
     {
      buffer=(unsigned int *)(nom+100);
      l=(Read(f,buffer,100000)>>2)-1;
      if(*buffer==0x50524F56)
      {
       if(num>l)
       {
        DateStamp(&dt);
        num=((dt.ds_Tick*dt.ds_Tick)%l)+1;
       }
       if(!opts[2])
        FPrintf(g,"Proverbe %lu\n",num);         
       num=buffer[num];
       Close(f);
       for(tn=nom;*tn!='.';tn++);
       strcpy(tn,".PROV");
       if(f=Open(nom,MODE_OLDFILE))
       {
        Seek(f,num,OFFSET_BEGINNING);
        if(opts[3])
         l=*((unsigned int *)opts[3]);
        else
         l=(w->Width-w->BorderLeft-w->BorderRight)/w->IFont->tf_XSize;
        nom[0]=0;
        for(;;)
        {
         num=FGetC(f);
         if(num=='|')
          break;
         if(num==-1)
         {
          FPrintf(g,"%s",ERR3);
          break;
         }
         if((num==' ')||(num=='\n'))
         {
          if(fl)
           continue;
          fl=1;
          nom[++nom[0]]=0;
          if(cnt>=l)
          {
           FPuts(g,"\n");
           cnt=nom[0];                   
          }
          else
           cnt++;
          FPrintf(g,"%s%lc",nom+1,num);
          if(num=='\n')
           cnt=0;
          nom[0]=0;
         }
         else
         {
          nom[++nom[0]]=num;
          cnt++;
          fl=0;
         }
        }
        nom[++nom[0]]=0;
        if(cnt>=l)
         FPuts(g,"\n");
        FPrintf(g,"%s\n",nom+1);
       }
      }
      else
       FPrintf(g,"%s",ERR3);
     }
     if(f)
      Close(f);
     else
      FPrintf(g,"%s",ERR2);
    }
    if(!ic)
     FreeArgs(rdargs);
    else
     Delay(del);
   }
   else
    PutStr(HELP);
b:
   if((ic)&&(g))
    Close(g);
   CloseLibrary((struct Library *)IntuitionBase);
  }
  CloseLibrary((struct Library *)DOSBase);
 }
a:
 FreeVec(nom);
 if(!PipoProc->pr_CLI)
 {
  if(ic) 
   FreeDiskObject(ic);
  if(IconBase)
   CloseLibrary(IconBase);
  Forbid();
  ReplyMsg(PipoMess);
 }
 return(0);
}
