summaryrefslogtreecommitdiff
path: root/tool/ifchange
blob: 5af41e01566f1ef50efea4d8c55639c9f021f66c (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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/bin/sh
# usage: ifchange target temporary

# Used in generating revision.h via Makefiles.

help() {
    cat <<HELP
usage: $0 [options] target new-file
options:
  --timestamp[=file] touch timestamp file. (default: prefixed with ".time".
                     under the directory of the target)
  --keep[=suffix]    keep old file with suffix. (default: '.old')
  --empty            assume unchanged if the new file is empty.
  --color[=always|auto|never] colorize output.
HELP
}

set -e
timestamp=
keepsuffix=
empty=
color=auto
until [ $# -eq 0 ]; do
    case "$1" in
	--)
	    shift
	    break;
	    ;;
	--timestamp)
	    timestamp=.
	    ;;
	--timestamp=*)
	    timestamp=`expr \( "$1" : '[^=]*=\(.*\)' \)`
	    ;;
	--keep)
	    keepsuffix=.old
	    ;;
	--keep=*)
	    keepsuffix=`expr \( "$1" : '[^=]*=\(.*\)' \)`
	    ;;
	--empty)
	    empty=yes
	    ;;
	--color)
	    color=always
	    ;;
	--color=*)
	    color=`expr \( "$1" : '[^=]*=\(.*\)' \)`
	    ;;
	--debug)
	    set -x
	    ;;
	--help)
	    help
	    exit
	    ;;
	--*)
	    echo "$0: unknown option: $1" 1>&2
	    exit 1
	    ;;
	*)
	    break
	    ;;
    esac
    shift
done

if [ "$#" != 2 ]; then
    help
    exit 1
fi

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

msg_begin= msg_unchanged= msg_updated= msg_reset=
if [ "$color" = always -o \( "$color" = auto -a -t 1 \) ]; then
    msg_begin="["
    case "`tput smso 2>/dev/null`" in
	"$msg_begin"*m)
	    if [ ${TEST_COLORS:+set} ]; then
		msg_unchanged=`expr ":$TEST_COLORS:" : ".*:pass=\([^:]*\):"` || :
		msg_updated=`expr ":$TEST_COLORS:" : ".*:fail=\([^:]*\):"` || :
	    fi
	    msg_unchanged="${msg_begin}${msg_unchanged:-32}m"
	    msg_updated="${msg_begin}${msg_updated:-31;1}m"
	    msg_reset="${msg_begin}m"
	    ;;
    esac
    unset msg_begin
fi

targetdir=
case "$target" in */*) targetdir=`dirname "$target"`;; esac
if [ -f "$target" -a ! -${empty:+f}${empty:-s} "$temp" ] || cmp "$target" "$temp" >/dev/null 2>&1; then
    echo "$target ${msg_unchanged}unchanged${msg_reset}"
    rm -f "$temp"
else
    echo "$target ${msg_updated}updated${msg_reset}"
    [ x"${targetdir}" = x -o -d "${targetdir}" ] || mkdir -p "${targetdir}"
    [ x"${keepsuffix}" != x -a -f "$target" ] && mv -f "$target" "${target}${keepsuffix}"
    mv -f "$temp" "$target"
fi

if [ -n "${timestamp}" ]; then
    if [ x"${timestamp}" = x. ]; then
	if [ x"$targetdir" = x ]; then
	    timestamp=.time."$target"
	else
	    timestamp="$targetdir"/.time.`basename "$target"`
	fi
    fi
    : > "$timestamp"
fi