joeylib2/make/support/dvx/writeIfChanged.sh
2026-10-13 14:01:46 -05:00

28 lines
713 B
Bash
Executable file

#!/usr/bin/env bash
# Part of JoeyLib, Copyright (c) 2026 Scott Duensing, under the MIT License.
# See LICENSE.md for license information.
# SPDX-License-Identifier: MIT
#
# 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