#!/bin/sh

# Like mv $1 $2, but if the files are the same, just delete $1.
# Status is 0 if $2 is changed, 1 otherwise.

set -e

usage ()
{
  cat <<EOF
usage: $0 [OPTION...] SOURCE DESTINATION

Rename SOURCE as DESTINATION, but preserve the timestamps of
DESTINATION if SOURCE and DESTINATION have equal contents.

Options:
  -c, --color       display the diffs in color if colordiff is available
  -h, --help        display this message and exit
  -I, --ignore-matching-lines REGEXP
                    ignore differences in lines matching REGEXP
  -s, --silent      do not report anything
  -v, --verbose     display the diffs
EOF
  exit 0
}

diff=diff
diffflags=-u
verbose=false

while test $# != 0
do
  case $1 in
    (-c|--color)
      # The Emacs shell and compilation-mode are really bad at
      # displaying colors.
      if (colordiff /dev/null /dev/null) >/dev/null 2>&1 \
          && test -z "$INSIDE_EMACS"; then
        diff=colordiff
      fi
      ;;

    (-h|--help)
      usage ;;

    (-I|--ignore-matching-lines)
      shift
      diffflags="$diffflags -I $1";;

    (-s|--silent)
      verbose=false;;

    (-v|--verbose)
      verbose=true;;

    (*)
      if test -z "$new"; then
        new=$1
      else
        old=$1
      fi
      ;;
  esac
  shift
done

if $verbose; then
  exec 5>&1
else
  exec 5>/dev/null
fi

if test -r "$old"; then
    if $diff $diffflags "$old" "$new" >/dev/null; then
        # New file is exactly the same as the old one.
        # Remove the new one.
        echo >&5 "$old is unchanged"
        rm -f "$new"
        exit 0
    elif $diff $diffflags "$old" "$new" >&5; then
        # The file might have actually changed, but changes that are
        # ignored.  In that case, we want the latest contents, but the
        # oldest time stamp.  Since mv preserves time stamps, just set the
        # time stamps of the new one to that of the old one.
        echo >&5 "$old is unchanged"
        touch -r "$old" "$new"
    fi
fi
# There are differences to install.
mv -f "$new" "$old"
