#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, "Ja|Nein"};
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 << "Syntax: AskReq [-tTitel] [-rReqText] [-gGadText]\n\n";
   cout << BOLD_OFF;
   cout << "Titel:       Der Titel des Requester.\n";
   cout << "ReqText:     Der Text im Requester.\n";
   cout << "GadText:     Die Texte der Gadgets (max. 2)\n\n";
	cout << "Um mehrzeiligen Text oder zwei Gadgets zu\n"
			  "benutzten, muß der Text mit \"|\" getrennt werden.\n\n";
   cout << "Returncode:  0     wenn das linke bzw. einzige Gadget selektiert wurde\n"
           "             WARN  wenn das rechte Gadget selektiert wurde\n"
           "             ERROR wenn ein Fehler auftrat\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, "Kein 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 << "\nFehler: 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);
}
