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