/*
 * startup30shell.c - simple startup code for Shell programs
 * part of WBLaunch - emulates WB startup of programs from the Shell
 * Copyright (C) 1998, 1999 Stephen Williams <sw@fysh.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */


#include <exec/types.h>
#include <exec/alerts.h>
#include <exec/execbase.h>
#include <dos/dosextens.h>

#include <clib/exec_protos.h>
#ifdef VBCC
#include <inline/exec_protos.h>
#endif


/* Library bases */
struct ExecBase *	SysBase;
struct Library *	DOSBase;


/*
 * geta4() function for VBCC; define PROVIDE_GETA4 if you want other
 * object files to be able to use it; else it'll be inlined
 */

#ifdef VBCC
#define GETA4
#ifdef PROVIDE_GETA4
void geta4(void);
#else
void geta4(void) = "\tpublic\t_DATA_BAS_\n\tlea.l\t_DATA_BAS_+32766,a4";
#endif /* PROVIDE_GETA4 */
#endif /* VBCC */
#ifdef _DCC
#define GETA4 __geta4
#endif


/* Proto for main() function */
int shellmain(void);


/*
 * Startup function, minimal
 * Just exits on startup from Workbench;
 * opens DOS v39+ and calls shellmain(void) on startup from Shell
 *
 * Last altered February 14, 1999
 */

#define ABSEXECBASE 4L
static int GETA4 startup(void)
{
  register struct Library *	privDOSBase;
  register int			rc = 20;
  register struct Process *	thisProc;

  /*
   * Set up SysBase, and get a pointer to ourself
   */
#ifdef VBCC
  geta4();
#endif
  SysBase = *((struct ExecBase **)ABSEXECBASE);
  thisProc = (struct Process *)SysBase->ThisTask;

  /*
   * Bail out if started from Workbench
   */
  if(!thisProc->pr_CLI) {
    register struct MsgPort * msgPort = &thisProc->pr_MsgPort;
    (void)WaitPort(msgPort);
    Forbid();
    ReplyMsg(GetMsg(msgPort));
  } else {

    /*
     * Open DOS
     */
    if(privDOSBase = OpenLibrary("dos.library", 39)) {
      /*
       * Got it; initialize base ptr and start
       */
      DOSBase = privDOSBase;
      rc = shellmain();
      CloseLibrary(privDOSBase);
    } else Alert(AG_OpenLib | AO_DOSLib);

  }

  return rc;
}


/* exported geta4() function for VBCC */
#ifdef VBCC
#ifdef PROVIDE_GETA4
void __geta4(void) = "\tpublic\t_DATA_BAS_\n\tlea.l\t_DATA_BAS_+32766,a4";
void geta4(void)
{
  __geta4();
}
#endif
#endif
