joeylib2/make/support/dvx/writeIfChanged.sh
2026-09-29 19:31:34 -05:00

24 lines
561 B
Bash
Executable file

#!/usr/bin/env bash
# Write standard input to FILE only when it differs from what FILE holds, so
# make/dvx.mk can regenerate a program's SDK project files on every build
# without touching their times (sdk.mk would otherwise rebuild the help and
# the resources each time).
#
# Usage: writeIfChanged.sh FILE
set -euo pipefail
if [[ $# -ne 1 ]]; then
echo "usage: $(basename "$0") FILE" >&2
exit 2
fi
file="$1"
tmp="$(mktemp "$file.XXXXXX")"
cat > "$tmp"
chmod 644 "$tmp"
if cmp -s "$tmp" "$file"; then
rm -f "$tmp"
else
mv "$tmp" "$file"
fi