/*
 * tests.c
 *
 * simple tests for !musmus to make sure that the new boot sequence is
 * installed and that the new wimp is actually loaded.
 */

#include "muscode.h"
#include "query.h"

/*
 | Hack: Certain versions of the new !Boot don't have BootResources:!Configure
 | 'visible' to RISC OS 3.1.
 |
 | MusMus retrieves the tile_1 texture from there, so if !Configure isn't
 | available we need to disallow the RPC texture option.
 |
 | The start-up tests will now (v1.24) not fail if just tile_1 is missing,
 | instead they will set this flag to true (ie. non-zero) and return success.
 */

int tile_1_na = 0;

/* for extra debug :( */
#include "desklib:msgs.h"

#include <stdlib.h>

/* The objects we check for when deciding if the !Boot is 'new' enough: */
static struct { char *file; int type; } required[] =
{
	{"Boot:Choices",0x1000},
	{"<Boot$ToBeLoaded>",0x1000},
	{"<Boot$ToBeTasks>",0x1000},
	/* {"BootResources:!Configure.tile_1",0xff9}, */
	{"BootResources:Configure.2dtools",0xff9},
	{"<Choices$Write>.Boot.Tasks",0x1000},
	{0,0}
};


/* These objects must either *not* exist, or be of the given type */
static struct { char *file; int type; } clash_list[] =
{
	{"<Choices$Write>.Boot.Tasks.Musmus",0x1000},
	{"<Choices$Write>.Boot.Tasks.Musmus.!Run",0xfeb},
	{"<Choices$Write>.Boot.Tasks.Musmus.Texture",0xff9},
	{ 0,0 }
};



static int new_boot(void)
{
	int i;
	for ( i=0 ; required[i].file ; i++ )
	{
		if ( File_Type(required[i].file) != required[i].type )
		{
			char tbuffer[512];
			Msgs_printf(tbuffer,"err.fmiss",required[i].file,
				File_Type(required[i].file),required[i].type );
			query_box2("err.fmiss",tbuffer);
			return 0;
		}
	}

	/* Hack: check for !Configure.tile_1 */
	tile_1_na = ( File_Type("BootResources:!Configure.tile_1") != 0xff9 );

	return 1;
}


static int file_clash(void)
{
	int i,t;
	for ( i=0 ; clash_list[i].file ; i++ )
	{
		t = File_Type(clash_list[i].file);
		if ( t!=-1 && t!=clash_list[i].type ) { return 1; }
	}
	return 0;
}







void startup_tests( void )
{
	/* Check that the new !Boot sequence is installed */
	if ( !new_boot() )
	{
		query_box("err.oldboot");
		exit(1);
	}

	/* Do we have a late enough version of the window manager? */
	if ( Module_Version("WindowManager")<320 ) { query_box("err.oldwimp"); }

	/* Are there files that would conflict with our operation? */
	if ( file_clash() )
		if ( query_box("err.clash") ) { exit(1); }
}

