#!/usr/bin/perl

# ARGV[0] = Path to package being installed 
# ARGV[1] = Destination path of where package is being installed
# ARGV[2] = Mountpoint of the destination volume
# ARGV[3] = Path to the directory containing the System folder that contains the active Installation framework. 

## Process "*_actions" scripts e.g. postflight_actions

$0 =~ m/.*\/(.*)$/;
my $stage = $1;
my $actionsDir = $ARGV[0] . "/Contents/Resources/" . $stage . "_actions/";

my $actionResult = 0;

foreach my $action (`/bin/ls \"$actionsDir\"`) {
	chomp($action);
	system($actionsDir . $action, $ARGV[0], $ARGV[1], $ARGV[2], $ARGV[3]);
	$actionResult = ($? << 8);
}

## ARD-specific stuff below this point -- should eventually move to its own "postflight_actions" script

use strict;

########################################################
my $PKGDIR          = $ARGV[0];
my $TARGET          = $ARGV[2];
my $SYSTEM_VERS 	= $TARGET . "/System/Library/CoreServices/SystemVersion.plist";
my $EXIT_VALUE 		= 0;

DO_CHECKS: 
{
    if (CheckVersion( $SYSTEM_VERS, "10.3", "ProductVersion", "<" )) 
    {
		## Move the ARD Jaguar Prefs Pane from temporary install location
		## to official Jaguar location.

		print STDERR "Moving preference pane...";
		
		system( "rm", "-rf", $TARGET . "/System/Library/PreferencePanes/ARDPref.prefPane" );
        system( "/bin/cp", "-Rp",
                $TARGET . "/System/Library/CoreServices/RemoteManagement/ARDAgent.app/Contents/Resources/ARDPref.prefPane/",
                $TARGET . "/System/Library/PreferencePanes/ARDPref.prefPane/" );

		## Put the ARD Jaguar Prefs pane into the "Network" row of
		## System Preferences

		system("$PKGDIR/Contents/Resources/add_ard_to_sys_prefs", "$TARGET");
		system("touch", "$TARGET/System/Library/PreferencePanes");
    }
}


## Run postflight makeuser and/or kickstart events, if any
system("$PKGDIR/Contents/Resources/postflight_kickstart", "$TARGET");

exit($EXIT_VALUE);

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

sub CheckVersion
{
    my $path            = $_[0];
    my $version         = $_[1];
    my $keyName         = $_[2];
    my $operator        = $_[3];
    my $i;
    my $oldSeperator;
    my $plistData;
    my @items;
    my $item;
    my @theVersionArray;
    my @versionArray;
    my %versiondata;

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

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

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

}
