/* OpenAmiga.org
 *  This file is part of the OpenAmiga "xfdmaster library project"
 *
 * For more information look at:
 *   http://openamiga.org/?function=viewproject&projectid=67
 */

#include "utils.h"
#include <string.h>
#include <stdio.h>

static const char USED version[] = "\0$VER: xfdLibInfo 53.4 (29.8.2014)";

static const CONST_STRPTR flags[16] = {
	"RELOC",
	"ADDR",
	"DATA",
	NULL,
	"PASSWORD",
	"RELMODE",
	"KEY16",
	"KEY32",
	"RECOGLEN",
	"USERTARGET",
	NULL,
	NULL,
	NULL,
	NULL,
	"M68K",
	"EXTERN"
};

int main(void) {
	struct xfdMasterBase *XfdBase;
	struct xfdSlave *slave;
	TEXT buffer[128];

	signal(SIGINT, SIG_IGN);

	IDOS->Printf(CSI"1m%s"CSI"0;32m (C) 2014 Fredrik Wikstrom"CSI";31m\n\n",
		version + 7);

	XfdBase = (struct xfdMasterBase *)IExec->OpenLibrary("xfdmaster.library", 53);
	if (XfdBase == NULL) {
		IDOS->PutErrStr("You need xfdmaster.library v53+\n");
		return RETURN_FAIL;
	}

	IDOS->Printf(CSI"4mContents of xfdmaster.library %lu.%lu:"CSI"0m\n\n",
		XfdBase->LibNode.lib_Version,
		XfdBase->LibNode.lib_Revision);

	IDOS->PutStr("Name                           |  ID  | MV | RB | DB | F1 | F2 | XFDPFF_#?\n");
	IDOS->PutStr("-------------------------------+------+----+----+----+----+----+-----------\n");

	slave = XfdBase->xfdm_FirstSlave;
	while (slave != NULL) {
		UBYTE flag;
		snprintf(&buffer[0], 30 + 1, "%-30s", slave->xfds_PackerName);
		strcpy(&buffer[30], " | ");
		if (!(slave->xfds_PackerFlags & XFDPFF_EXTERN))
			snprintf(&buffer[33], 5, "%04X", slave->xfds_SlaveID);
		else
			strcpy(&buffer[33], "----");
		strcpy(&buffer[37], " | ");
		snprintf(&buffer[40], 3, "%u", slave->xfds_MasterVersion);
		strcpy(&buffer[42], " | ");
		strcpy(&buffer[45], slave->xfds_RecogBuffer ? "++" : "--");
		strcpy(&buffer[47], " | ");
		strcpy(&buffer[50], slave->xfds_DecrunchBuffer ? "++" : "--");
		strcpy(&buffer[52], " | ");
		strcpy(&buffer[55], slave->xfds_RecogSegment ? "++" : "--");
		strcpy(&buffer[57], " | ");
		strcpy(&buffer[60], slave->xfds_DecrunchSegment ? "++" : "--");
		strcpy(&buffer[62], " | ");
		for (flag = 0; flag < 16; flag++) {
			if ((slave->xfds_PackerFlags & (1<<flag)) && flags[flag] != NULL) {
				if (buffer[65] != '\0') strlcat(&buffer[65], "!", sizeof(buffer) - 65);
				strlcat(&buffer[65], flags[flag], sizeof(buffer) - 65);
			}
		}
		IDOS->Printf("%s\n", buffer);
		slave = slave->xfds_Next;
	}
	
	IExec->CloseLibrary((struct Library *)XfdBase);

	return RETURN_OK;
}

