Commit | Line | Data |
---|---|---|
bcd49ef3 LW |
1 | #!/usr/bin/perl |
2 | use strict; | |
3 | use warnings; | |
4 | use Config::Simple; | |
5 | use Getopt::Long; | |
6 | ||
161504e3 | 7 | my ($help, $config_file, $component, $srcpath); |
bcd49ef3 LW |
8 | |
9 | GetOptions( | |
1c47dbca LW |
10 | 'help' => \$help, # show help message and exit |
11 | 'config=s' => \$config_file, # INI file for path mappings | |
12 | 'component=s' => \$component, # The component to grab the source paths for | |
161504e3 | 13 | 'srcpath=s' => \$srcpath # The source path to grap the destination path for |
bcd49ef3 LW |
14 | ); |
15 | ||
16 | if ($help) { | |
17 | print <<"HELP"; | |
18 | ||
19 | OPTIONS: | |
20 | --help | |
21 | Show help message and exit. | |
22 | --config | |
23 | Location of INI file for path mappings. | |
24 | --component | |
c6a6221d LW |
25 | The component of the INI file for which you want the paths. |
26 | If you do not specify a component value then all components | |
27 | (sections) in the INI file are returned. | |
bcd49ef3 LW |
28 | --srcpath |
29 | The source path to return the destination path for | |
30 | ||
31 | HELP | |
32 | exit; | |
33 | } | |
34 | ||
35 | # load config | |
36 | die "No config file specified\n" unless ($config_file); | |
37 | die "Config file does not exist\n" unless (-r $config_file and -s $config_file); | |
1c47dbca | 38 | |
bcd49ef3 LW |
39 | my $cfg = new Config::Simple($config_file); |
40 | ||
41 | if (!$component) { | |
42 | my @components = (); | |
43 | #pull out components from INI file | |
44 | open my $fh, '<', $config_file; | |
45 | while (<$fh>) { | |
46 | chomp; | |
47 | if ($_ =~ /^\[[^]]+\]$/) { | |
c6a6221d LW |
48 | $_ =~ s/^\[//; |
49 | $_ =~ s/\]$//; | |
50 | #we use newlines because spaces seem to be valid within an INI file section. | |
51 | #I am not sure if newlines are also valid because I could not find anything | |
52 | #defining the INI section syntax, but I imagine using a newline within an INI | |
53 | #file section declartion is not done very often if it is valid syntax. | |
54 | print "$_\n"; | |
bcd49ef3 LW |
55 | } |
56 | } | |
bcd49ef3 LW |
57 | } elsif (!$srcpath) { |
58 | foreach my $key (keys $cfg->get_block($component)) { | |
c6a6221d LW |
59 | #again we use newlines because spaces can be used within directory names. I belive |
60 | #it is possible to use a newline in a directory name, but someone would have to be | |
61 | #trying really hard to make things break if they started doing that with the Evergreen | |
62 | #source. | |
bcd49ef3 LW |
63 | print $key . "\n"; |
64 | } | |
65 | } else { | |
2385238b LW |
66 | while (!defined $cfg->get_block($component)->{$srcpath}) { |
67 | $srcpath =~ s/\/[^\/]*$//; | |
68 | if ($srcpath !~ '/') { | |
69 | last; | |
70 | } | |
71 | } | |
72 | ||
73 | if (defined $cfg->get_block($component)->{$srcpath}) { | |
74 | print $cfg->get_block($component)->{$srcpath}; | |
75 | } | |
bcd49ef3 LW |
76 | } |
77 |