2008-01-25 05:22:32 -05:00
|
|
|
|
#!/bin/sh
|
|
|
|
|
# usage: ifchange target temporary
|
|
|
|
|
|
2013-06-13 08:30:24 -04:00
|
|
|
|
set -e
|
2009-03-15 23:30:01 -04:00
|
|
|
|
timestamp=
|
2013-05-15 03:51:32 -04:00
|
|
|
|
keepsuffix=
|
2013-11-03 07:40:24 -05:00
|
|
|
|
empty=
|
2015-10-22 10:02:38 -04:00
|
|
|
|
color=auto
|
2013-06-17 00:33:52 -04:00
|
|
|
|
until [ $# -eq 0 ]; do
|
2009-03-15 23:30:01 -04:00
|
|
|
|
case "$1" in
|
|
|
|
|
--timestamp)
|
|
|
|
|
timestamp=.
|
|
|
|
|
;;
|
|
|
|
|
--timestamp=*)
|
2009-05-04 22:33:02 -04:00
|
|
|
|
timestamp=`expr \( "$1" : '[^=]*=\(.*\)' \)`
|
2009-03-15 23:30:01 -04:00
|
|
|
|
;;
|
2013-05-15 03:51:32 -04:00
|
|
|
|
--keep)
|
|
|
|
|
keepsuffix=.old
|
|
|
|
|
;;
|
|
|
|
|
--keep=*)
|
|
|
|
|
keepsuffix=`expr \( "$1" : '[^=]*=\(.*\)' \)`
|
|
|
|
|
;;
|
2013-11-03 07:40:24 -05:00
|
|
|
|
--empty)
|
|
|
|
|
empty=yes
|
|
|
|
|
;;
|
2015-10-22 10:02:38 -04:00
|
|
|
|
--color)
|
|
|
|
|
color=always
|
|
|
|
|
;;
|
|
|
|
|
--color=*)
|
|
|
|
|
color=`expr \( "$1" : '[^=]*=\(.*\)' \)`
|
|
|
|
|
;;
|
2009-03-15 23:30:01 -04:00
|
|
|
|
*)
|
|
|
|
|
break
|
|
|
|
|
;;
|
|
|
|
|
esac
|
2008-10-17 06:46:23 -04:00
|
|
|
|
shift
|
2009-03-15 23:30:01 -04:00
|
|
|
|
done
|
|
|
|
|
|
2008-01-25 05:22:32 -05:00
|
|
|
|
target="$1"
|
|
|
|
|
temp="$2"
|
|
|
|
|
if [ "$temp" = - ]; then
|
|
|
|
|
temp="tmpdata$$.tmp~"
|
2016-04-20 23:14:05 -04:00
|
|
|
|
cat > "$temp"
|
2008-01-25 05:22:32 -05:00
|
|
|
|
trap 'rm -f "$temp"' 0
|
|
|
|
|
fi
|
2009-03-15 23:30:01 -04:00
|
|
|
|
|
2015-10-20 03:15:04 -04:00
|
|
|
|
msg_begin= msg_unchanged= msg_updated= msg_reset=
|
2015-10-22 10:02:38 -04:00
|
|
|
|
if [ "$color" = always -o \( "$color" = auto -a -t 1 \) ]; then
|
|
|
|
|
msg_begin="["
|
|
|
|
|
case "`tput smso 2>/dev/null`" in
|
2015-10-30 20:02:19 -04:00
|
|
|
|
"$msg_begin"*m)
|
2015-10-20 03:15:04 -04:00
|
|
|
|
if [ ${TEST_COLORS:+set} ]; then
|
2016-04-20 23:14:05 -04:00
|
|
|
|
msg_unchanged=`expr ":$TEST_COLORS:" : ".*:pass=\([^:]*\):"` || :
|
|
|
|
|
msg_updated=`expr ":$TEST_COLORS:" : ".*:fail=\([^:]*\):"` || :
|
2015-10-20 03:15:04 -04:00
|
|
|
|
fi
|
|
|
|
|
msg_unchanged="${msg_begin}${msg_unchanged:-32;1}m"
|
|
|
|
|
msg_updated="${msg_begin}${msg_updated:-31;1}m"
|
|
|
|
|
msg_reset="${msg_begin}m"
|
|
|
|
|
;;
|
|
|
|
|
esac
|
|
|
|
|
unset msg_begin
|
|
|
|
|
fi
|
|
|
|
|
|
2016-06-09 02:31:54 -04:00
|
|
|
|
targetdir=
|
|
|
|
|
case "$target" in */*) targetdir=`dirname "$target"`;; esac
|
2013-11-03 07:40:24 -05:00
|
|
|
|
if [ -f "$target" -a ! -${empty:+f}${empty:-s} "$temp" ] || cmp "$target" "$temp" >/dev/null 2>&1; then
|
2015-10-20 03:15:04 -04:00
|
|
|
|
echo "$target ${msg_unchanged}unchanged${msg_reset}"
|
2008-01-29 03:05:26 -05:00
|
|
|
|
rm -f "$temp"
|
2008-01-25 05:22:32 -05:00
|
|
|
|
else
|
2015-10-20 03:15:04 -04:00
|
|
|
|
echo "$target ${msg_updated}updated${msg_reset}"
|
2016-06-09 02:31:54 -04:00
|
|
|
|
[ x"${targetdir}" = x -o -d "${targetdir}" ] || mkdir -p "${targetdir}"
|
|
|
|
|
[ x"${keepsuffix}" != x -a -f "$target" ] && mv -f "$target" "${target}${keepsuffix}"
|
2008-01-25 05:22:32 -05:00
|
|
|
|
mv -f "$temp" "$target"
|
|
|
|
|
fi
|
2009-03-15 23:30:01 -04:00
|
|
|
|
|
|
|
|
|
if [ -n "${timestamp}" ]; then
|
|
|
|
|
if [ x"${timestamp}" = x. ]; then
|
2016-06-09 02:31:54 -04:00
|
|
|
|
if [ x"$targetdir" = x. ]; then
|
|
|
|
|
timestamp=.time."$target"
|
|
|
|
|
else
|
|
|
|
|
timestamp="$targetdir"/.time.`basename "$target"`
|
|
|
|
|
fi
|
2009-03-15 23:30:01 -04:00
|
|
|
|
fi
|
|
|
|
|
: > "$timestamp"
|
2008-10-17 06:46:23 -04:00
|
|
|
|
fi
|