/* Test-plugin v1.1 rev.#2 by Deniil 715! for MultiRen
   Made 2000-07-04

   E-mail to me, Daniel Westerberg: deniil@algonet.se
*/

#include<proto/exec.h>
#include<proto/dos.h>
#include<proto/intuition.h>
#include<exec/tasks.h>
#include<intuition/intuition.h>
#include<stdlib.h>
#include<stdio.h>

#define _VER "$VER: Test-plugin v1.1 by Deniil 715! rev.#2 (2000-07-04)"
#define NUMSTRINGS_FOR_THIS_PLUGIN 2
#define FILE_NAME_LENGTH 512
#define COM_ASK 1
#define COM_EXTRACT 2
#define COM_CONFIGURE 3
#define COM_ABOUT 4
#define COM_QUIT 5
#define ERR_OK 0
#define ERR_NOMEM 1
#define ERR_NOFILE 2
#define ERR_NOSIG 3
#define ERR_NOTIMPL 4
#define ERR_UNKNOWN 5
#define ERR_OTHER 6
#define ERR_WRONGFORMAT 7
#define ERR_NOINFO 8
#define ERR_FATAL 20

struct multiren_plugin {
  long id;
  Task *task;
  long sig;
  short ret;
  char command;
  char numstrings;       /* You will */
  char *stringlist[256]; /* only use */
  char *name;            /* these 4 */
  char newname;          /* elements */
};

short ask(void);
short extract(void);
short configure(void);
short about(void);
short cleanup(void);
short req(char*,char*);

struct multiren_plugin *mrp;
char ascii[5], hex[9];

int main(int argc,char *argv[]) {
  char sigbit;
  long sig, msig;
  Task *mtask;
  if(DOSBase=(struct DosLibrary*)OpenLibrary("dos.library",0)) {
    if(IntuitionBase=(struct IntuitionBase*)OpenLibrary("intuition.library",0)) {
      if(argc==2) {
        if(mrp=(struct multiren_plugin*)atol(argv[1])) {
          if(mrp->id==*(long*)"MRPO") {
            if((sigbit=AllocSignal(-1))>=0) {
              sig=1<<sigbit;
              while(1) {
                switch(mrp->command) {
                case COM_ASK:
                  mtask=mrp->task;
                  msig=mrp->sig;
                  mrp->ret=ask();
                  SetProgramName(mrp->name);
                  mrp->task=FindTask(0L);
                  mrp->sig=sig;
                  break;
                case COM_EXTRACT:   mrp->ret=extract(); break;
                case COM_CONFIGURE: mrp->ret=configure(); break;
                case COM_ABOUT:     mrp->ret=about(); break;
                case COM_QUIT:
                  mrp->ret=cleanup();
                  FreeSignal(sigbit);
                  CloseLibrary((struct Library*)DOSBase);
                  Signal(mtask,msig);
                  return 0;
                default:
                  mrp->ret=ERR_UNKNOWN;
                }
                Signal(mtask,msig);
                Wait(sig);
              }
            }
            else {
              mrp->ret=ERR_NOSIG;
              Signal(mrp->task,mrp->sig);
              return 0;
            }
          }
        }
      }
      CloseLibrary((struct Library*)IntuitionBase);
    }
    CloseLibrary((struct Library*)DOSBase);
  }
  printf("This is a plugin for MultiRen!\n"
         "It is not supposed to be executed manually!\n"
         "Use it through the Renplacer tool in MultiRen instead!\n");
  return ERR_FATAL;
}

short ask() {
  mrp->numstrings=NUMSTRINGS_FOR_THIS_PLUGIN;  /* I will return 2 strings */
  mrp->stringlist[0]="First 4 bytes as hex";   /* Setting information-strings */
  mrp->stringlist[1]="First 4 bytes as ASCII";
  mrp->name="Test Plugin"; /* This plugins name */
  mrp->newname=0;          /* This plugin reads info from the file and it is
                              therefor unwise to request newname as it changes
                              and the file will not be found. */
  return ERR_OK; /* This plugin can't fail in initiation */
}

short extract() { /* Do my stuff.. */
  BPTR fh;
  long len;
  if(fh=Open(mrp->name,MODE_OLDFILE)) { /* Open the file that later is going to be renamed. */
    len=Read(fh,ascii,4L);
    Close(fh);
    hex[8]='\0';
    ascii[4]='\0';
    if(len!=4L) return ERR_OTHER;
    sprintf(hex,"%08lX",*(long*)ascii);
    mrp->stringlist[0]=hex;   /* Set the pointer so that MultiRen.. */
    mrp->stringlist[1]=ascii; /* ..can get my strings. */
    return ERR_OK; /* My stuff went ok */
  }
  return ERR_NOFILE; /* The file didn't open */
}

short configure() { return ERR_NOTIMPL; } /* There is no config for this plugin */

short about() {
  if(req("Test Plugin v1.1 rev.#2 by Deniil 715! for MultiRen\n\n"
         "It was made 2000-07-04 in Amiga-E\n\n"
         "E-mail: deniil@algonet.se","More|OK"))
    req("This plugin will return the first 4 bytes\n"
        "of the file to rename in two ways. The first\n"
        "string will be the 4 bytes in hexadecimal,\n"
        "the other one just as a plain string.","OK");
  return ERR_OK;
}

short cleanup() { return ERR_OK; }

short req(char *body,char *gads) {
  struct EasyStruct tags;
  tags.es_Title="Test Plugin";
  tags.es_TextFormat=body;
  tags.es_GadgetFormat=gads;
  return EasyRequestArgs(NULL,&tags,NULL,NULL);
}