/*
 *  This file is part of ixemul.library for the Amiga.
 *  Copyright (C) 1991, 1992  Markus M. Wild
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Library General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Library General Public License for more details.
 *
 *  You should have received a copy of the GNU Library General Public
 *  License along with this library; if not, write to the Free
 *  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#define KERNEL
#include "ixemul.h"

#include <workbench/startup.h>

/* smells abit kludgy I know.. but can live with quite few variables in
 * the user area 
 */

int
_main (union { char *_aline; struct WBStartup *_wb_msg; } a1,
       union { int   _alen;  char *_def_window;	        } a2,
       int (* main()))
#define aline		a1._aline
#define alen		a2._alen
#define wb_msg		a1._wb_msg
#define def_window	a2._def_window
{
  struct ExecBase	*SysBase	= *(void **)4;
  struct Process 	*me		= (struct Process *)SysBase->ThisTask;
  int			exit_val;
  char			**argv;
  int			argc;

  if (! me->pr_CLI)
    {
      /* Workbench programs expect to have their CD where the executed
       * program lives. */
      if (wb_msg->sm_ArgList)
        {
	  CurrentDir (wb_msg->sm_ArgList->wa_Lock);
	  syscall (SYS__wb_parse, me, wb_msg, def_window);
	}
      /* argc==0: this means, that argv is really a WBenchMsg, not a vector */
      argc = 0;
      argv = (char **) wb_msg;
    }
  else
    {
      /* if we were started from the CLI, alen & aline are valid and
       * should now be split into arguments. This is done by the
       * function "_cli_parse()", which does wildcard expansion if not
       * disabled (see cli_parse.c). */
      _cli_parse (me, alen, aline, &argc, &argv);
    }

  syscall (SYS_exit, main (argc, argv, 0));
}
