/*
 *  Request.c - open an "EasyRqeuester()" from script files
 *
 *  PUBLIC DOMAIN
 *
 *  by Stefan Sticht
 *
 *  V1.00 initial release
 */

#define VERSION "V1.00"

#include <stdlib.h>
#include <intuition/intuition.h>

#include <clib/dos_protos.h>
#include <pragmas/dos_pragmas.h>
#include <clib/exec_protos.h>
#include <pragmas/exec_pragmas.h>
#include <clib/intuition_protos.h>
#include <pragmas/intuition_pragmas.h>

/*
 *  some SAS/C specularities, change for other compilers!
 */
#ifdef __SASC
extern struct Library *DOSBase; /* Library base pointer from startup code */
extern struct Library *SysBase; /* Library base pointer from startup code */
void chkabort(void) {}          /* disable Ctrl-C detect */
#endif

#define PROGRAMTITLE "Request"

/*
 *  a string, which will be found by Version
 */
char version[] ="\0$VER: " PROGRAMTITLE " " VERSION;

#define USAGE "Usage: Request <text> [TITLE <title>] "\
              "[GADGETS <gad1|gad2|gad3|...>] [Screen <publicscreenname>]"

struct Library *IntuitionBase;

/*
 *  dummy window, we have to open
 */
struct NewWindow dummywindow = {
    0, 0,
    1, 1,
    -1, -1,
    0l,
    BORDERLESS | BACKDROP | SIMPLE_REFRESH,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    0, 0,
    0, 0,
    CUSTOMSCREEN
};

struct EasyStruct textreq = {
    sizeof (struct EasyStruct), /* ULONG es_StructSize      */
    0l,                         /* ULONG es_Flags           */
    NULL,                       /* UBYTE *es_Title          */
    NULL,                       /* UBYTE *es_TextFormat     */
    NULL,                       /* UBYTE *es_GadgetFormat   */
    };

#define TEMPLATE "Text/A,Title/K,Gadgets/K,Screen/K"
#define OPT_TEXT    0
#define OPT_TITLE   1
#define OPT_GADGETS 2
#define OPT_SCREEN  3
#define OPT_COUNT   4

/*
 *  this array will be filled by ReadArgs()
 */
long options[OPT_COUNT];

void _main(char *line)
{
    struct RDArgs *args;
    struct Screen *scr;
    struct Window *win = NULL;
    char *screen = NULL;
    long rc = 0l;

    if (IntuitionBase = OpenLibrary("intuition.library", 37l)) {

        if (args = ReadArgs((UBYTE *)TEMPLATE, options, NULL)) {

            if (options[OPT_TITLE])   textreq.es_Title = (UBYTE *)options[OPT_TITLE];
            if (options[OPT_GADGETS]) textreq.es_GadgetFormat = (UBYTE *)options[OPT_GADGETS];
            else textreq.es_GadgetFormat = "Ok";
            if (options[OPT_TEXT])    textreq.es_TextFormat = (UBYTE *)options[OPT_TEXT];
            if (options[OPT_SCREEN])  screen = (char *)options[OPT_SCREEN];

            if (screen && *screen) {
                /*
                 *  user wants a special screen, lock it
                 */
                if (scr = LockPubScreen(screen)) {
                    dummywindow.Screen = scr;
                    win = OpenWindow(&dummywindow);
                    /*
                     *  as the window is open, we can unlock
                     *  if the window didn't open, the requester will
                     *  open on the default public screen
                     */
                    UnlockPubScreen(NULL, scr);
                    }

                else {
                    /*
                     * public screen not found
                     */
                    PutStr("Can't find named public screen!\n");
                    rc = 20l;
                    }

                }

            if (!rc) {
                /*
                 *  win may be NULL
                 */
                rc = EasyRequestArgs(win, &textreq, NULL, NULL);
                /*
                 *  don't forget closing the window
                 */
                if (win) CloseWindow(win);
                }

            }

        else {
            /*
             *  print out some more help
             */
            PutStr(USAGE "\n");
            rc = 40l;

            }

        /*
         *  free arguments and close the lib
         */
        FreeArgs(args);
        CloseLibrary(IntuitionBase);

        }

    else {
        /*
         *  we really need intuition lib
         *
         *  don't use PutStr() here, because dos.library might be < v36
         */
        Write(Output(), "Can't open intuition.library V37+!\n", 35l);
        rc = 30l;
        }

    exit(rc);
}
