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

NSDictionary * TeXDist_rm(NSString * path)
{
	TeXDistLog(-1,@"TeXDist INFO: running command similar to");
    TeXDistLog(-1,@"rm -Rf \"%@\"",path);
	NSFileManager * DFM = [NSFileManager defaultManager];
    NSError * ROR = nil;
	if (NOT [DFM fileExistsAtPath:path] OR [DFM removeItemAtPath:path error:&ROR]) {
        return [NSDictionary dictionaryWithObjectsAndKeys:
                [NSNumber numberWithInteger:kTeXDistOK], kTeXDistErrorStatusKey,
                    nil];
    }
    NSLog(@"ERROR:%@",ROR);
    return [NSDictionary dictionaryWithObjectsAndKeys:
			[NSNumber numberWithInteger:kTeXDistResetCantRemove], kTeXDistErrorStatusKey,
			path, kTeXDistCantRemoveKey,
				nil];

}

NSDictionary * TeXDist_mkdir(NSString * path, BOOL createIntermediates, NSDictionary *attributes)
{
	TeXDistLog(1,@"TeXDist INFO: running command similar to");
    TeXDistLog(1,@"mkdir %@\"%@\"",(createIntermediates?@"-p ":@""),path);
	NSFileManager * DFM = [NSFileManager defaultManager];
    NSError * ROR = nil;
	if ([DFM createDirectoryAtPath:path withIntermediateDirectories:createIntermediates attributes:attributes error:&ROR]) {
        return [NSDictionary dictionaryWithObjectsAndKeys:
                [NSNumber numberWithInteger:kTeXDistOK], kTeXDistErrorStatusKey,
                    nil];
    }
    NSLog(@"ERROR:%@",ROR);
    return [NSDictionary dictionaryWithObjectsAndKeys:
			[NSNumber numberWithInteger:kTeXDistResetCantCreateDirectory], kTeXDistErrorStatusKey,
			path, kTeXDistCantLinkSourceKey,
				nil];

}

NSDictionary * TeXDist_soft_link(NSString * destPath, NSString * path)
{
	// ln -s destination source
	// destPath is expected to be a directory, it is created if it does not exist
	TeXDistLog(1,@"TeXDist INFO: running command similar to");
	TeXDistLog(1,@"ln -s \"%@\" \"%@\"",destPath,path);
	BOOL isDirectory = NO;
	NSFileManager * DFM = [NSFileManager defaultManager];
	if ([DFM fileExistsAtPath:destPath isDirectory:&isDirectory]) {
		if (NOT isDirectory) {
			TeXDistLog(1,@"TeXDist INFO : A directory was expected at:%@",destPath);
			return [NSDictionary dictionaryWithObjectsAndKeys:
				[NSNumber numberWithInteger:kTeXDistMissingDirectory], kTeXDistErrorStatusKey,
				destPath, kTeXDisMissingDirectoryKey,
					nil];
		}
	} else {
        NSString * dirPath = path.stringByDeletingLastPathComponent;
        if (dirPath.length AND NOT [DFM createDirectoryAtPath:dirPath withIntermediateDirectories:YES attributes:nil error:NULL]) {
            TeXDistLog(1,@"TeXDist INFO : Could not create a directory at:%@",dirPath);
            return [NSDictionary dictionaryWithObjectsAndKeys:
                [NSNumber numberWithInteger:kTeXDistCantCreate], kTeXDistErrorStatusKey,
                dirPath, kTeXDistCantCreateKey,
                    nil];
        }
    }
	NSString * safePath = [path stringByAppendingPathExtension:@"safe"];
	if ([DFM fileExistsAtPath:safePath] OR [DFM destinationOfSymbolicLinkAtPath:safePath error:NULL]) {
		if (NOT [DFM removeItemAtPath:safePath error:NULL]) {
			TeXDistLog(TeXDistLogLevelAlways,@"****  TeXDist INFO: Could not remove %@!",safePath);
			return [NSDictionary dictionaryWithObjectsAndKeys:
				[NSNumber numberWithInteger:kTeXDistCantRemove], kTeXDistErrorStatusKey,
				safePath, kTeXDistCantRemoveKey,
					nil];
		}
	}
	if (NOT [DFM createSymbolicLinkAtPath:safePath withDestinationPath:destPath error:NULL]) {
		TeXDistLog(TeXDistLogLevelAlways,@"****  TeXDist INFO: Could not link %@ to %@!",safePath,destPath);
		return [NSDictionary dictionaryWithObjectsAndKeys:
			[NSNumber numberWithInteger:kTeXDistCantLink], kTeXDistErrorStatusKey,
			safePath, kTeXDistCantLinkSourceKey,
			destPath, kTeXDistCantLinkDestinationKey,
				nil];
	}
	if ([DFM fileExistsAtPath:path] OR [DFM destinationOfSymbolicLinkAtPath:path error:NULL]) {
		if (NOT [DFM removeItemAtPath:path error:NULL]) {
			TeXDistLog(TeXDistLogLevelAlways,@"****  TeXDist INFO: Could not remove %@!",path);
			return [NSDictionary dictionaryWithObjectsAndKeys:
				[NSNumber numberWithInteger:kTeXDistCantRemove], kTeXDistErrorStatusKey,
				safePath, kTeXDistCantRemoveKey,
					nil];
		}
	}
	if (NOT [DFM moveItemAtPath:safePath toPath:path error:NULL]) {
		TeXDistLog(TeXDistLogLevelAlways,@"****  TeXDist INFO: Could not move %@ to %@!",safePath,path);
		return [NSDictionary dictionaryWithObjectsAndKeys:
			[NSNumber numberWithInteger:kTeXDistCantMove], kTeXDistErrorStatusKey,
			safePath, kTeXDistCantMoveFromKey,
			path, kTeXDistCantMoveToKey,
				nil];
	}
    if ([destPath.lowercaseString isEqual:[[DFM destinationOfSymbolicLinkAtPath:path error:NULL] lowercaseString]]) {
        TeXDistLog(1,@"TeXDistUtility INFO: Everything went OK");
        TeXDistLog(1,@"%@ -> %@", path, destPath);
        return [NSDictionary dictionaryWithObjectsAndKeys:
                            [NSNumber numberWithInteger:kTeXDistOK], kTeXDistErrorStatusKey,
                            nil];
    }
    return [NSDictionary dictionaryWithObjectsAndKeys:
			[NSNumber numberWithInteger:kTeXDistCantLink], kTeXDistErrorStatusKey,
			path, kTeXDistCantLinkSourceKey,
			destPath, kTeXDistCantLinkDestinationKey,
				nil];
}

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