misc-scripts/doasedit

33 lines
867 B
Bash
Executable File

#!/bin/sh
set -f
errecho() { echo "$*" >&2 ; }
error() { errecho "$*" && exit 1 ; }
TMPDIR="${TMPDIR:-/tmp}"
ed="${VISUAL:-${EDITOR:-vi}}"
clean() {
[ -f "${tmpfile}" ] && rm -f "$tmpfile"
}
trap 'clean' 1 INT HUP QUIT EXIT
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; }
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'"
$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"
elif [ $ec -eq 0 ]; then
errecho "File not changed, skipping..."
else
errecho "Problem running diff on $f! Skipping..."
fi
rm -f "$tmpfile"
done