1#!/bin/sh
2# Get modification time of a file or directory and pretty-print it.
3
4scriptversion=2007-03-30.02
5
6# Copyright (C) 1995, 1996, 1997, 2003, 2004, 2005, 2007 Free Software
7# Foundation, Inc.
8# written by Ulrich Drepper <drepper@gnu.ai.mit.edu>, June 1995
9#
10# This program is free software; you can redistribute it and/or modify
11# it under the terms of the GNU General Public License as published by
12# the Free Software Foundation; either version 2, or (at your option)
13# any later version.
14#
15# This program is distributed in the hope that it will be useful,
16# but WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18# GNU General Public License for more details.
19#
20# You should have received a copy of the GNU General Public License
21# along with this program; if not, write to the Free Software Foundation,
22# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23
24# As a special exception to the GNU General Public License, if you
25# distribute this file as part of a program that contains a
26# configuration script generated by Autoconf, you may include it under
27# the same distribution terms that you use for the rest of that program.
28
29# This file is maintained in Automake, please report
30# bugs to <bug-automake@gnu.org> or send patches to
31# <automake-patches@gnu.org>.
32
33case $1 in
34  '')
35     echo "$0: No file.  Try \`$0 --help' for more information." 1>&2
36     exit 1;
37     ;;
38  -h | --h*)
39    cat <<\EOF
40Usage: mdate-sh [--help] [--version] FILE
41
42Pretty-print the modification time of FILE.
43
44Report bugs to <bug-automake@gnu.org>.
45EOF
46    exit $?
47    ;;
48  -v | --v*)
49    echo "mdate-sh $scriptversion"
50    exit $?
51    ;;
52esac
53
54# Prevent date giving response in another language.
55LANG=C
56export LANG
57LC_ALL=C
58export LC_ALL
59LC_TIME=C
60export LC_TIME
61
62# GNU ls changes its time format in response to the TIME_STYLE
63# variable.  Since we cannot assume `unset' works, revert this
64# variable to its documented default.
65if test "${TIME_STYLE+set}" = set; then
66  TIME_STYLE=posix-long-iso
67  export TIME_STYLE
68fi
69
70save_arg1=$1
71
72# Find out how to get the extended ls output of a file or directory.
73if ls -L /dev/null 1>/dev/null 2>&1; then
74  ls_command='ls -L -l -d'
75else
76  ls_command='ls -l -d'
77fi
78# Avoid user/group names that might have spaces, when possible.
79if ls -n /dev/null 1>/dev/null 2>&1; then
80  ls_command="$ls_command -n"
81fi
82
83# A `ls -l' line looks as follows on OS/2.
84#  drwxrwx---        0 Aug 11  2001 foo
85# This differs from Unix, which adds ownership information.
86#  drwxrwx---   2 root  root      4096 Aug 11  2001 foo
87#
88# To find the date, we split the line on spaces and iterate on words
89# until we find a month.  This cannot work with files whose owner is a
90# user named `Jan', or `Feb', etc.  However, it's unlikely that `/'
91# will be owned by a user whose name is a month.  So we first look at
92# the extended ls output of the root directory to decide how many
93# words should be skipped to get the date.
94
95# On HPUX /bin/sh, "set" interprets "-rw-r--r--" as options, so the "x" below.
96set x`$ls_command /`
97
98# Find which argument is the month.
99month=
100command=
101until test $month
102do
103  shift
104  # Add another shift to the command.
105  command="$command shift;"
106  case $1 in
107    Jan) month=January; nummonth=1;;
108    Feb) month=February; nummonth=2;;
109    Mar) month=March; nummonth=3;;
110    Apr) month=April; nummonth=4;;
111    May) month=May; nummonth=5;;
112    Jun) month=June; nummonth=6;;
113    Jul) month=July; nummonth=7;;
114    Aug) month=August; nummonth=8;;
115    Sep) month=September; nummonth=9;;
116    Oct) month=October; nummonth=10;;
117    Nov) month=November; nummonth=11;;
118    Dec) month=December; nummonth=12;;
119  esac
120done
121
122# Get the extended ls output of the file or directory.
123set dummy x`eval "$ls_command \"\$save_arg1\""`
124
125# Remove all preceding arguments
126eval $command
127
128# Because of the dummy argument above, month is in $2.
129#
130# On a POSIX system, we should have
131#
132# $# = 5
133# $1 = file size
134# $2 = month
135# $3 = day
136# $4 = year or time
137# $5 = filename
138#
139# On Darwin 7.7.0 and 7.6.0, we have
140#
141# $# = 4
142# $1 = day
143# $2 = month
144# $3 = year or time
145# $4 = filename
146
147# Get the month.
148case $2 in
149  Jan) month=January; nummonth=1;;
150  Feb) month=February; nummonth=2;;
151  Mar) month=March; nummonth=3;;
152  Apr) month=April; nummonth=4;;
153  May) month=May; nummonth=5;;
154  Jun) month=June; nummonth=6;;
155  Jul) month=July; nummonth=7;;
156  Aug) month=August; nummonth=8;;
157  Sep) month=September; nummonth=9;;
158  Oct) month=October; nummonth=10;;
159  Nov) month=November; nummonth=11;;
160  Dec) month=December; nummonth=12;;
161esac
162
163case $3 in
164  ???*) day=$1;;
165  *) day=$3; shift;;
166esac
167
168# Here we have to deal with the problem that the ls output gives either
169# the time of day or the year.
170case $3 in
171  *:*) set `date`; eval year=\$$#
172       case $2 in
173	 Jan) nummonthtod=1;;
174	 Feb) nummonthtod=2;;
175	 Mar) nummonthtod=3;;
176	 Apr) nummonthtod=4;;
177	 May) nummonthtod=5;;
178	 Jun) nummonthtod=6;;
179	 Jul) nummonthtod=7;;
180	 Aug) nummonthtod=8;;
181	 Sep) nummonthtod=9;;
182	 Oct) nummonthtod=10;;
183	 Nov) nummonthtod=11;;
184	 Dec) nummonthtod=12;;
185       esac
186       # For the first six month of the year the time notation can also
187       # be used for files modified in the last year.
188       if (expr $nummonth \> $nummonthtod) > /dev/null;
189       then
190	 year=`expr $year - 1`
191       fi;;
192  *) year=$3;;
193esac
194
195# The result.
196echo $day $month $year
197
198# Local Variables:
199# mode: shell-script
200# sh-indentation: 2
201# eval: (add-hook 'write-file-hooks 'time-stamp)
202# time-stamp-start: "scriptversion="
203# time-stamp-format: "%:y-%02m-%02d.%02H"
204# time-stamp-end: "$"
205# End:
206