#!/usr/bin/perl

my $SYSTEM_VERS = "$ARGV[0]"."/System/Library/CoreServices/SystemVersion.plist";
my $EXIT_VALUE = 0;
my $QT_VERS = "$ARGV[0]"."/System/Library/Frameworks/QuickTime.framework/Versions/A/Resources/Info.plist";
my $Package_Path = $ENV{PACKAGE_PATH};
my $foundApp = 0;
my $hasG4 = 1;
my $badOS = 0;
my $badQT = 0;

DO_CHECKS: {

        if(CheckVersion("$SYSTEM_VERS", "10.2.5", "ProductVersion", "<")) {
                $badOS = 1;
        }

        if(CheckVersion("$QT_VERS", "6.4", "CFBundleShortVersionString", "<")) {
                $badQT = 1;
        }

        if ($badOS)
        {
		$EXIT_VALUE = (( 1 << 5 ) | 16 );
        }

        if ($badQT)
        {
		$EXIT_VALUE = (( 1 << 5 ) | 17 );
        }

        if ($badOS && $badQT)
        {
		$EXIT_VALUE = (( 1 << 5 ) | 18 );
        }

	if($ARGV[0] ne "/" && CheckVersion("$SYSTEM_VERS", "10.3", "ProductVersion", "<")) {
                $EXIT_VALUE = (( 1 << 5 ) | 19 );
                last;
        }

        if(CheckVersion("/Applications/Final Cut Express.app/Contents/Info.plist", "2.0", "CFBundleShortVersionString", ">=")) {
                if(CheckVersion("/Applications/Final Cut Express.app/Contents/Info.plist", "2.0.3", "CFBundleShortVersionString", "<=")) {
                        $foundApp = 1;
                }
        }

	if (!$foundApp)
	{
		$EXIT_VALUE = (( 1 << 5 ) | 22 );
	}

	if ($ENV{'COMMAND_LINE_INSTALL'})
        {
                last;
        }

}

exit($EXIT_VALUE);

###

sub CheckVersion
{
    my $path            = $_[0];
    my $version         = $_[1];
    my $keyName         = $_[2];
    my $operator        = $_[3];

    if (! -e $path) {
        return 0;
    }

    if (!$operator) {
        $operator = "==";
    }

    my $oldSeperator = $/;
    $/ = \0;

    open( PLIST, "$path") || do {
        return 0;
    };

    $plistData = <PLIST>;
    $plistData =~ /<dict>(.*?)<\/dict>/gis;

    @items = split(/<key>/, $plistData);

    shift @items;
    foreach $item (@items) {
        $item =~ /(.*?)<\/key>.*?<string>(.*?)<\/string>/gis;
        $versiondata{ $1 } = $2;
    }

    close(PLIST);

    $/ = $oldSeperator;

    @theVersionArray = split(/\./, $versiondata{$keyName});
    for ($i = 0; $i < 3; $i++) {
        if(!$theVersionArray[$i]) {
            $theVersionArray[$i] = '0';
        }
    }

    @versionArray = split(/\./, $version);
    
    my $actualVersion;

    for ($i = 0; $i < 3; $i++) {
        if (($theVersionArray[$i] != $versionArray[$i]) or ($i == 2)) {

            $actualVersion = $theVersionArray[$i];
            $version = $versionArray[$i];

            last;
        }
    }

    my $expression = '$actualVersion ' . $operator . ' $version';
    if( eval ($expression) )
    {
        return 1;
    }
    else
    {
        return 0;
    }

}

