/* Swapname-plugin v1.1a rev.#2 by Deniil 715! for MultiRen
   Made 2002-05-24

   My e-mail: 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>
#include<string.h>

/* This is how many strings this plugin will return. */
#define NUMSTRINGS_FOR_THIS_PLUGIN 1

/* Private, don't touch! */
#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

/* Use these as return values wherever suitable.
Return ERR_OK if all went well and the string are filled.
*/
#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 */
};

const char VersionString[]="$VER: Swapname-plugin v1.1a by Deniil 715! rev.#2 (2002-05-24)"

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

struct multiren_plugin *mrp;
char  s[FILE_NAME_LENGTH+1];
short inclext=1;
BPTR  fh;

short 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 one string */
  mrp->stringlist[0]="Filename turned backwards"; /* Setting information-strings */
  mrp->name="SwapName"; /* This plugins name */
  mrp->newname=1;       /* As this plugin is doing stuff with the filename
                           and not the file it is wise to request New name. */
  if(fh=Open("PROGDIR:Plugins/SwapName-plugin.cfg",MODE_OLDFILE)) { /* Open configfile */
    inclext=FGetC(fh); /* The prefs telling if we should include
                          extension in the swap or not.. */
    Close(fh);
  }
  return ERR_OK;
}

short extract() { /* Do my stuff.. */
  int  x, y, z;
  char *st;
  st=FilePart(mrp->name); /* Note that we get the complete path in name
                             and must separate it to get the filename. */
  x=strlen(st);
  if(x<FILE_NAME_LENGTH) {
    if(inclext) {
      for(y=0;y<x;y++) s[y]=st[x-1-y];
      s[x]='\0';
    }
    else {
      for(z=x;z>=0;z--) if(st[z]=='.') break;
      if(z>0) z--; else z=x;
      for(y=0;y<=z;y++) s[y]=st[z-y];
      s[z+1]='\0';
      if(z<x) strcat(s,st+z+1);
    }
    mrp->stringlist[0]=s; /* Setting the string-list position 0 to point to our string */
    return ERR_OK;
  }
  else
    return ERR_OTHER; /* Filename was longer than the set max-length for MultiRen v1.3 */
}

short configure() {
  char *str;
  if(inclext) str="Yes"; else str="No";
  sprintf(s,"Do you want the extension to be\n"
            "part of the filename swapping??\n"
            "Current state: %s",str);
  inclext=req(s,"Yes|No|Cancel");
  if(inclext) {
    if(fh=Open("PROGDIR:Plugins/SwapName-plugin.cfg",MODE_NEWFILE)) {
      FPutC(fh,inclext & 1);
      Close(fh);
    }
    else
      req("Could not save the settings!","OK");
  }
  return ERR_OK; /* Will always return ERR_OK here so that MultiRen will not
                    put up another requester telling this operation failed. */
}

short about() {
  if(req("SwapName Plugin v1.1a rev.#2 by Deniil 715! for MultiRen\n\n"
         "It was made 2002-05-24 in Amiga-E\n\n"
         "E-mail: deniil@algonet.se","More|OK"))
    req("This plugin will swap the filename backwards.\n"
        "It features a setting to include the file-\n"
        "extension in the swapping or not.","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);
}