/******************************************************************************
*									      *
* BReq: Opens a centered ReqTools requestor on a custom screen prompting user *
*	for OS selection.						      *
*									      *
* Copyright (c)1994 Eric R. Augustine (voodoo@well.sf.ca.us)		      *
*									      *
* This is Public Domain Software in the truest sense of the phrase.  Do with  *
* it what you wish. The author accepts no responsibility for any damage as a  *
* result of the use of this program nor does he make any claims or garantees  *
* as to what this program can do.					      *
*									      *
******************************************************************************/
#include <stdlib.h>
#include <string.h>
#include <exec/types.h>
#include <intuition/intuition.h>
#include <libraries/reqtools.h>
#include <proto/exec.h>
#include <proto/dos.h>
#include <proto/intuition.h>
#include <proto/reqtools.h>

#define TRUE  1
#define FALSE 0

extern struct  IntuitionBase *IntuitionBase;
struct ReqToolsBase  *ReqToolsBase;
struct Library	     *DosBase;

typedef int bool;

struct internal_args {
   char	 *timeout;			     /* Delay(ia.timeout) doesn't work with int */
   const char *defresponse;		     /* select default for timeout */
   char	 *sysstring;
};

void error(char *errstr) {		     /* avoid large stdio code by using ROM routines */
   Write(Output(),errstr,strlen(errstr));
}

main (int argc, char *argv[]) {
   int	  DefResponse;
   struct Screen *BScreen;
   ULONG  ret;
   struct internal_args ia = { NULL,
			       "A",
			       "sys:loadbsd sys:vmunix -a" };

   if(!(DosBase = (struct Library *)OpenLibrary("dos.library",0))) {
      error(argv[0]);
      error(": unable to open dos.library\n");
      exit(0);
   }

   (struct RDArgs *)ReadArgs("TIMEOUT/K,DEFAULT/K,SYSSTRING/K", (long *)&ia, NULL);

   if(!(ReqToolsBase  = (struct ReqToolsBase *)OpenLibrary(REQTOOLSNAME, REQTOOLSVERSION))) {
      error(argv[0]);
      error(": unable to open reqtools.library\n");
      exit(0);
   }
   if(!(IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library",0))) {
      error(argv[0]);
      error(": unable to open intuition.library\n");
      exit(0);
   }

   if(strcmp(ia.defresponse,"A") == 0)
      DefResponse = 4;
   else if(strcmp(ia.defresponse,"B") == 0)
      DefResponse = 3;
   else {
      error(argv[0]);
      error(": invalid selection for DEFAULT '");
      error((char *)ia.defresponse);
      error("', select \"A\" or \"B\".\n");
      exit(0);
   }

   if(!(BScreen = OpenScreenTags(0L,
      SA_Depth,4,
      SA_DisplayID,HIRESLACE_KEY|DEFAULT_MONITOR_ID,
      SA_Type,CUSTOMSCREEN,
      SA_ShowTitle,FALSE,
      SA_Draggable,FALSE,
      SA_Quiet,TRUE,
      TAG_DONE))) {
	 error(argv[0]);
	 error(": unable to open custom screen\n");
	 exit(0);
   }

   ret = (bool)rtEZRequestTags("Boot Request\n" "OS Selection\n",
      "NetBSD|ADOS", NULL, NULL, 
      RT_ReqPos, REQPOS_CENTERSCR,
      RT_Screen, (struct Screen *)BScreen,
      RTEZ_ReqTitle,(char *)"BReq",
      RTEZ_Flags,EZREQF_CENTERTEXT,
      RTEZ_DefaultResponse, DefResponse,     /* this req uses specific keys for buttons but, it's a good idea */
      TAG_END);

   if(ret == FALSE)  {
      CloseScreen(BScreen);
      CloseLibrary((struct Library *)IntuitionBase);
      CloseLibrary((struct Library *)DosBase);
      CloseLibrary((struct Library *)ReqToolsBase);
   }
   else
      if(ret == TRUE) {
	 CloseLibrary((struct Library *)DosBase);
	 CloseLibrary((struct Library *)ReqToolsBase);
	 system(ia.sysstring);
	 CloseScreen(BScreen);
	 CloseLibrary((struct Library *)IntuitionBase);
      }
   return;
}

