/* PBExecutableLocation.m created by luke on Sun 28-Sep-1997 */

#import "PBExecutableLocation.h"

static PBExecutableLocation *sharedPBExecutableLocation = nil;

@implementation PBExecutableLocation

+ (void)initialize
{
    [super initialize];

    if (sharedPBExecutableLocation)
        return;

    sharedPBExecutableLocation = [[self alloc] init];
}

- init
{
    NSNotificationCenter *notificationCenter;

    [super init];

    notificationCenter = [NSNotificationCenter defaultCenter];

    // Handle PBProjectWillOpenNotifications.
    [notificationCenter addObserver:self selector:@selector(projectWillOpen:)
                               name:PBProjectWillOpenNotification
                             object:nil];

    return self;
}

// This method is called just before a project is opened.  If the project is one which can have an
// executable (Application or Tool), it looks in the defaults database to see if an executable path
// has been specified.  If not, it specifies the appropriate executable path based on the Omni
// executable location standard.

- (void)projectWillOpen:(NSNotification *)notification
{
    NSString       *projectName, *projectTypeName, *executableKeyString, *selectedKeyString;
    NSArray        *executableList;
    NSDictionary   *executableDict;
    NSUserDefaults *defaults;
    PBProject      *project;

    // DEBUGGING
    NSLog(@"PBExecutableLocation.bundle: Handling PBProjectWillOpenNotification.");
    
    project         = (PBProject *)[notification object];
    projectName     = [project projectName];
    projectTypeName = [project projectTypeName];

    // Does this project even have an executable product?

    // DEBUGGING
    NSLog(@"projectTypeName: %@");
    
    if (![projectTypeName isEqualToString:@"Application"] && ![projectTypeName isEqualToString:@"Tool"]) {
        // DEBUGGING
        NSLog(@"This project doesn't have an executable product.");
        
        return;
    }

    // Look in defaults database and determine if an executable path has been set for this project.
    defaults            = [NSUserDefaults standardUserDefaults];
    executableKeyString = [NSString stringWithFormat:@"%@-OtherExecutables", projectName];
    selectedKeyString   = [NSString stringWithFormat:@"%@-selectedExecutable", projectName];
    executableList      = [defaults objectForKey:executableKeyString];

    // Are there no executable locations specified for this project?
    if ([executableList count] == 0) {
        NSArray *newExecutableList;

        executableDict    = [self executableDictionaryForProject:project];
        newExecutableList = [NSArray arrayWithObject:executableDict];

        NSLog(@"Setting new executable list to: %@\n", newExecutableList);
        [defaults setObject:newExecutableList forKey:[NSString stringWithFormat:executableKeyString]];
        [defaults setObject:[NSString stringWithFormat:@"%d", ([newExecutableList count] - 1)]
                     forKey:[NSString stringWithFormat:selectedKeyString]];
    } else {
        // DEBUGGING
        NSLog(@"This project already has an executable location specified.");
    }
}

- (NSDictionary *)executableDictionaryForProject:(PBProject *)project
{
    NSMutableDictionary *executableDict;
    NSString            *projectTypeName;
    NSString            *projectName;

    // DEBUGGING
    NSLog(@"Getting executable dictionary for current project.");
    
    projectName     = [project projectName];
    projectTypeName = [project projectTypeName];

    executableDict = [[NSMutableDictionary alloc] init];

    [executableDict setObject:[NSArray array]       forKey:@"args"];
    [executableDict setObject:[NSArray array]       forKey:@"asrcDirs"];
    [executableDict setObject:[NSArray array]       forKey:@"psrcDirs"];
    [executableDict setObject:[project projectName] forKey:@"name"];
    [executableDict setObject:@"."                  forKey:@"srcPath"];

    // TODO: Rather than directly specifying /Cache/%@/BuildOutput, etc., I should look at the 
    // value of the enviroment variable $OMNI_BUILD_OUTPUT_DIR.
    
    if ([projectTypeName isEqualToString:@"Application"]) {
        NSString *executablePath;

        executablePath = [NSString stringWithFormat:@"/Cache/%@/BuildOutput/Applications/%@/%@.app/%@",
            NSUserName(), projectName, projectName, projectName];

        [executableDict setObject:executablePath forKey:@"execPath"];
    } else {
        NSString *executablePath;

        executablePath = [NSString stringWithFormat:@"/Cache/%@/BuildOutput/Tools/%@/%@",
            NSUserName(), projectName, projectName];

        [executableDict setObject:executablePath forKey:@"execPath"];
    }

    return [executableDict autorelease];
}

@end
