2023-12-23 16:57:52 +00:00
|
|
|
#!/usr/bin/env bash
|
2021-02-20 22:35:06 +00:00
|
|
|
|
2021-07-09 22:50:17 +00:00
|
|
|
set -e
|
|
|
|
|
2023-12-16 19:38:07 +00:00
|
|
|
edit_dir_base="$XDG_RUNTIME_DIR/edit"
|
2021-02-20 22:35:06 +00:00
|
|
|
|
2023-06-02 23:29:58 +00:00
|
|
|
_pre_checks() {
|
|
|
|
if [ -z "$EDITOR" ]; then
|
|
|
|
echo "EDITOR is not set"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
}
|
2021-02-28 23:06:49 +00:00
|
|
|
|
2023-06-02 23:29:58 +00:00
|
|
|
_pre_checks_file() {
|
|
|
|
if [ -z "$1" ]; then
|
|
|
|
echo "No file specified"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
if sudo test -d "$1"; then
|
|
|
|
echo "Cannot edit directory"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
}
|
2021-03-03 17:22:02 +00:00
|
|
|
|
2023-11-09 02:18:12 +00:00
|
|
|
_readlink() {
|
|
|
|
sudo readlink -fn "$1"
|
|
|
|
}
|
|
|
|
|
2023-06-02 23:29:58 +00:00
|
|
|
_get_edit_directory() {
|
|
|
|
target_dir=$(dirname "$1")
|
2023-11-09 02:18:12 +00:00
|
|
|
edit_dir_sub=$(_readlink "$target_dir" | md5sum | awk '{ print $1 }')
|
2021-02-20 22:35:06 +00:00
|
|
|
|
2023-12-16 19:38:07 +00:00
|
|
|
edit_directory="$edit_dir_base/$edit_dir_sub"
|
2023-06-02 23:29:58 +00:00
|
|
|
echo "$edit_directory"
|
|
|
|
}
|
2021-02-20 22:35:06 +00:00
|
|
|
|
2023-06-02 23:29:58 +00:00
|
|
|
_get_edit_file() {
|
2023-11-09 02:18:12 +00:00
|
|
|
target=$(_readlink "$1")
|
2023-06-02 23:29:58 +00:00
|
|
|
target_name=$(basename "$target")
|
|
|
|
edit_directory=$(_get_edit_directory "$target")
|
2021-02-20 22:35:06 +00:00
|
|
|
|
2023-12-16 19:38:07 +00:00
|
|
|
edit_file="$edit_directory/$target_name"
|
2023-06-02 23:29:58 +00:00
|
|
|
echo "$edit_file"
|
|
|
|
}
|
|
|
|
|
|
|
|
_prepare_for_edit() {
|
2023-11-09 02:18:12 +00:00
|
|
|
target=$(_readlink "$1")
|
2023-06-02 23:29:58 +00:00
|
|
|
|
|
|
|
_pre_checks_file "$target"
|
|
|
|
|
|
|
|
edit_directory=$(_get_edit_directory "$target")
|
|
|
|
edit_file=$(_get_edit_file "$target")
|
|
|
|
|
|
|
|
mkdir -p "$edit_directory"
|
|
|
|
|
|
|
|
user=$(id -un)
|
|
|
|
group=$(id -gn)
|
2021-02-20 22:35:06 +00:00
|
|
|
|
2023-06-02 23:29:58 +00:00
|
|
|
if sudo test -e "$target"; then
|
|
|
|
sudo cp -i "$target" "$edit_file"
|
|
|
|
sudo chown "$user":"$group" "$edit_file"
|
|
|
|
else
|
|
|
|
touch "$edit_file"
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo "$edit_file"
|
|
|
|
}
|
|
|
|
|
|
|
|
_pre_checks
|
|
|
|
|
|
|
|
# Prepare all files for editing and collect the names of the edit files
|
2023-12-23 16:57:52 +00:00
|
|
|
all_edit_files=()
|
2023-06-02 23:29:58 +00:00
|
|
|
for arg in "$@"; do
|
|
|
|
edit_file=$(_prepare_for_edit "$arg")
|
2023-12-23 16:57:52 +00:00
|
|
|
all_edit_files+=("$edit_file")
|
2023-06-02 23:29:58 +00:00
|
|
|
done
|
|
|
|
|
2023-12-23 16:57:52 +00:00
|
|
|
$EDITOR "${all_edit_files[@]}"
|
2021-02-20 22:35:06 +00:00
|
|
|
|
2023-06-02 23:29:58 +00:00
|
|
|
# Copy all edit files back
|
|
|
|
for arg in "$@"; do
|
|
|
|
edit_file=$(_get_edit_file "$arg")
|
|
|
|
sudo cp "$edit_file" "$arg" && rm "$edit_file"
|
2021-02-21 22:13:41 +00:00
|
|
|
|
2023-06-02 23:29:58 +00:00
|
|
|
edit_directory=$(_get_edit_directory "$target")
|
|
|
|
# Try to remove the directory, but don't fail if it's not empty
|
|
|
|
rm -d "$edit_directory" 2>/dev/null || true
|
|
|
|
done
|