#include <workbench/workbench.h>

int newtype, modify, reset, nocare;
struct Library *IconBase;
 
main(ac, av)
char **av;
{
if(match(av[1], "?")) {
	printf("ICON RESET/S,DISK/S,DRAWER/S,TOOL/S,PROJECT/S,GARBAGE/S,DEVICE/S,KICK/S\n");
	Exit(0);
	}
if(!(IconBase = (struct Library *)OpenLibrary("icon.library", 0))) {
	printf("Couldn't open icon.library.\n");
	Exit(20);
	}
while(*++av) {
	if(match(*av, "DISK")) {
		modify = 1;
		newtype = WBDISK;
	} else if(match(*av, "DRAWER")) {
		modify = 1;
		newtype = WBDRAWER;
	} else if(match(*av, "TOOL")) {
		modify = 1;
		newtype = WBTOOL;
	} else if(match(*av, "PROJECT")) {
		modify = 1;
		newtype = WBPROJECT;
	} else if(match(*av, "GARBAGE")) {
		modify = 1;
		newtype = WBGARBAGE;
	} else if(match(*av, "KICK")) {
		printf("Cannot modify kickstart icons.\n");
		CloseLibrary(IconBase);
		Exit(10);
	} else if(match(*av, "FORCE")) {
		nocare = 1;
	} else if(match(*av, "RESET")) {
		modify = 1;
		reset = 1;
	} else if(match(*av, "FILE")) {
		++av;
		if(!*av) {
			printf("No file name specified.\n");
			CloseLibrary(IconBase);
			Exit(10);
			}
		munge(*av);
	} else munge(*av);
	}
CloseLibrary(IconBase);
}
 
static char *typenames[] = {
	"UNKNOWN",
	"DISK",
	"DRAWER",
	"TOOL",
	"PROJECT",
	"GARBAGE",
	"DEVICE",
	"KICK",
	"UNKNOWN"
};

munge(file)
char *file;
{
struct DiskObject *GetDiskObject(), *dobj, *drawerobj;
int errno, newclass, oldclass;
char *info=file+(strlen(file)-5);

if (info>=file && match(".info", info)) *info=0;
dobj = GetDiskObject(file);
if(!dobj) {
	if(errno=IoErr())
		printf("Can't open %s.info -- error %d.\n", file, errno);
	else
		printf("Not an icon: %s.info.\n", file);
	return;
	}
drawerobj=GetDiskObject("SYS:Empty");
if(!drawerobj) {
	printf("Can't open Empty.info -- error %d.\n", IoErr());
	FreeDiskObject(dobj);
	return;
	}

if(modify) {
	if(reset)
		dobj->do_CurrentX = dobj->do_CurrentY = NO_ICON_POSITION;
	if(newtype) {
		switch(newtype) {
			case WBDISK: case WBDEVICE:
			case WBDRAWER: case WBGARBAGE:
				newclass = WBDRAWER;
				break;
			case WBTOOL: case WBPROJECT:
				newclass = WBTOOL;
				break;
			default:
				newclass = 0;
				break;
			}
		switch(dobj->do_Type) {
			case WBDISK: case WBDEVICE:
			case WBDRAWER: case WBGARBAGE:
				oldclass = WBDRAWER;
				break;
			case WBTOOL: case WBPROJECT:
				oldclass = WBTOOL;
				break;
			default:
				oldclass = 0;
				break;
			}
		if(oldclass != newclass) {
			dobj->do_DrawerData=drawerobj->do_DrawerData;
			printf("I'm not supposed to convert %s to %s.\n",
				   typenames[newtype], typenames[dobj->do_Type]);
			if(!nocare) {
				FreeDiskObject(dobj);
				FreeDiskObject(drawerobj);
				return 0;
				}
			}
		dobj->do_Type = newtype;
		}
	PutDiskObject(file, dobj);
	}
 
printf("%s -- %s", file, typenames[dobj->do_Type]);
if(dobj->do_CurrentX == NO_ICON_POSITION || dobj->do_CurrentY == NO_ICON_POSITION)
	printf(", free to move.\n");
else
	printf(" at %d, %d.\n", dobj->do_CurrentX, dobj->do_CurrentY);

FreeDiskObject(dobj);
FreeDiskObject(drawerobj);
}
 
match(s1, s2)
register char *s1, *s2;
{
while(*s1 && *s2 && tolower(*s1)==tolower(*s2)) s1++, s2++;
return !*s1 && !*s2;
}
