#import "ScedApp.h"

#define TMP "/tmp"

/******** HERE IS WHAT TO DO
********* choose a suffix for a documenttype (eg fm)
********* choose a portname for a given application (eg FrameMaker) (or NULL)
********* find the proper extension
********* expand the map below 
********* edit Sced.iconheader (e.g.add: S       fm      Sced    ScedDoc)
********* make install
********* cmd-u for the workspacemanager
********/

struct tab {
	char *suffix;
	char *portname;
	char *properExtension;
} map[] = {
        { "fm" , "FrameMaker" , NULL},
        { "EPS" , NULL , "eps" },
	{ "fmb" , "FrameMaker", NULL },
	{ "cgm" , "IconBuilder" , NULL},
	{ "cgmb" , "IconBuilder" , NULL},
        { NULL , NULL, NULL}
};


@implementation ScedApp

int attp(char *text, char *a1, char *a2) {
	NXRunAlertPanel("Sced Error Message", text,"ok",NULL,NULL,a1,a2,NULL);
	return 0;
}

-  appDidInit:sender {
	
	[self hide:self];
	return(self);
}


- (int)openFile:(const char *)fullPath ok:(int *)flag {

	char *suffix;
	int i;
        int     msgDelivered, fileOpened; 
        port_t thePort;
        id  mySpeaker;
	char buffer[256];
	*flag=NO;

	/* find the suffix */
	if((suffix=rindex(fullPath,'.'))==NULL) {
		return(0);
	}
	suffix++;

	/* walk */
	i=0;
	while(map[i].suffix!=NULL) {
		if(strcmp(map[i].suffix,suffix)==0) {
			*flag=YES;
        		mySpeaker = [[Speaker alloc] init]; 
		        if(map[i].portname==NULL) {
				if(map[i].properExtension==NULL) {
					attp("Sced can't handle the configuration data for %s",map[i].suffix,"");
					return(1);
				}
				else
					thePort=NXPortFromName("Workspace", NULL);
			} else
				thePort = NXPortFromName(map[i].portname, NULL); 
        		if (thePort != PORT_NULL) {
                		[mySpeaker setSendPort:thePort]; 
                		
				if(map[i].properExtension==NULL) {
					strcpy(buffer,fullPath);
					msgDelivered = [mySpeaker openFile:fullPath ok:&fileOpened]; 
                		} else {
					strcpy(buffer,TMP);
					strcat(buffer,"/");
					suffix=rindex(fullPath,'/');
					if(suffix==NULL) strcat(buffer,fullPath);
					else strcat(buffer,suffix+1);
					suffix=rindex(buffer,'.');
					suffix++;
					strcpy(suffix,map[i].properExtension);
					if(symlink(fullPath,buffer)==0) 
						msgDelivered = [mySpeaker openTempFile:buffer ok:&fileOpened];
					else
						msgDelivered = [mySpeaker openFile:fullPath ok:&fileOpened];
				}
				if (msgDelivered == 0) {
                        		if (fileOpened == YES) 
                                		fprintf(stderr,"Sced invoced %s for %s with some success\n",map[i].portname,buffer);
                        		else
                                		attp("Sced failed to invoce %s for %s\n",map[i].portname,buffer);
                		}
                		else {
                                	attp("Sced failed to invoce %s for %s\n",map[i].portname,buffer);
                		}
        		}
        		else {
                                attp("Sced failed to invoce %s for %s\n",map[i].portname,fullPath);
        		}
        		[mySpeaker free];
        		port_deallocate(task_self(), thePort); 

			[self hide:self];
			return(0);
		}
		i++;
	}

	return(0);
}


- (BOOL) appAcceptsAnotherFile:sender {
	return(YES);
}

@end
