/*
** Created: Mon/22/Feb/1999
**
** Example program that uses Idm.library to obtain a files identity.
**
** Usage: GetID <nameoffile>
**
** Compiled with DICE v3.x
**
** This file is copyright © 1999 Andrew Bell.
**
*/

#include <ainc:system.h>
#include <GetID_rev.h>
#include <proto/exec.h>
#include <proto/dos.h>
#include <proto/Idm.h>
#include <libraries/Idm.h>

/*******************************************************************/
/* Prototypes */

LONG main( void );
BOOL InitPrg( void );
void EndPrg( void );
void DoErr( void );
void DoPrg( void );

/*******************************************************************/
/* Defines */

#define TITLE VERS " (" DATE ") Copyright © 1999 Andrew Bell.\n"
#define ARGPLATE "FILE/A"

struct ArgLayout
{
	UBYTE *ARG_FILE;
};

/*******************************************************************/
/* Variables */

struct ArgLayout ArgData;
struct RDArgs *ArgInfo    = NULL;
struct Library *IdmBase   = NULL;
struct IdmIdInfo *IdInfo  = NULL;

extern struct ExecBase *SysBase; /* let dlink pull these in */
extern struct DOSBase *DOSBase;

/*******************************************************************/

LONG main( void )
{
	if (SysBase->LibNode.lib_Version < 37) return RETURN_FAIL;

	if (InitPrg())
	{
		VPrintf(TITLE, NULL);
		DoPrg();
	}
	EndPrg();

	return RETURN_OK;
}

static const UBYTE EVer[] = VERSTAG;

/*******************************************************************/

BOOL InitPrg( void )
{
	if (!(ArgInfo = ReadArgs(ARGPLATE, &ArgData, NULL)))
	{
		DoErr(); return FALSE;
	}

	if (!(IdmBase = OpenLibrary(IDMNAME, IDMLIBVERSION)))
	{
		VPrintf("Sorry, you really need " IDMNAME " to use this program!\n", NULL);
		return FALSE;
	}

	if (!(IdInfo = IdmAllocIdInfo(NULL)))
	{
		VPrintf("Unable to obtain init Idm.library!\n", NULL);
		return FALSE;
	}

	return TRUE;
}

/*******************************************************************/

void EndPrg( void )
{
	if (IdInfo) { IdmFreeIdInfo(IdInfo); IdInfo = NULL; }
	if (IdmBase) { CloseLibrary(IdmBase); IdmBase = NULL; }
	if (ArgInfo) FreeArgs(ArgInfo);
}

/*******************************************************************/

void DoErr( void )
{
	PrintFault(IoErr(), "GetID error ");
}

/*******************************************************************/

void DoPrg( void )
{
	/*
	** REMEMBER: If you do not load the whole file to memory then you MUST
	**           use the following function to determine the smallest amount
	**           of bytes required to be loaded off the start of the file.
	*/

	ULONG ReadLen = IdmObtainIOLen();

	BPTR InFile = Open(ArgData.ARG_FILE, MODE_OLDFILE);

	if (InFile)
	{
		APTR FileBit = AllocVec(ReadLen, MEMF_CLEAR);

		if (FileBit)
		{
			ULONG r = Read(InFile, FileBit, ReadLen);

			if (r != ~NULL && r != NULL)
			{
				/*
				** Below is the function that does the identification. The following
				** paramters are passed:
				**
				** IdInfo   - This pointer is return by IdmAllocIdInfo()
				** FileBit  - Pointer to file is memory.
				** r        - Length of file (or the amount of bytes loaded off it)
				** NULL     - Taglist, non are defined so pass a NULL for now.
				*/

				UBYTE *FileIDString = IdmIdentifyMem(IdInfo, FileBit, r, NULL);

				if (FileIDString)
				{
					/* We get a pointer if the filetype is known */

					VPrintf("This file is a: %s\n", &FileIDString);
				}
				else
				{
					/* Else we get a NULL if the filetype is unknown */

					VPrintf("Idm.library was unable to identify this file!\n", NULL);
				}
			}
			else DoErr();

			FreeVec(FileBit);
		}
		Close(InFile);
	}
	else DoErr();
}
