#include <exec/types.h>
#include <workbench/workbench.h>
#include <workbench/startup.h>
#include <libraries/dos.h>

#include <clib/alib_protos.h>
#include <clib/icon_protos.h>
#include <clib/dos_protos.h>
#include <clib/exec_protos.h>

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

void cleanexit(int error);

struct Library *IconBase = NULL;
struct DiskObject *dobj = NULL;

void main(int argc,char *argv[]) {

	if(argc!=2) {
		printf("Usage:\n");
		printf("%s <file [.info]>");
		cleanexit(RETURN_OK);
	}

	if(!(IconBase=OpenLibrary("icon.library",33))) {
		printf("Couldn't open icon.library\n");
		cleanexit(RETURN_FAIL);
	}

	/* Strip .info from end of each filename if it ends that way */
	if(strlen(argv[1]) > 5 && !strcmp(&argv[1][strlen(argv[1])-5],".info"))
		argv[1][strlen(argv[1])-5]='\0';

	if(!(DeleteDiskObject(argv[1]))) {
		printf("File not deleted\n");
		cleanexit(RETURN_FAIL);
	}

	cleanexit(RETURN_OK);
}

void cleanexit(int error) {
	if(IconBase) CloseLibrary(IconBase);
	if(dobj) FreeDiskObject(dobj);
	exit(error);
}
