//    ShellDepth         $VER: v1.0 1991-07-28       E. Lundevall
//
//    Move the current shell window to the front or to the back

#include <exec/types.h>
#include <exec/memory.h>
#include <dos/dosextens.h>
#include <dos/rdargs.h>
#include <clib/exec_protos.h>
#include <clib/dos_protos.h>

#define TEMPLATE (UBYTE *)"FRONT/S,BACK/S,VERBOSE/S"

struct IntuitionBase *IntuitionBase;

UBYTE vertxt[] = "\0$VER: v1.0_1991-07-28";


void NeedKS20()
{
 UBYTE only20txt[] = "Sorry, I need Kickstart 2.0x. ";

 only20txt[strlen(only20txt)-1] = 10; // LF at EOLN.
 if(DOSBase->lib_Version < 36) {
	Write(Output(),only20txt,strlen(only20txt));
	exit(20);
 }
}


int main(void)
{
 struct InfoData  *id;
 struct RDArgs    *rda;
 LONG             args[3] = { 0,0,0 };

 NeedKS20();    // Abort if not Kickstart 2.0x

 if(!GetConsoleTask())
	exit(10);       // Exit if no Shell.

 if(!(rda = ReadArgs(TEMPLATE,args,NULL))) {
	PrintFault(IoErr(),NULL);
	exit(10);
 }
 if(!(id = AllocVec(sizeof(struct InfoData),MEMF_CLEAR|MEMF_PUBLIC))) {
	VPrintf((UBYTE *)"Not enough memory!\n",NULL);
        FreeArgs(rda);
	exit(10);
 }
 if(!(IntuitionBase = (struct IntuitionBase *)OpenLibrary((UBYTE *)"intuition.library",36))) {
	VPrintf((UBYTE *)"Couldn't open intuition.library!\n",NULL);
        FreeArgs(rda);
	exit(10);
 }

 if(DoPkt(GetConsoleTask(),ACTION_DISK_INFO,MKBADDR(id),0,0,0,0)) {
	if(args[2]) {
		UBYTE* text = args[0] ? (UBYTE*)"front" :
					  args[1] ? (UBYTE*)"back"  : (UBYTE*)"same";
		VPrintf((UBYTE*)"Shell -> %s\n",(LONG*)&text);
    }
	if(args[0]) // Got front switch ?
		WindowToFront(id->id_VolumeNode);
	else if(args[1])  // Got back switch ?
		WindowToBack(id->id_VolumeNode);
 }
 else PrintFault(IoErr(),NULL);
 CloseLibrary((struct Library*)IntuitionBase);
 FreeArgs(rda);
 FreeVec(id);
 exit(0);
}
