2 eval '(exit $?0)' && eval 'exec perl -w -S $0 ${1+"$@"}'
3 & eval 'exec perl -w -S $0 $argv:q'
6 # ******************************************************************
8 # Create Date: 3/09/2004
9 # ******************************************************************
11 # ******************************************************************
13 # ******************************************************************
22 # ******************************************************************
24 # ******************************************************************
28 my %defaults = ('header_files' => 1,
32 'template_files' => 1,
36 my %special = ('exe_output' => 1,
46 eval 'symlink("", "");';
47 my $hasSymlink = ($@
eq '');
49 # ******************************************************************
51 # ******************************************************************
55 my @parts = split(/[\/\\]/, $path);
57 ## Split the path into parts and check for '..'. If we find one
58 ## and the previous entry wasn't one, then we can remove them both.
59 for(my $i = 0; $i <= $#parts; $i++) {
60 if ($i > 0 && $parts[$i] eq '..' && $parts[$i - 1] ne '..') {
61 splice(@parts, $i - 1, 2);
65 return join('/', @parts);
69 my($file, $insdir) = @_;
70 return rm_updirs
($insdir . '/' .
71 (defined $actual{$file}
72 ?
"$actual{$file}/" . basename
($file)
77 my($files, $insdir, $symlink, $verbose) = @_;
78 my $type = ($symlink ?
'link' : 'copy');
81 # If > 1 file in @$files is a symlink to the same target, create one of the
82 # files (the shorter-named one) as a symlink in the destination tree.
83 my %localLink; # keys: files to create as symlinks in the destination
84 # values: symlink target (used in the main loop, below)
85 my %linkTargets; # keys: link targets in the source tree
86 # values: name of the symlink in the source tree
87 foreach my $file (@
$files) {
89 my $dest = readlink($file);
90 if (exists $linkTargets{$dest} && $file ne $linkTargets{$dest}) {
91 if (length $file <= length $linkTargets{$dest}) {
92 $localLink{$file} = $linkTargets{$dest};
95 $localLink{$linkTargets{$dest}} = $file;
96 $linkTargets{$dest} = $file;
100 $linkTargets{$dest} = $file;
105 foreach my $file (@
$files) {
106 my $dest = get_dest
($file, $insdir);
107 my $fulldir = dirname
($dest);
110 foreach my $part (split(/[\/\\]/, $fulldir)) {
116 if (! -e
$dest || (-M
$file) < (-M
$dest)) {
118 print '', ($symlink ?
'Linking' : 'Copying'), " to $dest\n";
123 $status = symlink("$cwd/$file", $dest);
125 elsif (exists $localLink{$file}) {
126 my $target = get_dest
($localLink{$file}, $insdir);
127 if ($fulldir eq dirname
($target)) {
128 $target = basename
($target);
131 print "\tCreating local symlink to $target\n";
134 $status = symlink($target, $dest);
137 $status = copy
($file, $dest);
138 chmod(0755, $dest) if ($status && -x
$file);
141 print STDERR
"ERROR: Unable to $type $file to $dest\n";
148 print "Skipping $file\n" if ($verbose);
155 sub determineSpecialName
{
156 my($tag, $dir, $info) = @_;
158 my($insdir, $name) = split(/\s+/, $info);
167 my $odir = ($insdir =~ /^\// ?
$insdir : ($dir . $insdir)) . $binarydir;
168 $odir = '.' if $odir eq '';
169 if ($tag eq 'exe_output') {
171 my $fh = new FileHandle
();
172 if (opendir($fh, $odir)) {
173 foreach my $file (grep(!/^\.\.?$/, readdir($fh))) {
174 if ($file =~ /^$name$/ ||
175 $file =~ /^$name.*\.exe$/i) {
176 push(@exes, "$dir$insdir$binarydir$file");
183 elsif ($tag eq 'lib_output') {
185 my $fh = new FileHandle
();
186 if (opendir($fh, $odir)) {
187 foreach my $file (grep(!/^\.\.?$/, readdir($fh))) {
188 if ($file =~ /^lib$name(-[0-9]+(\.[0-9]+)*)?\.(a|so|sl|dylib)/ ||
189 $file =~ /^(lib)?$name.*\.(dll|lib)$/i) {
190 push(@libs, "$dir$insdir$binarydir$file");
202 sub replaceVariables
{
204 while($line =~ /(\$\(([^)]+)\))/) {
207 my $val = (defined $ENV{$name} ?
$ENV{$name} : '');
208 $line =~ s/\$\([^)]+\)/$val/;
215 my($files, $tags, $verbose) = @_;
216 my $fh = new FileHandle
();
219 foreach my $file (@
$files) {
220 if (open($fh, $file)) {
222 print "Loading $file\n";
224 my $base = dirname
($file);
229 $base =~ s/^\.[\/\\]+//;
240 if ($line =~ /^(\w+):$/) {
241 if (defined $$tags{$1}) {
248 elsif (defined $current) {
249 $line = replaceVariables
($line);
250 my $perFileOverride = undef;
251 my $start = $#copy + 1;
252 if (defined $special{$current}) {
253 push(@copy, determineSpecialName
($current, $base, $line));
257 if ($line =~ /^\"([^"]+)\" (.*)/ || $line =~ /^(\S+) (\S+)/) {
258 ($src, $dst) = ($1, $2);
261 ($src, $dst) = ($line, '');
263 if (defined $extended{$current}) {
264 push(@copy, "$base$src");
265 $perFileOverride = $dst;
268 push(@copy, "$base$src");
273 my $replace = $perFileOverride
274 ?
($perFileOverride . '/' . basename
($dir)) : '';
277 if (!(-d
|| /\.svn\// || /~$/)) {
280 $rel =~ s/^$dir/$replace/;
282 (defined $override{$current} ?
$override{$current}
283 : $base{$current}) . '/' . dirname
($rel);
288 elsif (defined $override{$current}) {
289 for(my $i = $start; $i <= $#copy; ++$i) {
290 $actual{$copy[$i]} = $override{$current};
293 elsif (defined $base{$current}) {
294 for(my $i = $start; $i <= $#copy; ++$i) {
295 $actual{$copy[$i]} = $base{$current} . '/' .
296 ($perFileOverride ?
$perFileOverride : dirname
($copy[$i]));
305 print STDERR
"Unable to open $file\n";
319 my $fh = new FileHandle
();
320 if (opendir($fh, $file)) {
321 foreach my $f (grep(!/^\.\.?$/, readdir($fh))) {
322 push(@files, getInsFiles
("$file/$f"));
327 elsif ($file =~ /\.$insext$/) {
337 print STDERR
"$msg\n" if (defined $msg);
339 my $base = basename
($0);
340 my $spc = ' ' x
(length($base) + 8);
341 print STDERR
"$base v$version\n",
342 "Usage: $base [-a tag1[,tagN]] [-b tag=dir] [-d dir] ",
343 ($hasSymlink ?
'[-l] ' : ''), "[-o tag=dir]\n",
344 $spc, "[-s tag1[,tagN]] [-x tag1[,tagN]] [-v] [-k] [-i]\n",
345 $spc, "[install directory] [$insext files or directories]\n\n",
346 "Install files matching the tag specifications found ",
347 "in $insext files.\n\n",
348 "-a Adds to the default set of tags that get copied.\n",
349 "-b Install tag into dir underneath the install directory.\n",
350 "-d Libs/executables are copied from this sub-directory.\n",
351 "-i Read standard input in place of the $insext file.\n",
352 "-k Keep going if a file to be copied is missing.\n",
353 ($hasSymlink ?
"-l Use symbolic links instead of copying.\n" : ''),
354 "-o Install tag into dir.\n",
355 "-s Sets the tags that get copied.\n",
356 "-v Enables verbose mode.\n",
357 "-x Enable extended behavior for the given tags:\n",
358 " These tags will use the 'gendir' setting from the\n",
359 " project as a relative target directory.\n",
361 "The default set of tags are:\n";
363 foreach my $key (sort keys %defaults) {
364 print STDERR
'', ($first ?
'' : ', '), $key;
372 # ******************************************************************
374 # ******************************************************************
381 my %tags = %defaults;
383 for(my $i = 0; $i <= $#ARGV; ++$i) {
388 if (defined $ARGV[$i]) {
389 foreach my $tag (split(',', $ARGV[$i])) {
394 usageAndExit
('-a requires a parameter.');
397 elsif ($arg eq '-b') {
399 if (defined $ARGV[$i]) {
400 if ($ARGV[$i] =~ /([^=]+)=(.*)/) {
404 usageAndExit
("Invalid parameter to -b: $ARGV[$i]");
408 usageAndExit
('-b requires a parameter.');
411 elsif ($arg eq '-d') {
413 if (defined $ARGV[$i]) {
414 $binarydir = $ARGV[$i];
415 $binarydir =~ s/\\/\//g
;
416 $binarydir .= '/' unless $binarydir =~ /\
/$/;
419 usageAndExit
('-d requires a parameter.');
422 elsif ($arg eq '-i') {
423 push(@insfiles, '-');
425 elsif ($arg eq '-k') {
428 elsif ($arg eq '-l') {
429 $symlink = $hasSymlink;
431 elsif ($arg eq '-o') {
433 if (defined $ARGV[$i]) {
434 if ($ARGV[$i] =~ /([^=]+)=(.*)/) {
438 usageAndExit
("Invalid parameter to -o: $ARGV[$i]");
442 usageAndExit
('-o requires a parameter.');
445 elsif ($arg eq '-s') {
447 if (defined $ARGV[$i]) {
449 foreach my $tag (split(',', $ARGV[$i])) {
454 usageAndExit
('-s requires a parameter.');
457 elsif ($arg eq '-v') {
460 elsif ($arg eq '-x') {
462 if (defined $ARGV[$i]) {
463 foreach my $tag (split(',', $ARGV[$i])) {
468 usageAndExit
('-x requires a parameter.');
472 usageAndExit
('Unknown option: ' . $arg);
475 elsif (!defined $insdir) {
483 print "Collecting $insext files...\n";
487 push(@insfiles, getInsFiles
($arg));
491 if (!defined $insdir) {
494 elsif (!defined $insfiles[0]) {
495 print "No $insext files were found.\n";
500 my @files = loadInsFiles
(\
@insfiles, \
%tags, $verbose);
501 if (defined $files[0]) {
502 $status = (copyFiles
(\
@files, $insdir, $symlink, $verbose) ?
0 : 1);