#!/bin/sh
# Get modification time of a file or directory and pretty-print it.

scriptversion=2004-12-08.12

# Copyright (C) 1995, 1996, 1997, 2003, 2004 Free Software Foundation, Inc.
# written by Ulrich Drepper <[email protected]>, June 1995
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

# As a special exception to the GNU General Public License, if you
# distribute this file as part of a program that contains a
# configuration script generated by Autoconf, you may include it under
# the same distribution terms that you use for the rest of that program.

# This file is maintained in Automake, please report
# bugs to <[email protected]> or send patches to
# <[email protected]>.

case $1 in
 '')
    echo "$0: No file.  Try \`$0 --help' for more information." 1>&2
    exit 1;
    ;;
 -h | --h*)
   cat <<\EOF
Usage: mdate-sh [--help] [--version] FILE

Pretty-print the modification time of FILE.

Report bugs to <[email protected]>.
EOF
   exit 0
   ;;
 -v | --v*)
   echo "mdate-sh $scriptversion"
   exit 0
   ;;
esac

# Prevent date giving response in another language.
LANG=C
export LANG
LC_ALL=C
export LC_ALL
LC_TIME=C
export LC_TIME

save_arg1="$1"

# Find out how to get the extended ls output of a file or directory.
if ls -L /dev/null 1>/dev/null 2>&1; then
 ls_command='ls -L -l -d'
else
 ls_command='ls -l -d'
fi

# A `ls -l' line looks as follows on OS/2.
#  drwxrwx---        0 Aug 11  2001 foo
# This differs from Unix, which adds ownership information.
#  drwxrwx---   2 root  root      4096 Aug 11  2001 foo
#
# To find the date, we split the line on spaces and iterate on words
# until we find a month.  This cannot work with files whose owner is a
# user named `Jan', or `Feb', etc.  However, it's unlikely that `/'
# will be owned by a user whose name is a month.  So we first look at
# the extended ls output of the root directory to decide how many
# words should be skipped to get the date.

# On HPUX /bin/sh, "set" interprets "-rw-r--r--" as options, so the "x" below.
set x`ls -l -d /`

# Find which argument is the month.
month=
command=
until test $month
do
 shift
 # Add another shift to the command.
 command="$command shift;"
 case $1 in
   Jan) month=January; nummonth=1;;
   Feb) month=February; nummonth=2;;
   Mar) month=March; nummonth=3;;
   Apr) month=April; nummonth=4;;
   May) month=May; nummonth=5;;
   Jun) month=June; nummonth=6;;
   Jul) month=July; nummonth=7;;
   Aug) month=August; nummonth=8;;
   Sep) month=September; nummonth=9;;
   Oct) month=October; nummonth=10;;
   Nov) month=November; nummonth=11;;
   Dec) month=December; nummonth=12;;
 esac
done

# Get the extended ls output of the file or directory.
set x`eval "$ls_command \"\$save_arg1\""`

# Remove all preceding arguments
eval $command

# Get the month.  Next argument is day, followed by the year or time.
case $1 in
 Jan) month=January; nummonth=1;;
 Feb) month=February; nummonth=2;;
 Mar) month=March; nummonth=3;;
 Apr) month=April; nummonth=4;;
 May) month=May; nummonth=5;;
 Jun) month=June; nummonth=6;;
 Jul) month=July; nummonth=7;;
 Aug) month=August; nummonth=8;;
 Sep) month=September; nummonth=9;;
 Oct) month=October; nummonth=10;;
 Nov) month=November; nummonth=11;;
 Dec) month=December; nummonth=12;;
esac

day=$2

# Here we have to deal with the problem that the ls output gives either
# the time of day or the year.
case $3 in
 *:*) set `date`; eval year=\$$#
      case $2 in
        Jan) nummonthtod=1;;
        Feb) nummonthtod=2;;
        Mar) nummonthtod=3;;
        Apr) nummonthtod=4;;
        May) nummonthtod=5;;
        Jun) nummonthtod=6;;
        Jul) nummonthtod=7;;
        Aug) nummonthtod=8;;
        Sep) nummonthtod=9;;
        Oct) nummonthtod=10;;
        Nov) nummonthtod=11;;
        Dec) nummonthtod=12;;
      esac
      # For the first six month of the year the time notation can also
      # be used for files modified in the last year.
      if (expr $nummonth \> $nummonthtod) > /dev/null;
      then
        year=`expr $year - 1`
      fi;;
 *) year=$3;;
esac

# The result.
echo $day $month $year

# Local Variables:
# mode: shell-script
# sh-indentation: 2
# eval: (add-hook 'write-file-hooks 'time-stamp)
# time-stamp-start: "scriptversion="
# time-stamp-format: "%:y-%02m-%02d.%02H"
# time-stamp-end: "$"
# End: