/** WBRexx.c
*
*                     Copyright 1988, W.G.J. Langeveld
*                           All Rights Reserved
*                           Freely Distributable
*
*   This program can be used to execute REXX sccripts or REXX command strings
*   from the workbench. This can be accomplished either by double clicking on
*   a WBRexx project icon, where the default tool points to this program, or
*   by extended-selecting a number of such icons and double-clicking on the 
*   WBRexx tool icon.
*
**/
#include "exec/exec.h"
#include "libraries/dos.h"
#include "libraries/dosextens.h"
#include "libraries/arpbase.h"
#include "intuition/intuition.h"
#include "workbench/startup.h"
#include "workbench/workbench.h"
#include "functions.h"
#include "stdio.h"
#include "arpfunctions.h"

static char *ExpandArg(), *rindex();

/**
*
*   Main function. This one handles the Workbench message and all that stuff.
*
**/
void WBRexx(WB_msg)
struct WBStartup *WB_msg;
{
   struct DiskObject *ARIcon = NULL;
   int i, isrxstr = 0, debug = 0;
   char buffer[80], *result1, *result2, *SendREXXMsg();
/*
*  Handle the WorkBench message.
*/
   if (WB_msg) {
      for (i = 0; i < (int) WB_msg->sm_NumArgs; i++) {
         if (debug) {
            strcpy(buffer, "Argument is ");
            strncat(buffer, WB_msg->sm_ArgList[i].wa_Name, 58);
            AReqBool(NULL, buffer, NULL, "Okay");
         }
         result1 = ExpandArg(&WB_msg->sm_ArgList[i], NULL);
/*
*   If this is an inline rexx function (tooltype RX), it will have no file
*   associated with it. Therefore, get the name of the info file.
*/
         isrxstr = (strlen(result1) > 0) ? 0 : 1;
         if (isrxstr) result1 = ExpandArg(&WB_msg->sm_ArgList[i], ".info");

         if (debug) {
            strcpy(buffer, "Expands to ");
            strncat(buffer, result1, 59);
            AReqBool(NULL, buffer, NULL, "Okay");
         }
/*
*   If the name string is still zero, ignore this one.
*/
         if (strlen(result1) > 0) {
/*
*   Else, open the icon.
*/
            ARIcon = GetDiskObject(result1);
            if (ARIcon != NULL) {
               if (ARIcon->do_Type == WBPROJECT) {
/*
*   Only do project Icons, of course. If this is an inline string, get the
*   string from the ToolType RX.
*/
                  if (isrxstr) result1 = FindToolType(ARIcon->do_ToolTypes, "RX");
                  if (result1) {
/*
*   Send it to Rexx. If there's an error, post an autorequest
*/
                     if (debug) {
                        strcpy(buffer, "REXX gets ");
                        strncat(buffer, result1, 60);
                        AReqBool(NULL, buffer, NULL, "Okay");
                     }

                     result2 = SendREXXMsg(result1, isrxstr);
                     if (result2) AReqBool(NULL, result2, NULL, "Okay");
                  }
               }
/*
*   If this is the tool icon, see if DEBUG is set.
*/
               else if (ARIcon->do_Type == WBTOOL) {
                  result1 = FindToolType(ARIcon->do_ToolTypes, "DEBUG");
                  if (result1) {
                     if (MatchToolValue(result1, "ON")) debug = TRUE;
                  }
               }
/*
*   Free the icon.
*/
               FreeDiskObject(ARIcon);
            }
         }
      }
   }  

   return;
}

/**
*
*   This function expands a WBArg filename to include the full pathname and
*   if given, the extension "extens".
*
**/
char *ExpandArg(arg, extens)
struct WBArg *arg;
char *extens;
{
   struct FileLock *olddir, *lock;
   static char iconname[255];
   static char buff[80], *rind;
   long len;

   iconname[0] = '\0';
/*
*   See if this type can be locked. If not, it must be a device, return.
*/
   if (arg->wa_Lock == NULL) return(iconname);
/*
*   Change directory to the tools directory.
*/
   olddir = CurrentDir(arg->wa_Lock);
/*
*   Get the current path
*/
   strcpy(buff, arg->wa_Name);
/*
*   If present, append the file extension.
*/
   if (extens) strcat(buff, extens);

   lock = Lock(buff, ACCESS_READ);
   if (lock) {
      len = PathName(lock, iconname, 1);
      UnLock(lock);
      if (extens) {
         rind = rindex(iconname, '.');
         if (rind) *rind = '\0';
      }
   }
/*
*   Set directory back
*/
   CurrentDir(olddir);

   return(iconname);
}
