#!/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. 

use File::Spec;

my $SOURCE_OF_GOODNESS;
my $packagePath = File::Spec->canonpath($ENV{'PACKAGE_PATH'});
if (-e "$packagePath/Contents/Installers/RemoteDesktopClient.pkg") {
	$SOURCE_OF_GOODNESS = $packagePath . "/Contents/Installers/";
} else {
	my @PATHARRAY = File::Spec->splitdir($packagePath);
	pop @PATHARRAY;
	my $packageContainerPath = File::Spec->catdir(@PATHARRAY);
	$SOURCE_OF_GOODNESS = $packageContainerPath . "/";
}

# The ARD app is not currently relocatable, so here's where we're installing stuff:
my $DESTINATION_IN_APP		= $ARGV[2] . "/Applications/Remote Desktop.app/Contents/Resources/Installers/";

# Now copy the Client mpkg from source to destination:
$TEMPDIR=`mktemp -d "$3/private/tmp/copypackages.XXXXXX"`;
chomp($TEMPDIR);
if ( ! -d "$TEMPDIR" ) {
        print "Error calling mktemp, exiting.";
        exit(1);
}

if ( -e "$DESTINATION_IN_APP/RemoteDesktopRMDB.pkg") {
	system("rm -rf '$DESTINATION_IN_APP/RemoteDesktopRMDB.pkg'");
}

# And now whack the placeholder
system( "/bin/rm", "-rf", $DESTINATION_IN_APP . "RemoteDesktopClient.pkg/Contents/Installers/placeholder");

# And now whack the old client mpkgs
system( "/bin/rm", "-rf", $DESTINATION_IN_APP . "RemoteDesktopClient200.mpkg");
system( "/bin/rm", "-rf", $DESTINATION_IN_APP . "RemoteDesktopClient210.mpkg");
system( "/bin/rm", "-rf", $DESTINATION_IN_APP . "RemoteDesktopClient220.mpkg");
system( "/bin/rm", "-rf", $DESTINATION_IN_APP . "RemoteDesktopClient.mpkg");

# And always return 0
exit(0);

