1#!/usr/bin/env 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
9use strict;
10
11sub get_ts($$) {
12	my $path = shift;
13	my $options = shift;
14	my $ts = 0;
15	my $fn = "";
16	$path .= "/" if( -d $path);
17	open FIND, "find $path -type f -and -not -path \\*/.svn\\* -and -not -path \\*CVS\\* $options 2>/dev/null |";
18	while (<FIND>) {
19		chomp;
20		my $file = $_;
21		next if -l $file;
22		my $mt = (stat $file)[9];
23		if ($mt > $ts) {
24			$ts = $mt;
25			$fn = $file;
26		}
27	}
28	close FIND;
29	return ($ts, $fn);
30}
31
32(@ARGV > 0) or push @ARGV, ".";
33my $ts = 0;
34my $n = ".";
35my %options;
36while (@ARGV > 0) {
37	my $path = shift @ARGV;
38	if ($path =~ /^-x/) {
39		my $str = shift @ARGV;
40		$options{"findopts"} .= " -and -not -path '".$str."'"
41	} elsif ($path =~ /^-f/) {
42		$options{"findopts"} .= " -follow";
43	} elsif ($path =~ /^-n/) {
44		my $arg = $ARGV[0];
45		$options{$path} = $arg;
46	} elsif ($path =~ /^-/) {
47		$options{$path} = 1;
48	} else {
49		my ($tmp, $fname) = get_ts($path, $options{"findopts"});
50		if ($tmp > $ts) {
51			if ($options{'-F'}) {
52				$n = $fname;
53			} else {
54				$n = $path;
55			}
56			$ts = $tmp;
57		}
58	}
59}
60
61if ($options{"-n"}) {
62	exit ($n eq $options{"-n"} ? 0 : 1);
63} elsif ($options{"-p"}) {
64	print "$n\n";
65} elsif ($options{"-t"}) {
66	print "$ts\n";
67} else {
68	print "$n\t$ts\n";
69}
70