/* -----------------------------------------------------------------------------

  Use remote.library to show a HTML page. Compile with DICE:

  dcc main.c -// -mRR -r -proto -3.0 -l reqtoolssr.lib -l //dlib/remotesr.lib -o ///c/showurl

  ------------------------------------------------------------------------------
*/

/// "includes"

#define Prototype extern

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <exec/exec.h>
#include <exec/types.h>
#include <workbench/workbench.h>
#include <workbench/startup.h>
#include <dos/dos.h>
#include <clib/alib_protos.h>
#include <clib/dos_protos.h>
#include <clib/exec_protos.h>
#include <clib/icon_protos.h>
#include <clib/intuition_protos.h>
#include <reqtools/include/reqtools.h>
#include <reqtools/clib/reqtools_protos.h>

// remote.library includes

#include "//include/remote.h"

#include "//clib/remote_protos.h"

///
/// "prototypes"

Prototype int   main  (int, char **);
Prototype int   wbmain(struct WBStartup *);
Prototype UWORD Ask   (char *, char *, char *);

///
/// "globals"

UBYTE Version[] = "$VER: showurl 38.0 (" __COMMODORE_DATE__ ")";

struct Library *RemoteBase   = NULL;
struct Library *ReqToolsBase = NULL;

///
/// "main"

/* ----------------------------------- main ------------------------------------

 DOS entry point

*/

int
main(argc, argv)

int    argc;
char **argv;
{
    int error = 0;

    puts("showurl ©2001 Dietmar Eilert");

    if (RemoteBase = OpenLibrary("remote.library", 37))
    {
        // manually initialize remote.library (we want to see the error code)

        if (error = RemoteInit())
        {
            puts(RemoteError(error));
        }
        else {

            ULONG argArray[] = { 0 };

            struct RDArgs *rdArgs;

            // parse arguments

            if (rdArgs = ReadArgs("URL/A", argArray, NULL))
            {
                // open file in browser

                if (error = RemoteOpen("BROWSER", NULL, (UBYTE *)argArray[0], REMOTE_OPEN_ASYNC))
                {
                    puts(RemoteError(error));
                }

                FreeArgs(rdArgs);
            }
            else
            {
                UBYTE buffer[256];

                if (Fault(IoErr(), "showurl", buffer, sizeof(buffer)))

                    puts(buffer);

                error = 20;
            }
        }

        CloseLibrary(RemoteBase);
    }
    else {

        error = 20;

        puts("showurl: remote.library not available");
    }

    if (ReqToolsBase)

        CloseLibrary(ReqToolsBase);

    return(error);
}   

/* ---------------------------------- wbmain -----------------------------------

 WB entry point

*/

int
wbmain(struct WBStartup *wbs)
{
    int error = 0;

    if (RemoteBase = OpenLibrary("remote.library", 37))
    {
        // manually initialize remote.library (we want to see the error code)

        if (error = RemoteInit())
        {
            Ask("showurl", RemoteError(error), "OK");
        }
        else {

            struct DiskObject *diskObject;

            if (wbs->sm_NumArgs > 1)
            {
                if (diskObject = GetDiskObject(wbs->sm_ArgList[1].wa_Name))
                {
                    UBYTE localfile[256], *url;

                    url = FindToolType(diskObject->do_ToolTypes, "URL");

                    if (url == NULL)
                    {
                        if (NameFromLock(GetProgramDir(), localfile, 256))
                        {
                            AddPart(localfile, wbs->sm_ArgList[1].wa_Name, 256);

                            strins(localfile, "file:///");

                            url = localfile;
                        }
                    }

                    if (url)
                    {
                        if (error = RemoteOpen("BROWSER", NULL, url, REMOTE_OPEN_ASYNC))

                            Ask("showurl", RemoteError(error), "OK");
                    }

                    FreeDiskObject(diskObject);
                }
                else
                    Ask("showurl", "Can not read icon", "OK");
            }
            else
                Ask("showurl", "No project icon selected", "OK");
        }

        CloseLibrary(RemoteBase);
    }
    else {

        error = 20;

        Ask("showurl", "remote.library not available", "OK");
    }

    if (ReqToolsBase)

        CloseLibrary(ReqToolsBase);

    return(error);
}   

///
/// "misc"

/* -------------------------------------- Ask ----------------------------------

 Show requester and ask for a selection.

*/

UWORD
Ask(title, body, button)

char *title;
char *body;
char *button;
{
    UWORD result = 0;

    if (ReqToolsBase == NULL)

        ReqToolsBase = OpenLibrary("reqtools.library", 38);

    if (ReqToolsBase)
    {
         result = rtEZRequestTags(body, button, NULL, NULL, RT_Underscore, '_', RTEZ_ReqTitle,  title, TAG_DONE);
    }
    else {

        struct EasyStruct easyStruct = { sizeof(struct EasyStruct), 0, title, body, button };

        result = EasyRequestArgs(NULL, &easyStruct, NULL, NULL);
    }

    return(result);
}

///
