#!/usr/bin/perl

my $SYSVERS = "$ARGV[3]"."/System/Library/CoreServices/SystemVersion.plist";
my $TARGET  = "$ARGV[3]";
my $PKG_RESOURCES = "\Q$ARGV[0]";

##################

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;
    }

}

#######################################

# Remove obsolete plists [6367498]
#system("/bin/rm", "-f", "$TARGET/System/Library/LaunchDaemons/com.apple.IIDCAssistant.plist");
#system("/bin/rm", "-f", "$TARGET/private/etc/mach_init.d/IIDCAssistant.plist");
system("/bin/rm", "-f", "$TARGET/System/Library/LaunchDaemons/com.apple.mio.IIDCVideoAssistant.plist");
system("/bin/rm", "-f", "$TARGET/private/etc/mach_init.d/IIDCVideoAssistant.plist");
system("/bin/rm", "-f", "$TARGET/System/Library/LaunchDaemons/com.apple.mio.VDCAssistant.plist");
system("/bin/rm", "-f", "$TARGET/private/etc/mach_init.d/VDCAssistant.plist");
system("/bin/rm", "-f", "$TARGET/System/Library/LaunchDaemons/com.apple.mio.AVCAssistant.plist");
system("/bin/rm", "-f", "$TARGET/private/etc/mach_init.d/AVCAssistant.plist");

#<rdar://problem/6409619> Vestigial Assistants Should Be Removed
system("/bin/rm", "-f", "$TARGET/System/Library/PrivateFrameworks/CoreMediaIOServicesPrivate.framework/Versions/A/Resources/AVCAssistant");
system("/bin/rm", "-f", "$TARGET/System/Library/PrivateFrameworks/CoreMediaIOServicesPrivate.framework/Versions/A/Resources/IIDCVideoAssistant");
system("/bin/rm", "-f", "$TARGET/System/Library/PrivateFrameworks/CoreMediaIOServicesPrivate.framework/Versions/A/Resources/VDCAssistant");

#######################################

my $runnerPID = getppid();
my $installerPID = "";
my $uid = "";
my $uname = "";

open( PSOUT, "/bin/ps -axww -o pid,ppid -p $runnerPID |" );
while( <PSOUT> ) {
	my @fields = split '\s+', $_;
	if ("$fields[1]" eq "$runnerPID") {
		$installerPID = $fields[2];
	}
}
close( PSOUT );

if ("$installerPID" ne "") {
	open( PSOUT, "/bin/ps -axww -o pid,ruid -p $installerPID |" );
	while( <PSOUT> ) {
		my @fields = split '\s+', $_;
		if ("$fields[1]" eq "$installerPID") {
			$uid = $fields[2];
		}
	}
	close( PSOUT );
}

if ("$uid" ne "") {
	open( PSOUTA, "/usr/bin/id -p $uid |" );
	while( <PSOUTA> ) {
		my @fields = split '\s+', $_;
		if ("$fields[0]" eq "uid") {
			$uname = $fields[1];
		}
	}
	close( PSOUTA );
}

###################################################################

# Remove old unneeded quicktime pieces
my $CLEANUP_SCRIPT = "$PKG_RESOURCES" . "/Contents/Resources/cleancruft";
$cmd = "$CLEANUP_SCRIPT $PKG_RESOURCES \"$ARGV[1]\" \"$ARGV[2]\" \"$ARGV[3]\"";
system( "$cmd" );

########	
# Stuff the installation info into the preferences for QT Updates
my $INSTALL_INFO= "$PKG_RESOURCES" . "/Contents/Resources/installinfo";
my $cmd = "$INSTALL_INFO -postinstall -volume \"$ARGV[2]\" -type full";
#print STDOUT "INSTALL_INFO: $cmd\n";
system( "$cmd" );

########

# Configure the browser mime types based on the installed QuickTime
my $CONFIGURE_MIME_TYPES= "$PKG_RESOURCES" . "/Contents/Resources/ConfigureMimeTypes";
my $cmd = "$CONFIGURE_MIME_TYPES -rescan -verbose";
if ("$uname" ne "") {
	$cmd = "sudo -u $uname $CONFIGURE_MIME_TYPES -rescan -verbose";
}
#print STDOUT "CONFIGURE_MIME_TYPES: $cmd\n";
system( "$cmd" );

########

# Remove old receipts
system ("/bin/rm -rf \"$TARGET/Library/Receipts/QuickTime702.pkg\"");
system ("/bin/rm -rf \"$TARGET/Library/Receipts/QuickTime703.pkg\"");
system ("/bin/rm -rf \"$TARGET/Library/Receipts/QuickTime704\"*\".pkg\"");
system ("/bin/rm -rf \"$TARGET/Library/Receipts/QuickTime71.pkg\"");
system ("/bin/rm -rf \"$TARGET/Library/Receipts/QuickTime71Seed.pkg\"");
system ("/bin/rm -rf \"$TARGET/Library/Receipts/QuickTime711.pkg\"");
system ("/bin/rm -rf \"$TARGET/Library/Receipts/QuickTime712.pkg\"");
system ("/bin/rm -rf \"$TARGET/Library/Receipts/QuickTime713.pkg\"");
system ("/bin/rm -rf \"$TARGET/Library/Receipts/QuickTime715.pkg\"");
system ("/bin/rm -rf \"$TARGET/Library/Receipts/QuickTime716.pkg\"");
system ("/bin/rm -rf \"$TARGET/Library/Receipts/QuickTime71?Seed.pkg\"");
system ("/bin/rm -rf \"$TARGET/Library/Receipts/QuickTime720.pkg\"");
system ("/bin/rm -rf \"$TARGET/Library/Receipts/QuickTime730.pkg\"");
system ("/bin/rm -rf \"$TARGET/Library/Receipts/QuickTime740.pkg\"");
system ("/bin/rm -rf \"$TARGET/Library/Receipts/QuickTime741.pkg\"");
system ("/bin/rm -rf \"$TARGET/Library/Receipts/QuickTime745.pkg\"");
system ("/bin/rm -rf \"$TARGET/Library/Receipts/QuickTime75.pkg\"");

###########

#remove files in cleanup list (for Tiger)
my $Delete = "$PKG_RESOURCES" . "/Contents/Resources/deleteomatic";
my $TIGER_DELETED_FILES="$PKG_RESOURCES" . "/Contents/Resources/tigercleanup-list-post";
if((CheckVersion( "$SYSVERS", "10.4", "ProductVersion", ">=" )) && (CheckVersion( "$SYSVERS", "10.5", "ProductVersion", "<" ))) {
	system ("$Delete \"$ARGV[2]\" $TIGER_DELETED_FILES"); 
}

####cleanup is not absent in SEED package#######

my $CLEAN_SCRIPT = "$PKG_RESOURCES" . "/Contents/Resources/cleanup";
$cmd = "$CLEAN_SCRIPT $PKG_RESOURCES \"$ARGV[1]\" \"$ARGV[2]\" \"$ARGV[3]\"";
if(-e $CLEAN_SCRIPT) {
	system( "$cmd" );
}

#######################<rdar://problem/6968256> QuickTime 7.6.2 permissions issue
my $targDisk = $ARGV[2];

system("/usr/sbin/pkgutil", "--edit-pkg", "com.apple.pkg.Essentials", "--learn", $targDisk . "System/Library/Frameworks/CoreVideo.framework/Versions/A/CodeResources");
system("/usr/sbin/pkgutil", "--edit-pkg", "com.apple.pkg.Essentials", "--learn", $targDisk . "System/Library/Components/CoreMediaAuthoringPrivateComponents.component/Contents/CodeResources");
system("/usr/sbin/pkgutil", "--edit-pkg", "com.apple.pkg.Essentials", "--learn", $targDisk . "System/Library/PrivateFrameworks/CoreMediaAuthoringPrivate.framework/Versions/A/CodeResources");
system("/usr/sbin/pkgutil", "--edit-pkg", "com.apple.pkg.Essentials", "--learn", $targDisk . "System/Library/PrivateFrameworks/CoreMediaIOServicesPrivate.framework/Versions/A/CodeResources");
system("/usr/sbin/pkgutil", "--edit-pkg", "com.apple.pkg.Essentials", "--learn", $targDisk . "System/Library/PrivateFrameworks/CoreMediaIOServicesPrivate.framework/Versions/A/Resources/AVC.plugin/Contents/CodeResources");
system("/usr/sbin/pkgutil", "--edit-pkg", "com.apple.pkg.Essentials", "--learn", $targDisk . "System/Library/PrivateFrameworks/CoreMediaIOServicesPrivate.framework/Versions/A/Resources/IIDC.plugin/Contents/CodeResources");
system("/usr/sbin/pkgutil", "--edit-pkg", "com.apple.pkg.Essentials", "--learn", $targDisk . "System/Library/PrivateFrameworks/CoreMediaIOServicesPrivate.framework/Versions/A/Resources/MPEGAudioDecoder.component/Contents/CodeResources");
system("/usr/sbin/pkgutil", "--edit-pkg", "com.apple.pkg.Essentials", "--learn", $targDisk . "System/Library/PrivateFrameworks/CoreMediaIOServicesPrivate.framework/Versions/A/Resources/MPEGLayer2AudioEncoder.component/Contents/CodeResources");
system("/usr/sbin/pkgutil", "--edit-pkg", "com.apple.pkg.Essentials", "--learn", $targDisk . "System/Library/PrivateFrameworks/CoreMediaIOServicesPrivate.framework/Versions/A/Resources/Tundra.component/Contents/CodeResources");
system("/usr/sbin/pkgutil", "--edit-pkg", "com.apple.pkg.Essentials", "--learn", $targDisk . "System/Library/PrivateFrameworks/CoreMediaIOServicesPrivate.framework/Versions/A/Resources/VDC.plugin/Contents/CodeResources");
system("/usr/sbin/pkgutil", "--edit-pkg", "com.apple.pkg.Essentials", "--learn", $targDisk . "System/Library/PrivateFrameworks/CoreMediaPrivate.framework/Versions/A/CodeResources");
system("/usr/sbin/pkgutil", "--edit-pkg", "com.apple.pkg.Essentials", "--learn", $targDisk . "Applications/QuickTime Player.app/Contents/CodeResources");
system("/usr/sbin/pkgutil", "--edit-pkg", "com.apple.pkg.Essentials", "--learn", $targDisk . "Applications/QuickTime Player.app/Contents/PlugIns/AnnotationInspector.propPane/Contents/CodeResources");
system("/usr/sbin/pkgutil", "--edit-pkg", "com.apple.pkg.Essentials", "--learn", $targDisk . "Applications/QuickTime Player.app/Contents/PlugIns/AudioSettingsInspector.propPane/Contents/CodeResources");
system("/usr/sbin/pkgutil", "--edit-pkg", "com.apple.pkg.Essentials", "--learn", $targDisk . "Applications/QuickTime Player.app/Contents/PlugIns/DataRefInspector.propPane/Contents/CodeResources");
system("/usr/sbin/pkgutil", "--edit-pkg", "com.apple.pkg.Essentials", "--learn", $targDisk . "Applications/QuickTime Player.app/Contents/PlugIns/HintTrackInspector.propPane/Contents/CodeResources");
system("/usr/sbin/pkgutil", "--edit-pkg", "com.apple.pkg.Essentials", "--learn", $targDisk . "Applications/QuickTime Player.app/Contents/PlugIns/NetworkInspector.propPane/Contents/CodeResources");
system("/usr/sbin/pkgutil", "--edit-pkg", "com.apple.pkg.Essentials", "--learn", $targDisk . "Applications/QuickTime Player.app/Contents/PlugIns/SettingsInspector.propPane/Contents/CodeResources");
system("/usr/sbin/pkgutil", "--edit-pkg", "com.apple.pkg.Essentials", "--learn", $targDisk . "Applications/QuickTime Player.app/Contents/PlugIns/VisualTrackInspector.propPane/Contents/CodeResources");
system("/usr/sbin/pkgutil", "--edit-pkg", "com.apple.pkg.Essentials", "--learn", $targDisk . "System/Library/QuickTime/QuickTimeComponents.component/Contents/CodeResources");
system("/usr/sbin/pkgutil", "--edit-pkg", "com.apple.pkg.Essentials", "--learn", $targDisk . "System/Library/QuickTimeJava/QuickTimeJava.bundle/Contents/CodeResources");
system("/usr/sbin/pkgutil", "--edit-pkg", "com.apple.pkg.Essentials", "--learn", $targDisk . "System/Library/QuickTime/QuickTime3GPP.component/Contents/CodeResources");
system("/usr/sbin/pkgutil", "--edit-pkg", "com.apple.pkg.Essentials", "--learn", $targDisk . "System/Library/QuickTime/QuickTimeFireWireDV.component/Contents/CodeResources");
system("/usr/sbin/pkgutil", "--edit-pkg", "com.apple.pkg.Essentials", "--learn", $targDisk . "System/Library/QuickTime/QuickTimeH264.component/Contents/CodeResources");
system("/usr/sbin/pkgutil", "--edit-pkg", "com.apple.pkg.Essentials", "--learn", $targDisk . "System/Library/QuickTime/QuickTimeIIDCDigitizer.component/Contents/CodeResources");
system("/usr/sbin/pkgutil", "--edit-pkg", "com.apple.pkg.Essentials", "--learn", $targDisk . "System/Library/QuickTime/QuickTimeImporters.component/Contents/CodeResources");
system("/usr/sbin/pkgutil", "--edit-pkg", "com.apple.pkg.Essentials", "--learn", $targDisk . "System/Library/QuickTime/QuickTimeMPEG.component/Contents/CodeResources");
system("/usr/sbin/pkgutil", "--edit-pkg", "com.apple.pkg.Essentials", "--learn", $targDisk . "System/Library/QuickTime/QuickTimeMPEG4.component/Contents/CodeResources");
system("/usr/sbin/pkgutil", "--edit-pkg", "com.apple.pkg.Essentials", "--learn", $targDisk . "System/Library/QuickTime/QuickTimeStreaming.component/Contents/CodeResources");
system("/usr/sbin/pkgutil", "--edit-pkg", "com.apple.pkg.Essentials", "--learn", $targDisk . "System/Library/QuickTime/QuickTimeVR.component/Contents/CodeResources");
system("/usr/sbin/pkgutil", "--edit-pkg", "com.apple.pkg.Essentials", "--learn", $targDisk . "Library/Internet Plug-Ins/QuickTime Plugin.plugin/Contents/CodeResources");
system("/usr/sbin/pkgutil", "--edit-pkg", "com.apple.pkg.Essentials", "--learn", $targDisk . "Library/Internet Plug-Ins/QuickTime Plugin.webplugin/Contents/CodeResources");
system("/usr/sbin/pkgutil", "--edit-pkg", "com.apple.pkg.Essentials", "--learn", $targDisk . "System/Library/Frameworks/QTKit.framework/Versions/A/Resources/QTKitIBPlugin.ibplugin/Contents/CodeResources");
system("/usr/sbin/pkgutil", "--edit-pkg", "com.apple.pkg.Essentials", "--learn", $targDisk . "System/Library/Spotlight/QuickTime.mdimporter/Contents/CodeResources");
system("/usr/sbin/pkgutil", "--edit-pkg", "com.apple.pkg.Essentials", "--learn", $targDisk . "Library/Audio/Plug-Ins/HAL/iSightAudio.plugin/Contents/CodeResources");
system("/usr/sbin/pkgutil", "--edit-pkg", "com.apple.pkg.Essentials", "--learn", $targDisk . "System/Library/Frameworks/QuickTime.framework/Versions/A/CodeResources");
system("/usr/sbin/pkgutil", "--edit-pkg", "com.apple.pkg.Essentials", "--learn", $targDisk . "System/Library/PreferencePanes/QuickTime.prefPane/Contents/CodeResources");
system("/usr/sbin/pkgutil", "--edit-pkg", "com.apple.pkg.Essentials", "--learn", $targDisk . "System/Library/PreferencePanes/QuickTime.prefPane/Contents/Resources/QTAdvanced.prefPane/Contents/CodeResources");
system("/usr/sbin/pkgutil", "--edit-pkg", "com.apple.pkg.Essentials", "--learn", $targDisk . "System/Library/PreferencePanes/QuickTime.prefPane/Contents/Resources/QTAdvanced.prefPane/Contents/Resources/QTMediaKeys.bundle/Contents/CodeResources");
system("/usr/sbin/pkgutil", "--edit-pkg", "com.apple.pkg.Essentials", "--learn", $targDisk . "System/Library/PreferencePanes/QuickTime.prefPane/Contents/Resources/QTAdvanced.prefPane/Contents/Resources/QTMime.bundle/Contents/CodeResources");
system("/usr/sbin/pkgutil", "--edit-pkg", "com.apple.pkg.Essentials", "--learn", $targDisk . "System/Library/PreferencePanes/QuickTime.prefPane/Contents/Resources/QTAdvanced.prefPane/Contents/Resources/QTTransport.bundle/Contents/CodeResources");
system("/usr/sbin/pkgutil", "--edit-pkg", "com.apple.pkg.Essentials", "--learn", $targDisk . "System/Library/PreferencePanes/QuickTime.prefPane/Contents/Resources/QTPlugIn.prefPane/Contents/CodeResources");
system("/usr/sbin/pkgutil", "--edit-pkg", "com.apple.pkg.Essentials", "--learn", $targDisk . "System/Library/PreferencePanes/QuickTime.prefPane/Contents/Resources/QTRegister.prefPane/Contents/CodeResources");
system("/usr/sbin/pkgutil", "--edit-pkg", "com.apple.pkg.Essentials", "--learn", $targDisk . "System/Library/PreferencePanes/QuickTime.prefPane/Contents/Resources/QTRegister.prefPane/Contents/Resources/QTAbout.bundle/Contents/CodeResources");
system("/usr/sbin/pkgutil", "--edit-pkg", "com.apple.pkg.Essentials", "--learn", $targDisk . "System/Library/PreferencePanes/QuickTime.prefPane/Contents/Resources/QTStreaming.prefPane/Contents/CodeResources");
system("/usr/sbin/pkgutil", "--edit-pkg", "com.apple.pkg.Essentials", "--learn", $targDisk . "System/Library/PreferencePanes/QuickTime.prefPane/Contents/Resources/QTUpdate.prefPane/Contents/CodeResources");
system("/usr/sbin/pkgutil", "--edit-pkg", "com.apple.pkg.Essentials", "--learn", $targDisk . "System/Library/Components/AudioCodecs.component/Contents/CodeResources");
system("/usr/sbin/pkgutil", "--edit-pkg", "com.apple.pkg.Essentials", "--learn", $targDisk . "System/Library/PrivateFrameworks/CoreAUC.framework/Versions/A/CodeResources");
system("/usr/sbin/pkgutil", "--edit-pkg", "com.apple.pkg.Essentials", "--learn", $targDisk . "System/Library/QuickTime/AppleVAH264HW.component/Contents/CodeResources");

# Removed exit(0); because engineering builds wants to concat more stuff to the end of this file
