//
//  TeXDistSystemVersion.m
//  TeXDist
//
//  Created by coder on Tue Jun 14 08:40:45 UTC 2011.
//  Copyright 2011 Laurens'Tribune. All rights reserved.
//

#import "TeXDistSystemVersion.h"
#define NOT !
#define OR ||
#define AND &&

@interface TeXDistSystemVersion()
- (id)initWithString:(NSString *)aString error:(NSError **)RORef;
@end

@implementation TeXDistSystemVersion
@synthesize major = iVarMajor;
@synthesize minor = iVarMinor;
@synthesize bugFix = iVarBugFix;
+ (void)initialize;
{
    NSLog(@"TeXDist Preference Pane: running OS is %@",[self runningSystemVersion]);
    return;
}
+ (id)systemVersionWithString:(NSString *)aString error:(NSError **)RORef;
{
    return [[[self alloc] initWithString:aString error:RORef] autorelease];
}
- (id)initWithString:(NSString *)aString error:(NSError **)RORef;
{
    if ((self = [self init])) {
        self.major = 10;
        self.minor = self.bugFix = 0;
        if ([aString isKindOfClass:[NSString class]]) {
            NSEnumerator * E = [aString componentsSeparatedByString:@"."].objectEnumerator;
            NSString * component = nil;
            if ((component = E.nextObject)) {
                self.major = MAX(component.integerValue,10);
                if ((component = E.nextObject)) {
                    self.minor = MAX(component.integerValue,0);
                    if ((component = E.nextObject)) {
                        self.bugFix = MAX(component.integerValue,0);
                    }
                }
            }
        } else if (RORef) {
            *RORef = [NSError errorWithDomain:@"TeXDist" code:1 userInfo:
                [NSDictionary dictionaryWithObjectsAndKeys:@"Bad parameter.",NSLocalizedDescriptionKey,nil]];
        }
    }
    return self;
}
- (NSComparisonResult)compare:(TeXDistSystemVersion *)rhs
{
    NSParameterAssert(NOT rhs OR [rhs isKindOfClass:[TeXDistSystemVersion class]]);
    if (self.major < rhs.major) {
        return NSOrderedAscending;
    }
    if (self.major > rhs.major) {
        return NSOrderedDescending;
    }
    if (self.minor < rhs.minor) {
        return NSOrderedAscending;
    }
    if (self.minor > rhs.minor) {
        return NSOrderedDescending;
    }
    if (self.bugFix < rhs.bugFix) {
        return NSOrderedAscending;
    }
    if (self.bugFix > rhs.bugFix) {
        return NSOrderedDescending;
    }
    return NSOrderedSame;
}
+ (TeXDistSystemVersion *)firstSystemVersion;
{
    static TeXDistSystemVersion * TeXDist_firstSystemVersion = nil;
    return TeXDist_firstSystemVersion? :(TeXDist_firstSystemVersion=[[self systemVersionWithString:@"10.0.0" error:NULL] retain]);
}
+ (TeXDistSystemVersion *)lastSystemVersion;
{
    static TeXDistSystemVersion * TeXDist_lastSystemVersion = nil;
    if(NOT TeXDist_lastSystemVersion) {
        TeXDist_lastSystemVersion = [[self systemVersionWithString:@"10.0.0" error:NULL] retain];
        TeXDist_lastSystemVersion.major = TeXDist_lastSystemVersion.minor = TeXDist_lastSystemVersion.bugFix = NSUIntegerMax;
    }
    return TeXDist_lastSystemVersion;
}
+ (TeXDistSystemVersion *)runningSystemVersion;
{
    static TeXDistSystemVersion * TeXDist_runningSystemVersion = nil;
    if (NOT TeXDist_runningSystemVersion) {
        TeXDist_runningSystemVersion = [[self systemVersionWithString:@"10.0.0" error:NULL] retain];
        OSErr err;
        SInt32 systemVersion, versionMajor, versionMinor, versionBugFix;
        if (((err = Gestalt(gestaltSystemVersion, &systemVersion)) == noErr) AND (systemVersion < 0x1040)) {
            TeXDist_runningSystemVersion.major = ((systemVersion & 0xF000) >> 12) * 10 +
                ((systemVersion & 0x0F00) >> 8);
            TeXDist_runningSystemVersion.minor = (systemVersion & 0x00F0) >> 4;
            TeXDist_runningSystemVersion.bugFix = (systemVersion & 0x000F);
        } else {
            if ((err = Gestalt(gestaltSystemVersionMajor, &versionMajor)) == noErr) TeXDist_runningSystemVersion.major = versionMajor;
            if ((err = Gestalt(gestaltSystemVersionMinor, &versionMinor)) == noErr) TeXDist_runningSystemVersion.minor = versionMinor;
            if ((err = Gestalt(gestaltSystemVersionBugFix, &versionBugFix)) == noErr) TeXDist_runningSystemVersion.bugFix = versionBugFix;
        }
    }
    return TeXDist_runningSystemVersion;
}
- (NSString *) description;
{
    return [NSString stringWithFormat:@"%lu.%lu.%lu",self.major,self.minor,self.bugFix];
}
@end
