/**************************************************************************
*        
*                           Program : FixSprite
*                           Author  : Adam Nelson
*                           Version : 1.03
*
* History
* -------
*
* Version 1.03 (27/04/94)
* -----------------------
* No code changes, just re-compiled with SAS/C version 6.50.
* 
* Version 1.02 (30/01/94)
* -----------------------
* Added a command line option to disable the AutoRequester.
* First Public release.
*
* Version 1.01 (29/01/94)
* -----------------------
* Changed Ctrl-F checking to Ctrl-C for compatibility.
* An AutoRequest will now be displayed when program is sent a Ctrl-C.
* Small optimisations.
*
* Version 1.00 (25/01/94)
* -----------------------
* Initial release.
*
**************************************************************************/

#include <intuition/intuition.h>
#include <graphics/sprite.h>
#include <libraries/dos.h>
#include <stdio.h>

void ReleaseResources();

struct IntuitionBase *IntuitionBase = NULL;
struct GfxBase *GfxBase = NULL;

struct SimpleSprite EmptySprite=
{
	NULL,           /* We don't want an image, and we have no data!       */
	0,              /* The height is 0 pixels tall.                       */
	0, 0,           /* The sprite's x/y coordinates on the screen.        */
	-1,             /* Don't worry about this...                          */
};

struct IntuiText BodyText=
{
	0,              /* FrontPen, colour 0.                                */
	0,              /* BackPen, not used.                                 */
	JAM1,           /* DrawMode, do not change the background.            */
	15,             /* LedtEdge, 15 pixels out.                           */
	5,              /* TopEdge, 5 lines down.                             */
	NULL,           /* ITextFont, default font.                           */
	"FixSprite V1.03 terminated. Sprite deallocated.",    /* The text.    */
	NULL,           /* No more IntuiText structures link.                 */
};

struct IntuiText OkayText=
{
	0,              /* FrontPen, colour 0.                                */
	0,              /* BackPen, not used.                                 */
	JAM1,           /* DrawMode, do not change the background.            */
	6,              /* LeftEdge, 6 pixels out.                            */
	3,              /* TopEdge, 3 lines down.                             */
	NULL,           /* ITextFont, default font.                           */
	"OK",           /* IText, the text that will be printed.              */
	NULL,           /* No more IntuiText structures link.                 */
};

main(argc,argv)
int argc;
char *argv[];
{
	char *VersionString = "$VER: FixSprite V1.03 (27/04/94)";
	char VersionNumber[] = "1.03";
	static char NoReqString[] = "NOREQUESTER";
	char *KeyWord = NoReqString;
	long SpriteNumber;
	BOOL NoRequester = FALSE;
	BOOL NotOkay = FALSE;

	if(argc < 2 || argc > 3)  /* If not the correct number of arguments... */
	{
		printf("FixSprite V%s ©1994 Adam Nelson.\n",VersionNumber);
		printf("USAGE: FixSprite n <NOREQUESTER> where n is the sprite number from 0 to 7.\n");
		printf("       Use the NOREQUESTER keyword if you do not want a requester\n       to be displayed on exit.\n");
		exit(0);
	}

	SpriteNumber = atol(argv[1]);  /* Get the sprite number from user */

	if(SpriteNumber < 0 || SpriteNumber > 7)  /* If out of range... */
	{
		printf("FATAL ERROR: Illegal sprite number.\n");
		exit(0);
	}

	if(argc == 3)  /* If the user specified three arguments... */
	{              /* Check to make sure the third is 'norequester' */
		do         
			if(tolower(*KeyWord) != tolower(*argv[2]))
				NotOkay = TRUE;
		while(argv[2]++,*KeyWord++);
	}
	else  /* If there was only one argument, then show a requester */
		NotOkay = TRUE;

	if(!NotOkay)   /* If it was 'norequester'... */
		NoRequester = TRUE;

	IntuitionBase = (struct IntuitionBase *)
	OpenLibrary("intuition.library", 0);
  
	if(IntuitionBase == NULL)
		ReleaseResources();       /* Could NOT open the Intuition Library! */

	GfxBase = (struct GfxBase *)
		OpenLibrary("graphics.library", 0);

	if(GfxBase == NULL)
		ReleaseResources();       /* Could NOT open the Graphics Library! */

	if(GetSprite(&EmptySprite, SpriteNumber) != SpriteNumber)  /* If we couldn't get a sprite... */
		{
		printf("FATAL ERROR: Could not use sprite number %d. Sprite is in use.\n",SpriteNumber);
		ReleaseResources(); /* Could not reserve sprite number SpriteNumber. */
		}

	printf("FixSprite V%s ©1994 Adam Nelson.\n",VersionNumber);
	printf("Sprite number %d allocated.\n",SpriteNumber);
	Wait(SIGBREAKF_CTRL_C);  /* Wait forever until sent a Ctrl-C */
	printf("Sprite number %d deallocated.\n",SpriteNumber);

	if(!NoRequester)  /* If user didn't specify NOREQUESTER... */
		AutoRequest(NULL,&BodyText,NULL,&OkayText,NULL,NULL,320,72);

	ReleaseResources();  /* Free all allocated memory, close libraries etc. */
}

void ReleaseResources()
{
	if(EmptySprite.num != -1)  /* If the sprite was allocated, free it.    */
		FreeSprite(EmptySprite.num);

	if(GfxBase)                /* If graphics.library was open, close it.  */
		CloseLibrary(GfxBase);

	if(IntuitionBase)          /* If intuition.library was open, close it. */
		CloseLibrary(IntuitionBase);

	exit(0);
}
