/******************  wbmain.c  *******************
 *                                               *
 *                                               *
 *                  IconAuthor                   *
 *                                               *
 *                  © by Koessi                  *
 *                                               *
 *             Thursday, 13 Aug 1992             *
 *                                               *
 *                                               *
 *************************************************/

#include "defs.h"

#define  FILENAME_MAX 128
#define  FIB          struct FileInfoBlock

__autoinit
int   checkexec(void);
void  wbmain(struct WBStartup *);

extern struct Library *SysBase;

/*************************************************
 *                                               *
 *                                               *
 *    FUNCTION: checkexec                        *
 *                                               *
 *                                               *
 *    INPUT:    void                             *
 *                                               *
 *    OUTPUT:   __autoinit int                   *
 *                                               *
 *    NOTE:     before _main                     *
 *                                               *
 *************************************************/

const char osstr[] = "RAW:0/11/320/11/ I need OS 2.0 \8-)";

__autoinit int
checkexec(void)
{
  if (SysBase->lib_Version < 36)
  {
    BPTR out;
    if (out = Open(osstr, MODE_NEWFILE))
    {
      Delay(150);
      Close(out);
    }
    return(RETURN_FAIL);
  }
}

/*************************************************
 *                                               *
 *                                               *
 *    FUNCTION: wbmain                           *
 *                                               *
 *                                               *
 *    INPUT:    struct WBStartup *_WBMsg         *
 *                                               *
 *    OUTPUT:   void                             *
 *                                               *
 *    NOTE:     before main                      *
 *                                               *
 *************************************************/


void
wbmain(struct WBStartup *_WBMsg)
{
  volatile char  **av;
  volatile ULONG   ac;

  FIB *infoblock;
  if (infoblock = (FIB *)AllocMem(sizeof(FIB), MEMF_PUBLIC|MEMF_CLEAR))
  {
    long na = _WBMsg->sm_NumArgs;
    if (av = (char **)malloc((na + 1) << 2))
    {
      struct WBArg *wa = _WBMsg->sm_ArgList;
      for  (ac = 0; ac < na; ++ac, ++wa)
      {
        char *path;
        if (path = (char *)malloc(FILENAME_MAX))
        {
          av[ac] = path;

          NameFromLock( wa->wa_Lock, path, FILENAME_MAX);
          AddPart(path, wa->wa_Name, FILENAME_MAX);

          while (*path)
            path++;

          /*
          if (*--path != ':')
            *++path = '/';

          ++path;

          char *s = wa->wa_Name;

          while (*s)
            *path++ = *s++;
          */

          switch (*--path)
          {
          case '/':
            *path = '\0';
            break;

          case ':':
            *++path = 'D';
            *++path = 'i';
            *++path = 's';
            *++path = 'k';

          default:
            *++path = '\0';
            break;
          }
        }
      }
      av[ac] = NULL;
    }
    FreeMem(infoblock, sizeof(FIB));
    main(ac, av);
  }
}
