misc-scripts/doasedit

33 lines
867 B
Plaintext
Raw Permalink Normal View History

2023-05-03 17:29:22 -05:00
#!/bin/sh
set -f
errecho() { echo "$*" >&2 ; }
error() { errecho "$*" && exit 1 ; }
2023-05-03 17:29:22 -05:00
TMPDIR="${TMPDIR:-/tmp}"
ed="${VISUAL:-${EDITOR:-vi}}"
2023-05-03 17:29:22 -05:00
clean() {
[ -f "${tmpfile}" ] && rm -f "$tmpfile"
2023-05-03 17:29:22 -05:00
}
2023-12-14 23:06:26 -06:00
trap 'clean' 1 INT HUP QUIT EXIT
2023-05-03 17:29:22 -05:00
for f; do
[ -f "$f" ] || { errecho "File $f is not a regular file, is not accessible by the user, or does not exist. Skipping..." && continue; }
2023-05-03 17:29:22 -05:00
tmpfile="$(mktemp -t doasedit_XXXXXXXX)" || error "Cannot make temp file for $f! Exiting..."
cp -f "$f" "$tmpfile" || error "Cannot copy file $f! Exiting..."
errecho "$ed '$f'"
2023-05-03 17:29:22 -05:00
$ed "$tmpfile" || error "Exit code != 0 by editor. Exiting..."
cmp "$f" "$tmpfile" >/dev/null 2>&1
ec=$?
if [ $ec -eq 1 ]; then
doas cp "$tmpfile" "$f"
2023-05-03 17:29:22 -05:00
elif [ $ec -eq 0 ]; then
errecho "File not changed, skipping..."
2023-05-03 17:29:22 -05:00
else
errecho "Problem running diff on $f! Skipping..."
2023-05-03 17:29:22 -05:00
fi
rm -f "$tmpfile"
done