/*
 * A template for use in building new saver utilities.
 *
 * Copyright 1991, Mike Meyer
 * All Rights Reserved
 *
 * See the file "ShadowMaster:Distribution" for information on distribution.
 *
 * ===build instructions
 * % lc guard ; output= guard.o input= guard.c utilitymain.h
 * % blink guard.o LIB lib:lcr.lib SC SD ; output= guard input=guard.o
 * % copy guard //savers
 * ===endbuild
 *
 * Note: the lib:lcr.lib may not be needed. Try linking without it and see...
 *
 * Basically, the only thing to do is change doutility to run your code, and
 * have it return the name of a blanker to run, or a NULL pointer if it shouldn't
 * run any blanker at all.
 *
 * If you compile this with -dLOOPS, then your code is called multiple times,
 * with a TRUE argument the first time and FALSE thereafter. If your argument
 * is FALSE, your utility routine is expected to wait for the CTRL C, and then
 * return either another name, or a NULL to indicate that we want to stop now.
 * This code will kill the running saver module, and start the one you asked
 * it to start.
 */

#include <exec/types.h>
#include <dos/dos.h>
#include <dos/dostags.h>
#include <dos/rdargs.h>
#include <proto/dos.h>
#include <proto/exec.h>

char *doutility(int) ;
static void __saveds procend(void) ;

struct ExecBase		*SysBase = NULL ;
struct DosLibrary	*DOSBase = NULL ;

/* Don't change anything above this point... */

/*
 * We use ReadArgs for parsing the arguments. Just define template correctly,
 * and set up the opts array with appropriate names for your code. If you don't
 * want the arguments looked at, comment out the define of template. Change opts
 * to set any defaults you feel need setting.
 */
#define TEMPLATE	"FROM"
static long opts[1] = { 0 };

/*
 * Private variables used by the utility.
 */
#define MAXSTRING 200
static char command[MAXSTRING], *args = NULL ;
static int size, corners ;
#define TOP_LEFT	1
#define TOP_RIGHT	2
#define BOTTOM_LEFT	4
#define BOTTOM_RIGHT	8

/*
 * The proctags table is here for you to change - to a degree. We expect the
 * Seglist to be first, and CommandName to be second. Your code can rely on
 * (if you want to) Arguments to be third.
 */
struct TagItem proctags[] = {
	{NP_Seglist, 0},
	{NP_CommandName, 0},
	{NP_Arguments, &" "},	/* The space guards against a bug in 2.0 */
	{NP_FreeSeglist, 1},
	{NP_StackSize, 8000},
	{NP_Cli, 1},
	{NP_CopyVars, 0},
	{NP_ExitCode, &procend},
	{TAG_DONE, 0}
	} ;

#include "utilitymain.h"
/*
 * You should replace from here down with your own doutility function.
 * Just return the name of the blanker you want run, and we'll do the
 * rest. Note that it's valid to return a pointer to a static data area.
 */
#include <intuition/intuitionbase.h>

struct IntuitionBase	*IntuitionBase = NULL ;

int LoadFile(char *) ;

char *
doutility(int x) {
	int skip_corner ;

	if ((IntuitionBase = (struct IntuitionBase *)
	    OpenLibrary("intuition.library", 37)) == NULL)
		return NULL ;

	if (!LoadFile(*opts ? (char *) *opts : "env:shadowmaster/guard.prefs"))
		return NULL ;
	if (IntuitionBase->FirstScreen->MouseY <= size)
		skip_corner = TOP_LEFT | TOP_RIGHT ;
	else if (IntuitionBase->FirstScreen->Height - IntuitionBase->FirstScreen->MouseY <= size)
		skip_corner = BOTTOM_LEFT | BOTTOM_RIGHT ;
	else skip_corner = 0 ;

	if (IntuitionBase->FirstScreen->MouseX <= size)
		skip_corner &= TOP_LEFT | BOTTOM_LEFT ;
	else if (IntuitionBase->FirstScreen->Width - IntuitionBase->FirstScreen->MouseX <= size)
		skip_corner &= TOP_RIGHT | BOTTOM_RIGHT ;
	else skip_corner = 0 ;

	CloseLibrary((struct Library *) IntuitionBase) ;

	if (args)
		proctags[2].ti_Data = (long) args ;
	if ((skip_corner & corners) == 0) return (char *) command ;
	Wait(SIGBREAKF_CTRL_C) ;
	return NULL ;
	}

#include <string.h>
#include <stdlib.h>

int
LoadFile(char *file) {
	BPTR fh ;
	char *cp ;

	if (!(fh = Open(file, MODE_OLDFILE))) return FALSE ;
	FGets(fh, command, MAXSTRING) ;
	corners = atoi(command) ;
	if (!corners) corners = TOP_LEFT ;
	FGets(fh, command, MAXSTRING) ;
	size = atoi(command) ;
	FGets(fh, command, MAXSTRING) ;
	if (cp = strchr(command, '\n')) *cp = '\0' ;
	if (cp = strchr(command, ' ')) {
		*cp = '\0' ;
		args = cp ;
		}
	Close(fh) ;

	return TRUE ;
	}
