File: FindOSGData.pl

package info (click to toggle)
openscenegraph 2.8.3-5
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 33,968 kB
  • ctags: 30,935
  • sloc: cpp: 287,169; ansic: 9,050; sh: 654; yacc: 548; objc: 374; makefile: 264; lex: 151; perl: 119
file content (119 lines) | stat: -rw-r--r-- 3,093 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/usr/bin/perl -w

# Author: Eric Wing
# 
# This calls mdfind to find Folders on your system called 
# "OpenSceneGraph-Data*" on your system. If successful, 
# the program will sort entries by most recently changed
# and suggest a copy command for you to cut-and-paste if 
# correct.
# Usage: perl FindOSGData.pl 
# To suppress list of all possible matches use:
# perl FindOSGData.pl --single 
# Special: To find LICENCE_GDAL.rtf use:
# perl FindOSGData.pl [--single] LICENSE_GDAL.rtf

use strict;
use warnings;

my $SHOULD_ONLY_PRINT_SUGGESTION = 0;
my $MDFIND_SEARCH_CRITERIA = "kMDItemDisplayName == 'OpenSceneGraph-Data'w && kMDItemKind=Folder";
my $SUGGESTED_COPY_TO_PATH = "PackageDir/Resources";
my $AM_COPYING_DIR = 1;

# Quick and dirty extract file options
if(scalar(@ARGV))
{
	foreach my $item(@ARGV)
	{
		if($item eq "--single")
		{
			$SHOULD_ONLY_PRINT_SUGGESTION = 1;
		}
		elsif($item eq "LICENSE_GDAL.rtf")
		{
			$MDFIND_SEARCH_CRITERIA = "kMDItemDisplayName == 'LICENSE_GDAL.rtf' && kMDItemKind='Rich Text Format (RTF) document'";
			$SUGGESTED_COPY_TO_PATH = "PackageDir";
			$AM_COPYING_DIR = 0;
		}
		else
		{
			print("Unknown argument: $item\n");
		}
	}

}
		

sub main()
{
	# Call mdfind and return the list of files to an array
	print("Calling mdfind (Spotlight)...\n");

	my @filelist = `/usr/bin/mdfind "$MDFIND_SEARCH_CRITERIA"` or die "Couldn't find anything that matched criteria on your system using Spotlight\n";
	my %folder_lastused_map;
	
	#print(@filelist);
	
	foreach my $file(@filelist)
	{
		my $escaped_string = $file;
		# Need to escape all the spaces in the file name
		# (and kill trailing newline if there)
		$escaped_string =~ s/ /\\ /g;
		chomp($escaped_string);

		# Call mdls on each file to get the last changed date
		my $ret_string = `/usr/bin/mdls -name kMDItemFSContentChangeDate $escaped_string`;
		if( $ret_string =~ m/^.*?\nkMDItem.*?=\s+(.*)/ )
		{
			# extract the date string (and kill trailing newline if there)
			my $date_string = $1;
			chomp($date_string);

			#print("Date string: $date_string\n");
			# copy the date string to the map
			$folder_lastused_map{$escaped_string} = $date_string;
		}
		else
		{
			print "Ooops, no match...mdls format may have changed";
		}
	}

	my @sorted_by_most_recently_changed = sort {$folder_lastused_map{$b} cmp $folder_lastused_map{$a}} keys(%folder_lastused_map);
	
	if(0 == scalar(@sorted_by_most_recently_changed))
	{
		print("No matches for OpenSceneGraph-Data were found.");
		exit;
	}

	if(not $SHOULD_ONLY_PRINT_SUGGESTION)
	{
		print("This is the list of possible matches sorted by most recently changed:\n");
		foreach my $file(@sorted_by_most_recently_changed)
		{
			print("$file\n");
		}
	}

	print("\nIf the following file is correct, you may want to copy and paste this line:\n");

	my $item = $sorted_by_most_recently_changed[0];
	# need to escape string
	$item =~ s/ /\\ /g;

	if($AM_COPYING_DIR == 1)
	{
		print("/Developer/Tools/CpMac -r $item/* $SUGGESTED_COPY_TO_PATH\n");
	}
	else
	{
		print("/Developer/Tools/CpMac -r $item $SUGGESTED_COPY_TO_PATH\n");
	}
}

main()