#import "scFrontend.h"
#import <appkit/Application.h>
#import <appkit/Pasteboard.h>
#import <appkit/Speaker.h>
#import <appkit/Listener.h>
#import <appkit/defaults.h>
#import <libc.h>
#import <string.h>
#import <mach.h>


@implementation scFrontend : Object

- copyString:(char *)s {
  id p = [Pasteboard new];
  [p declareTypes:&NXAsciiPboard num:1 owner:self];
  [p writeType:NXAsciiPboard data:s length:strlen(s)];
  return self;
}

- launchTerminal:(char *)program {
  id p = [NXApp appSpeaker];
  port_t t = NXPortFromName("Terminal",NULL);
  int ok;
  if (t==PORT_NULL) return self;
  [p setSendPort:t];
  [self copyString:program];
  (void)[p msgPaste:&ok];
  return self;
}


static id openFile(const char *directory, const char *name)
/*
 * Opens a file with the given name in the specified directory.
 * Returns self if successful, nil otherwise.
 */
{
    char buffer[MAXPATHLEN+1], path[MAXPATHLEN+1], cmd[MAXPATHLEN+1];

    if (name) {
        if (!directory) {
            directory = ".";
        } else if (*directory != '/') {
            strcpy(buffer, "./");
            strcat(buffer, directory);
            directory = buffer;
        }
        if (!chdir(directory) && getwd(path)) {
	    if (strlen(name)) {
	    	sprintf(cmd,"cd %s;sc %s\n", path, name);
	    } else {
	    	sprintf(cmd,"cd %s;sc\n", path);
	    }
            return [[NXApp delegate] launchTerminal:cmd];
	}
    }

    return nil;
}

/* Public methods */

- openBlankSheet:sender
{
    openFile(".","");
    [NXApp hide:sender];
    return self;
}
    
/*
 * Application object delegate methods.
 */

- appDidInit:sender
/*
 * Check for files to open specified on the command line.
 * If there are no open documents, then open a blank one.
 */
{
    int i;
    char buffer[MAXPATHLEN+1];
    char *directory, *name, *ext;
    
    if (NXArgc > 1) {
        for (i = 1; i < NXArgc; i++) {
            strcpy(buffer, NXArgv[i]);
            ext = strrchr(buffer, '.');
            if (!ext || strcmp(ext, ".pdsc")) strcat(buffer, ".pdsc");
            name = strrchr(buffer, '/');
            if (name) {
                *name++ = '\0';
                directory = buffer;
            } else {
                name = buffer;
                directory = NULL;
            }
            openFile(directory, name);
	    [NXApp hide:self];
        }
    } else {
	if (gotFile!=YES)
	    [[NXApp delegate] openBlankSheet:self];
    }

    return self;
}

- (int)appOpenFile:(const char *)path type:(const char *)type
/*
 * This method is performed whenever a user double-clicks on an icon
 * in the Workspace Manager representing a PD SC program document.
 */
{
    char *name;
    char directory[MAXPATHLEN+1];

    if (type && !strcmp(type, "pdsc")) {
        strcpy(directory, path);
        name = strrchr(directory, '/');
        if (name) {
            *name++ = '\0';
            if (openFile(directory, name)) {
		[NXApp hide:self];
                return gotFile= YES;
            }
        }
    }

    return NO;
}

- (BOOL)appAcceptsAnotherFile:sender
/*
 * We accept any number of appOpenFile:type: messages.
 */
{
    return YES;
}

@end
