/*
**	reboot.c - reboot Amiga
**	$VER: reboot.c 42.1 (10.2.96)
**	Copyright © 1996 Michal Letowski
**
**	42.1 (10.2.96) - initial version
*/

#define __USE_SYSBASE

#include <exec/types.h>
#include <exec/execbase.h>
#include <exec/libraries.h>
#include <dos/dos.h>
#include <dos/rdargs.h>
#include <support/types.h>
#include <support/dos.h>

#include <proto/exec.h>
#include <proto/dos.h>

#include "reboot.rev.h"


/*
**	Constants
*/
#define DOS_NAME			"dos.library"
#define DOS_VERN			37L

#define TEMPLATE			"NORES=CLEAR/S"


/*
**	Private structures
*/
struct Options
{
	LBOOL opt_Clear;															/* NORES=CLEAR/S */
};	/* Options */


/*
**	Global data
*/
STATIC CONST TEXT VersionString[]=
	VERSION(PROG_NAME,PROG_VERSION,PROG_REVISION,PROG_DATE);


/*
**	Public functions
*/
/****** C/Reboot ************************************************************
*
*   NAME
*       Reboot -- reboot your computer. (V42)
*
*   SYNOPSIS
*       Reboot [CLEAR]
*
*   TEMPLATE
*       Reboot "NORES=CLEAR/S"
*
*   FUNCTION
*       Reboots your Amiga by calling exec.library/ColdReboot(). If CLEAR
*       parameter is given, will clear system vectors befor rebooting. This
*       is useful if you want to get rid of some reset-resident programs,
*       like RAD: drive.
*
*   INPUTS
*       CLEAR - switch to clear system vectors before rebooting.
*
*   EXAMPLE
*       Reboot
*           ; Reboots your Amiga, doesn't touch resident programs.
*
*       Reboot CLEAR
*           ; Removes resident programs then reboots your Amiga.
*
*   NOTES
*       Reboot is pure and can be made resident.
*
*****************************************************************************
*
*/
LONG Reboot(VOID)
{
	struct ExecBase *SysBase=INITSYSBASE;
	struct Library *DOSBase;

	struct Options Opts;
	struct RDArgs *Args;
	LONG RC=RETURN_FAIL;
	
	clear(&Opts);																	/* Set defaults */

	/* Open DOS, process arguments */
	if(DOSBase=OpenLibrary(DOS_NAME,DOS_VERN))
	{
		if(Args=ReadArgs(TEMPLATE,(LONG *)&Opts,NULL))
		{
			RC=RETURN_OK;															/* Everything OK! */
			FreeArgs(Args);
		}
		else
			PrintFault(IoErr(),PROG_NAME);
		CloseLibrary(DOSBase);
	}
	else
		SetResult2(ERROR_INVALID_RESIDENT_LIBRARY);

	/* Do the thing */
	Disable();																		/* Nothing messing around... */
	if(Opts.opt_Clear)														/* Clearing requested... */
	{																							/* Perform it! */
		SysBase->ColdCapture=NULL;
		SysBase->CoolCapture=NULL;
		SysBase->WarmCapture=NULL;
		SysBase->KickMemPtr=NULL;										/* No allocations */
		SysBase->KickTagPtr=NULL;										/* Only ROM resident modules */
	}
	ColdReboot();																	/* Reboot!!! */
	Enable();																			/* In case ColdReboot() returns ;-) */

	return(RC);
}	/* Reboot */
