#include "storm.h"
#include <stdio.h>
#include <string.h>
#include <proto/dos.h>
#include <proto/exec.h>
#include <proto/rexxsyslib.h>


struct RxsLib *RexxSysBase = NULL;
struct DosLibrary *DOSBase = NULL;

static struct MsgPort *replyPort;
static char *sendPortName;
static char *stormInfo;


int initStorm(char *portname)
{
    int ret = 1;

    RexxSysBase = (struct RxsLib*) OpenLibrary("rexxsyslib.library", 36);
    if (!RexxSysBase)
    {
        fprintf(stderr, "Failed to open rexxsyslib.library V36 or up.\n");
        ret = 0;
    }

    DOSBase = (struct DosLibrary*) OpenLibrary("dos.library", 36);
    if (!DOSBase)
    {
        fprintf(stderr, "Failed to open dos.library V36 or up.\n");
        ret = 0;
    }

    replyPort = CreateMsgPort();
    if (!replyPort)
    {
        fprintf(stderr, "Failed to create reply-port.\n");
        ret = 0;
    }
    sendPortName = portname;

    return ret;
}


void exitStorm(void)
{
    if (replyPort)
    {
        DeleteMsgPort(replyPort);
        replyPort = NULL;
    }
    if (RexxSysBase)
    {
        CloseLibrary((struct Library*) RexxSysBase);
        RexxSysBase = NULL;
    }
    if (DOSBase)
    {
        CloseLibrary((struct Library*) DOSBase);
        DOSBase = NULL;
    }
}


void setStormInfo(char *s)
{
    stormInfo = s;
}


const char *getStormInfo(void)
{
    return stormInfo;
}


void sendStorm(const char *s, ...)
{
    va_list vargs;

    va_start(vargs, s);
    vsendStorm(s, vargs);
    va_end(vargs);
}


void vsendStorm(const char *s, va_list vargs)
{
    if (RexxSysBase && replyPort && sendPortName)
    {
        static char stringbuffer[4096];
        struct RexxMsg *msg;

        vsprintf(stringbuffer, s, vargs);
        printf("cvs: now sending '%s'.\n", stringbuffer);

        msg = CreateRexxMsg(replyPort, NULL, NULL);
        if (msg)
        {
            if (msg->rm_Args[0] = CreateArgstring(stringbuffer, strlen(stringbuffer)))
            {
                struct MsgPort *sendPort;

                Forbid();
                if (sendPort = FindPort(sendPortName))
                {
                    PutMsg(sendPort, (struct Message*)msg);
                    Permit();

                    WaitPort(replyPort);
                    while(GetMsg(replyPort));
                }
                else
                {
                    Permit();
                }
                DeleteArgstring(msg->rm_Args[0]);
            }
            DeleteRexxMsg(msg);
        }
    }
}


char *fullPath(const char *s)
{
    static char fullpath [512];

    BPTR lock = Lock((STRPTR)s, ACCESS_READ);
    if(lock  &&  NameFromLock(lock, fullpath, 512))
    {
        s = (const char*) fullpath;
    }
    UnLock(lock);

    return (char*) s;
}


const char *skipFirstPathPart(const char *s)
{
    char c;
    int i;

    for(i=0; c=s[i]; i++)
    {
        if(c == '/'  ||  c == ':')
        {
            return (s + i + 1);
        }
    }
    return s;
}


int fileExists(const char *s)
{
	int ret = 0;
	BPTR lock = Lock((STRPTR)s, ACCESS_READ);
	if(lock)
	{
		ret = 1;
		UnLock(lock);
	}
	return ret;
}

