#!/usr/bin/perl

#############################
my $resourcePath	= $ARGV[0] . "/Contents/Resources";
my $targetVolume	= $ARGV[2];

my $PlistBuddy		= $resourcePath . "/PlistBuddy";
my $Plist			= $targetVolume . "/Library/Preferences/com.apple.iMovie.plist";
my $defaultLocation	= $targetVolume . "/Library/Application Support/iMovie/Themes";

#############################
# determine where themes were installed as recorded in the plist
my $customLocation = `"$PlistBuddy" -c "Print :searchPathsForThemes:0" "$Plist"`;
chomp($customLocation);

# if the location does not already begin with /Volumes, add the targetVolume on the front
if ($customLocation !~ m|^/Volumes/| ) {
	$customLocation = $targetVolume . $customLocation;
}

#canonicalize file paths
use Cwd 'abs_path';
$defaultLocation = abs_path($defaultLocation);
$customLocation = abs_path($customLocation);

# If nothing didn't change none, don't move nothing nowhere
exit(0) if ($defaultLocation eq $customLocation);

#############################
# move theme content
system("/usr/bin/ditto", $defaultLocation, $customLocation);
system("/bin/rm", "-rf", $defaultLocation);
		
#############################
# And always
exit(0);

