.dotfiles/.local/bin/edit

30 lines
525 B
Text
Raw Normal View History

#!/usr/bin/sh
priv_cmd="sudo"
if [ -x "$(command -v doas)" ]
then
priv_cmd="doas --"
fi
edit_directory="$HOME/.cache/edit/"
mkdir -p $edit_directory
2021-02-20 23:43:32 +00:00
filename=$(readlink -fn "$1" | md5sum | awk '{ print $1 }')-$(basename $1)
filepath="$edit_directory/$filename"
2021-02-26 14:28:32 +00:00
filepath=$(realpath -s "$filepath")
user=$(id -un)
group=$(id -gn)
echo "Editing $1 in $filepath as $user:$group"
$priv_cmd cp "$1" "$filepath"
$priv_cmd chown $user:$group "$filepath"
$EDITOR "$filepath"
$priv_cmd cp "$filepath" "$1"
rm "$filepath"