/**
 * Scout - The Amiga System Monitor
 *
 *------------------------------------------------------------------
 *
 * 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
 * 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.
 *
 * You must not use this source code to gain profit of any kind!
 *
 *------------------------------------------------------------------
 *
 * @author Andreas Gelhausen
 * @author Richard Körber <rkoerber@gmx.de>
 */

#include "system_headers.h"

#define EXECBASE        (*(struct ExecBase **)4)
#define aprintf         Printf

extern struct ExecBase    *SysBase;
struct DosLibrary *DOSBase;
struct IntuitionBase *IntuitionBase;
struct Library *UtilityBase;
struct Library *IconBase;

struct Args opts;

ULONG __saveds startup(void)
{
    ULONG rc = RETURN_OK;
    struct Process *me;
    struct WBStartup *wbmsg = NULL;

    SysBase = EXECBASE;

    me = (struct Process *)FindTask(NULL);

    if (!me->pr_CLI) {
        WaitPort(&me->pr_MsgPort);
        wbmsg = (struct WBStartup *)GetMsg(&me->pr_MsgPort);
    }

    if (DOSBase = (struct DosLibrary *)OpenLibrary(DOSNAME, 37)) {
        if (IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library", 37)) {
            if (UtilityBase = OpenLibrary(UTILITYNAME, 37)) {
                struct SmartArgs sa;

                IconBase = OpenLibrary(ICONNAME, 37);

                memset(&sa, 0x00, sizeof(struct SmartArgs));
                sa.sa_Template = TEMPLATE;
                sa.sa_Parameter = (LONG *)&opts;
                sa.sa_FileParameter = -1;
                sa.sa_Window = "CON:20/20/400/100/Scout/AUTO/CLOSE/WAIT";

                if (SmartReadArgs(wbmsg, &sa) == 0) {
                    if (opts.ToolPri != NULL && (*opts.ToolPri < -128 || *opts.ToolPri > 127)) {
                        FPrintf(Output(), "ToolPri is not in range (-128..127).\n");
                    } else if (opts.CpuDisplay != NULL && (*opts.CpuDisplay < 0 || *opts.CpuDisplay > 2)) {
                        FPrintf(Output(), "CpuDisplay is not in range (0..2).\n");
                    } else {
                        LONG oldtaskpri = 0;

                        if (opts.ToolPri) oldtaskpri = SetTaskPri((struct Task *)me, *opts.ToolPri);

                        // Call the main program
                        rc = scout_main();

                        // Reset program priority if necessary
                        if (opts.ToolPri) SetTaskPri((struct Task *)me, (LONG)oldtaskpri);
                    }
                }
                SmartFreeArgs(&sa);

                if (IconBase) CloseLibrary(IconBase);
                CloseLibrary(UtilityBase);
            }

            CloseLibrary((struct Library *)IntuitionBase);
        }

        CloseLibrary((struct Library *)DOSBase);
    }

    if (wbmsg) ReplyMsg((struct Message *)wbmsg);

    return rc;
}

void __stdargs _XCEXIT (long mist)
{
}
