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=
|
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
|
|
|
|
;;
|
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~"
|
|
|
|
cat > "$temp" || exit $?
|
|
|
|
trap 'rm -f "$temp"' 0
|
|
|
|
fi
|
2009-03-16 06:30:01 +03:00
|
|
|
|
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
|
2008-01-25 13:22:32 +03:00
|
|
|
echo "$target unchanged"
|
2008-01-29 11:05:26 +03:00
|
|
|
rm -f "$temp"
|
2008-01-25 13:22:32 +03:00
|
|
|
else
|
|
|
|
echo "$target updated"
|
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
|