credit where credit is due
[openwrt-10.03/.git] / scripts / timestamp.pl
1 #!/usr/bin/perl
2
3 # Copyright (C) 2006 OpenWrt.org
4 #
5 # This is free software, licensed under the GNU General Public License v2.
6 # See /LICENSE for more information.
7 #
8
9 use strict;
10
11 sub get_ts($$) {
12         my $path = shift;
13         my $options = shift;
14         my $ts = 0;
15         my $fn = "";
16         open FIND, "find $path -not -path \\*.svn\\* -and -not -path \\*CVS\\* $options 2>/dev/null |";
17         while (<FIND>) {
18                 chomp;
19                 my $file = $_;
20                 open FILE, "<$file";
21                 my @stat = stat FILE;
22                 close FILE;
23                 if ($stat[9] > $ts) {
24                         $ts = $stat[9];
25                         $fn = $file;
26                 }
27         }
28         close FIND;
29         return ($ts, $fn);
30 }
31
32 (@ARGV > 0) or push @ARGV, ".";
33 my $ts = 0;
34 my $n = ".";
35 my %options;
36 while (@ARGV > 0) {
37         my $path = shift @ARGV;
38         if ($path =~ /^-x/) {
39                 my $str = shift @ARGV;
40                 $options{"-x"} .= " -and -not -path \\*".$str."\\*"
41         } elsif ($path =~ /^-/) {
42                 $options{$path} = 1;
43         } else {
44                 my ($tmp, $fname) = get_ts($path, $options{"-x"});
45                 if ($tmp > $ts) {
46                         if ($options{'-f'}) {
47                                 $n = $fname;
48                         } else {
49                                 $n = $path;
50                         }
51                         $ts = $tmp;
52                 }
53         }
54 }
55
56 if ($options{"-p"}) {
57         print "$n\n";
58 } elsif ($options{"-t"}) {
59         print "$ts\n";
60 } else {
61         print "$n\t$ts\n";
62 }