//
//  main.m
//  TeXDist
//
//  Created by itexmac2 on 29/06/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import <Cocoa/Cocoa.h>
#import <Carbon/Carbon.h>
#define NOT !
#define AND &&
int main(int argc, char *argv[])
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    NSProcessInfo * PI = [NSProcessInfo processInfo];
    NSString * bundlePath = PI.arguments.objectEnumerator.nextObject;
    while (bundlePath.length>0 AND NOT [bundlePath.lastPathComponent.pathExtension isEqual:@"app"]) {
        bundlePath = bundlePath.stringByDeletingLastPathComponent;
    }
    CFURLRef myBundleURL = (CFURLRef)[NSURL fileURLWithPath:bundlePath];// bridge
    if (myBundleURL) {
        FSRef myBundleRef;
        if (CFURLGetFSRef(myBundleURL, &myBundleRef)) {
            NSLog(@"TeXDistPrefPane - Register help book: %@", myBundleURL);
            OSStatus err = AHRegisterHelpBook(&myBundleRef);
            if (err) {
                NSLog(@"TeXDistPrefPane - Could not register help book at %@\ndue to error:%i",myBundleURL,err);
            } else {
                NSLog(@"TeXDistPrefPane - Help book registered, if any...");
                NSLog(@"TeXDistPrefPane - openHelpAnchor:%@ inBook:%@",TeXDist_HELP_BOOK_ACCESS,TeXDist_HELP_BOOK_ID);
                [[NSHelpManager sharedHelpManager] openHelpAnchor:TeXDist_HELP_BOOK_ACCESS inBook:TeXDist_HELP_BOOK_ID];
 
            }
        } else {
            NSLog(@"TeXDistPrefPane - Could not register help book at %@\nbecause I could not create a CFURLRef",myBundleURL);
        }
    } else {
        NSLog(@"TeXDistPrefPane - Could not register help book at %@\nbecause I could not create a FSRef",myBundleURL);
    }
    [pool release];
    return 0;
}
