/*
	Restore
	Alex Waugh 1998,2002

	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.
	
	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

*/

/*	Includes  */

#include "DeskLib:Window.h"
#include "DeskLib:Error.h"
#include "DeskLib:Event.h"
#include "DeskLib:EventMsg.h"
#include "DeskLib:Handler.h"
#include "DeskLib:Hourglass.h"
#include "DeskLib:Icon.h"
#include "DeskLib:Menu.h"
#include "DeskLib:Msgs.h"
#include "DeskLib:Resource.h"
#include "DeskLib:Screen.h"
#include "DeskLib:Template.h"
#include "DeskLib:File.h"
#include "DeskLib:Filing.h"
#include "DeskLib:Sprite.h"
#include "DeskLib:GFX.h"

#include "AJWLib:Window.h"
#include "AJWLib:Menu.h"
#include "AJWLib:Msgs.h"
#include "AJWLib:Misc.h"
#include "AJWLib:Handler.h"
#include "AJWLib:Error.h"

#include <stdlib.h>
#include <string.h>
#include <stdio.h>

/*	Macros  */

#define VERSION "1.01 (1-Dec-02)"
#define DIRPREFIX "Restore"

#define menuitem_INFO 0
#define menuitem_QUIT 1

/*	Variables  */

window_handle mainwin;

/*	Functions  */

BOOL ReceiveDrag(event_pollblock *block, void *r)
{
	char *filename;
	char pathname[256];
	char suffixes[3][12]={".!Sprites",".!Sprites22",".!Sprites23"};
	int i;
	filename=block->data.message.data.dataload.filename;
	if (!File_IsDirectory(filename)) {
		Beep();
		return TRUE;
	}
	for (i=0;i<3;i++) {
		strcpy(pathname,filename);
		strcat(pathname,suffixes[i]);
		if (File_Exists(pathname)) {
			sprite_area sprite=NULL;
			os_error *err;
			char name[sprite_MAXNAME+1];
			char leafname[256];
			sprite=Sprite_LoadFile(pathname);
			Sprite_GetName(sprite,name,1);
			err=Sprite_Rename(sprite,name,Filing_GetLeafname(filename,leafname));
			if (err) {
				Error_Report(err->errnum,err->errmess);
			} else {
				Error_Check(Sprite_Save(sprite,pathname));
			}
			free(sprite);
		}
	}
	strcpy(pathname,"IconSprites ");
	strcat(pathname,filename);
	strcat(pathname,suffixes[0]);
	Wimp_StartTask(pathname);
	Window_ForceRedraw(-1,0,0,1000000,1000000);
	return TRUE;
}

BOOL CloseMainWin(event_pollblock *block, void *r)
{
	Event_CloseDown();
	return TRUE;
}


int main(void)
{
	/*Error_RegisterSignals();*/
	Resource_Initialise(DIRPREFIX);
	Msgs_LoadFile("Messages");
	Event_Initialise(Msgs_TempLookup("Task.Name:"));
	EventMsg_Initialise();
	Screen_CacheModeInfo();
	Template_Initialise();
	EventMsg_Claim(message_MODECHANGE,event_ANY,Handler_ModeChange,NULL);
	Event_Claim(event_CLOSE,event_ANY,event_ANY,Handler_CloseWindow,NULL);
	Event_Claim(event_OPEN,event_ANY,event_ANY,Handler_OpenWindow,NULL);
	Event_Claim(event_KEY,event_ANY,event_ANY,Handler_KeyPress,NULL);
	Event_Claim(event_REDRAW,event_ANY,event_ANY,Handler_HatchRedraw,NULL);
	Template_LoadFile("Templates");
	mainwin=Window_Create("Main",template_TITLEMIN);
	Window_Show(mainwin,open_CENTERED);
	EventMsg_Claim(message_DATALOAD,event_ANY,ReceiveDrag,NULL);
	Event_Claim(event_CLOSE,mainwin,event_ANY,CloseMainWin,NULL);
	while (TRUE) Event_Poll();
	return 0;
}

