#include <stdio.h>
#include <stdlib.h>
#include "testpluginlib.h"
#include "../environ.h"

int main(int argc, char **argv)
{
   char        *lib        = NULL;
   char        *plugname   = NULL;
   char        *arg        = NULL;
   void        *libhandle  = NULL;
   PLUGINREF   pr          = NULL;
   int         n           = 0;

   if (argc == 3)
   {
      lib = PLUGINLIB;
      plugname = argv[1];
      arg = argv[2];
   }
   else
   if (argc == 4)
   {
      lib = argv[1];
      plugname = argv[2];
      arg = argv[3];
   }
   else
   {
      fprintf(stderr, "Usage: testpluginlib pluginName testString\n");
      fprintf(stderr, "   or: testpluginlib libName pluginName testString\n");
      exit(EXIT_FAILURE);
   }

   if ((libhandle = pluginlib_open(lib)) == NULL)
   {
      logmsg("Error: %s", pluginlib_error());
      exit(EXIT_FAILURE);
   }

   pr = (PLUGINREF)plugin_getref(libhandle, plugname);
   if (!pr)
   {
      logmsg("Error: getting plugin: %s", pluginlib_error());
      exit(EXIT_FAILURE);
   }

   n = pr(arg);
   fprintf(stdout, "%d = %s(%s)\n", n, plugname, arg);
   
   pluginlib_close(libhandle);
   return(EXIT_SUCCESS);
}

