//
//  main.m
//  TeXDistUtility-main
//
//  Created by coder on 04/12/06.
//  Copyright Laurens'Tribune 2006. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "TeXDistCommon.h"
#import "TeXDistUtility.h"

NSDictionary * TeXDist_help(void);

int main(int argc, char *argv[])
{
NSLog(@"%@",[NSProcessInfo processInfo].arguments);
	TeXDistLog(2,@"TeXDist INFO : authorized tool:");
	NSAutoreleasePool * POOL = [[NSAutoreleasePool alloc] init];
    TeXDistLog(2,@"[[NSFileManager defaultManager] currentDirectoryPath] %@",[[NSFileManager defaultManager] currentDirectoryPath]);
	NSEnumerator * E = [NSProcessInfo processInfo].arguments.objectEnumerator;
    [E nextObject];// Ignore $0
    NSString * argument = E.nextObject;
    NSDictionary * statusDictionary = nil;
    int result = 0;
    NSString * destPath = nil;
    NSString * path = nil;
    BOOL yorn = NO;
    if ([argument isEqual:TeXDistUtilityCommand_ln]) {
        while ((argument = E.nextObject)) {
            if ([argument isEqual:TeXDistUtilityOption_dest_path]) {
                destPath = E.nextObject;
            } else if ([argument isEqual:TeXDistUtilityOption_path]) {
                path = E.nextObject;
            }
        }
        if (NOT destPath.length) {
            TeXDistLog(0,@"TeXDist INFO : No destPath given");
            statusDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                [NSNumber numberWithInteger:kTeXDistArgMissingDestinationPath], kTeXDistErrorStatusKey,
                    nil];
terminate:
            [statusDictionary TeXDist_writeTo_stdout];
            result = [[statusDictionary objectForKey:kTeXDistErrorStatusKey] intValue];
            [POOL drain];
            return result;
        }
        if (NOT path.length) {
            TeXDistLog(0,@"TeXDist INFO : No path given");
            statusDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                [NSNumber numberWithInteger:kTeXDistArgMissingPath], kTeXDistErrorStatusKey,
                    nil];
            goto terminate;
        }
        statusDictionary = TeXDist_soft_link(destPath, path);
    } else if ([argument isEqual:TeXDistUtilityCommand_mkdir]) {
        while ((argument = E.nextObject)) {
            if ([argument isEqual:TeXDistUtilityOption_create_intermediates]) {
                yorn = [E.nextObject boolValue];
            } else if ([argument isEqual:TeXDistUtilityOption_path]) {
                path = E.nextObject;
            }
        }
        if (NOT path.length) {
            TeXDistLog(0,@"TeXDist INFO : No path given either");
            statusDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                [NSNumber numberWithInteger:kTeXDistArgMissingPath], kTeXDistErrorStatusKey,
                    nil];
            goto terminate;
        }
        statusDictionary = TeXDist_mkdir(path,yorn,nil);
    } else if ([argument isEqual:TeXDistUtilityCommand_rm]) {
        while ((argument = E.nextObject)) {
            if ([argument isEqual:TeXDistUtilityOption_path]) {
                path = E.nextObject;
            }
        }
        if (NOT path.length) {
            TeXDistLog(0,@"TeXDist INFO : No path given either");
            statusDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                [NSNumber numberWithInteger:kTeXDistArgMissingPath], kTeXDistErrorStatusKey,
                    nil];
            goto terminate;
        }
        statusDictionary = TeXDist_rm(path);
    } else if ([argument isEqual:kTeXDistHelpArgument]) {
        statusDictionary = TeXDist_help();
    }
    goto terminate;
}

NSDictionary * TeXDist_help(void)
{
    NSLog(@"TeXDistUtility usage.");
    NSLog(@"TeXDistUtility %@ %@ %@ %@",TeXDistUtilityCommand_mkdir,TeXDistUtilityOption_path,@"PATH",TeXDistUtilityOption_create_intermediates);
    NSLog(@"TeXDistUtility %@ %@ %@",TeXDistUtilityCommand_mkdir,TeXDistUtilityOption_path,@"PATH");
    NSLog(@"TeXDistUtility %@ %@ %@ %@ %@",TeXDistUtilityCommand_ln,TeXDistUtilityOption_dest_path,@"DESTINATION",TeXDistUtilityOption_path,@"PATH");
    return [NSDictionary dictionary];
}

//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=  
