[rubygems/rubygems] Use a constant empty tar header to avoid extra allocations
[ruby.git] / tool / ifchange
blob5af41e01566f1ef50efea4d8c55639c9f021f66c
1 #!/bin/sh
2 # usage: ifchange target temporary
4 # Used in generating revision.h via Makefiles.
6 help() {
7 cat <<HELP
8 usage: $0 [options] target new-file
9 options:
10 --timestamp[=file] touch timestamp file. (default: prefixed with ".time".
11 under the directory of the target)
12 --keep[=suffix] keep old file with suffix. (default: '.old')
13 --empty assume unchanged if the new file is empty.
14 --color[=always|auto|never] colorize output.
15 HELP
18 set -e
19 timestamp=
20 keepsuffix=
21 empty=
22 color=auto
23 until [ $# -eq 0 ]; do
24 case "$1" in
25 --)
26 shift
27 break;
29 --timestamp)
30 timestamp=.
32 --timestamp=*)
33 timestamp=`expr \( "$1" : '[^=]*=\(.*\)' \)`
35 --keep)
36 keepsuffix=.old
38 --keep=*)
39 keepsuffix=`expr \( "$1" : '[^=]*=\(.*\)' \)`
41 --empty)
42 empty=yes
44 --color)
45 color=always
47 --color=*)
48 color=`expr \( "$1" : '[^=]*=\(.*\)' \)`
50 --debug)
51 set -x
53 --help)
54 help
55 exit
57 --*)
58 echo "$0: unknown option: $1" 1>&2
59 exit 1
62 break
64 esac
65 shift
66 done
68 if [ "$#" != 2 ]; then
69 help
70 exit 1
73 target="$1"
74 temp="$2"
75 if [ "$temp" = - ]; then
76 temp="tmpdata$$.tmp~"
77 cat > "$temp"
78 trap 'rm -f "$temp"' 0
81 msg_begin= msg_unchanged= msg_updated= msg_reset=
82 if [ "$color" = always -o \( "$color" = auto -a -t 1 \) ]; then
83 msg_begin="\e["
84 case "`tput smso 2>/dev/null`" in
85 "$msg_begin"*m)
86 if [ ${TEST_COLORS:+set} ]; then
87 msg_unchanged=`expr ":$TEST_COLORS:" : ".*:pass=\([^:]*\):"` || :
88 msg_updated=`expr ":$TEST_COLORS:" : ".*:fail=\([^:]*\):"` || :
90 msg_unchanged="${msg_begin}${msg_unchanged:-32}m"
91 msg_updated="${msg_begin}${msg_updated:-31;1}m"
92 msg_reset="${msg_begin}m"
94 esac
95 unset msg_begin
98 targetdir=
99 case "$target" in */*) targetdir=`dirname "$target"`;; esac
100 if [ -f "$target" -a ! -${empty:+f}${empty:-s} "$temp" ] || cmp "$target" "$temp" >/dev/null 2>&1; then
101 echo "$target ${msg_unchanged}unchanged${msg_reset}"
102 rm -f "$temp"
103 else
104 echo "$target ${msg_updated}updated${msg_reset}"
105 [ x"${targetdir}" = x -o -d "${targetdir}" ] || mkdir -p "${targetdir}"
106 [ x"${keepsuffix}" != x -a -f "$target" ] && mv -f "$target" "${target}${keepsuffix}"
107 mv -f "$temp" "$target"
110 if [ -n "${timestamp}" ]; then
111 if [ x"${timestamp}" = x. ]; then
112 if [ x"$targetdir" = x ]; then
113 timestamp=.time."$target"
114 else
115 timestamp="$targetdir"/.time.`basename "$target"`
118 : > "$timestamp"