//
//  main.m
//  injectSources
//
//  Created by John Holdsworth on 19/03/2015.
//  Copyright (c) 2015 John Holdsworth. All rights reserved.
//

#import <stdio.h>
#import "JuicePluginController.h"

int main( int argc, const char * argv[] )
{
    if ( argc < 3 ) {
        printf( "Usage: [injectSources,patchProject,unpatchProject] workspacePath [filesToInject..]\n\n" );
        exit(1);
    }

    @autoreleasepool {
        NSConnection *theConnection = [NSConnection connectionWithRegisteredName:JUICE_ENGINE_DO
                                                                   host:nil];
        id<JuiceService> juiceEngine = (id<JuiceService>)[theConnection rootProxy];
        [(id)juiceEngine setProtocolForProxy:@protocol(JuiceService)];

        const char *action = argv[1];

        NSString *workspacePath = [NSString stringWithUTF8String:argv[2]];
        [juiceEngine workspaceChanged:workspacePath logDirectory:nil];

        if ( strcmp( action, "patchProject" ) == 0 )
            exit( [juiceEngine patchProject] ? 0 : 1 );
        else if ( strcmp( action, "unpatchProject" ) == 0 )
            [juiceEngine unpatchProject];
        else if ( strcmp( action, "injectSources" ) == 0 ) {
            NSMutableArray *sources = [NSMutableArray new];

            for ( int arg = 3 ; arg < argc ; arg++ )
                [sources addObject:[NSString stringWithUTF8String:argv[arg]]];

            [juiceEngine appCode:workspacePath injectSources:sources];
        }
        else {
            printf( "Invalid action %s\n\n", action );
            exit(1);
        }
    }

    sleep(1);
    return 0;
}
