summaryrefslogtreecommitdiff
path: root/tool/ifchange
blob: ca2ea40644d2f597d05e9b9bb4270d5d4961cf1c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/bin/sh
# usage: ifchange target temporary

timestamp=
until [ "$0" = 0 ]; do
    case "$1" in
	--timestamp)
	    timestamp=.
	    ;;
	--timestamp=*)
	    timestamp="${1#*=}" || timestamp=`expr "$1" : '[^=]*=\(.*\)'`
	    ;;
	*)
	    break
	    ;;
    esac
    shift
done

target="$1"
temp="$2"
if [ "$temp" = - ]; then
    temp="tmpdata$$.tmp~"
    cat > "$temp" || exit $?
    trap 'rm -f "$temp"' 0
fi

if cmp "$target" "$temp" >/dev/null 2>&1; then
    echo "$target unchanged"
    rm -f "$temp"
else
    echo "$target updated"
    mv -f "$temp" "$target"
fi

if [ -n "${timestamp}" ]; then
    if [ x"${timestamp}" = x. ]; then
	case "$target" in
	    */*)
		timestamp="${target%/*}"/.time."${target##*/}" ||
		timestamp=`dirname "$target"`/.time.`basename "$target"`
		;;
	    *)
		timestamp=.time."$target"
		;;
	esac
    fi
    : > "$timestamp"
fi