#include <intuition/intuitionbase.h>
#include <pragma/intuition_lib.h>
#include <pragma/exec_lib.h>
#include <stdlib.h>
#include <stream.h>
#include <string.h>
#include <escseq.h>

struct EasyStruct es = {
   sizeof (EasyStruct), 0, "AskReq (c) by Harald Pehl in 1993", NULL, "Yes|No"};
struct IntuitionBase *IntuitionBase;
struct Window *actwin;
unsigned long lock;


void close_all (int how, char *why)
{
   if (IntuitionBase)   CloseLibrary ((struct Library *)IntuitionBase);
   if (why)             cout << BOLD_ON"\n\n" << why << "\n"BOLD_OFF;
   exit (how);
}


void usage ()
{
   SET_A_PEN (2)
   cout << BOLD_ON;
   cout << "\nAskReq (c) by Harald Pehl in 1993\n\n";
   SET_A_PEN (1)
   cout << "Usage: AskReq [-tTitle] [-rReqText] [-gGadText]\n\n";
   cout << BOLD_OFF;
   cout << "Title:      The title of the requester.\n";
   cout << "ReqText:    The text of the requester.\n";
   cout << "GadText:    The text(s) of the gadgets (max 2).\n\n";
   cout << "To use several lines in the requester or\n"
           "two gadget the text is to be split with \"|\"\n\n";
   cout << "Returncode: 0     if the left or the only gadget was selected\n"
           "            WARN  if the right gadget was selected\n"
           "            ERROR if an error occurred\n\n";
   close_all (10, NULL)
}


void main (int argc, char **argv)
{
   char *s = 0, count = 1; 
      
   if (! (IntuitionBase = (struct IntuitionBase *) 
      OpenLibrary ("intuition.library",37)))
         close_all (10, "No Kick 2.0 !!");
   lock = LockIBase (0);
   actwin = IntuitionBase -> ActiveWindow;
   UnlockIBase (lock);
   while (--argc > 0 && **++argv == '-')
   {
      switch (*++argv[0])
      {
         case 't':
            es.es_Title = *argv+1;
            break;
         case 'r':
            s = *argv+1;
            while (*s++)
               if (*s == '|')
                  *s = '\n';
            es.es_TextFormat = *argv+1;
            break;
         case 'g':
            count = 0;
            s = *argv+1;
            while (*s++)
               if (*s == '|')
                  count++;
            es.es_GadgetFormat = *argv+1;
            break;
         default:
            cout << "\nError: illegale option \"" <<**argv<< "\".\n";
            argc = 1;
            break;
      }
   }
   if (argc || count > 1)
      usage ();
   if (EasyRequestArgs (actwin, &es, NULL, NULL) && count == 1)
      close_all (5, NULL)
   else
      close_all (0, NULL);
}
