1#!/bin/sh
2# usage: ifchange target temporary
3
4timestamp=
5until [ "$0" = 0 ]; do
6    case "$1" in
7	--timestamp)
8	    timestamp=.
9	    ;;
10	--timestamp=*)
11	    timestamp=`expr \( "$1" : '[^=]*=\(.*\)' \)`
12	    ;;
13	*)
14	    break
15	    ;;
16    esac
17    shift
18done
19
20target="$1"
21temp="$2"
22if [ "$temp" = - ]; then
23    temp="tmpdata$$.tmp~"
24    cat > "$temp" || exit $?
25    trap 'rm -f "$temp"' 0
26fi
27
28if cmp "$target" "$temp" >/dev/null 2>&1; then
29    echo "$target unchanged"
30    rm -f "$temp"
31else
32    echo "$target updated"
33    mv -f "$temp" "$target"
34fi
35
36if [ -n "${timestamp}" ]; then
37    if [ x"${timestamp}" = x. ]; then
38	case "$target" in
39	    */*)
40		timestamp=`dirname "$target"`/.time.`basename "$target"`
41		;;
42	    *)
43		timestamp=.time."$target"
44		;;
45	esac
46    fi
47    : > "$timestamp"
48fi
49