1
0
Fork 0
dotfiles/.local/bin/imv-slatectx

59 lines
1.2 KiB
Bash
Executable File

#!/bin/bash
[ -z "$SLATECTX_CONTEXT_NAME" ] && SLATECTX_CONTEXT_NAME="$(slatectx-get-context-name)"
[ -z "$XDG_RUNTIME_DIR" ] && XDG_RUNTIME_DIR="/run/user/$(id -u)"
mkdir -p "$XDG_RUNTIME_DIR/slatectx"
pidfile="$XDG_RUNTIME_DIR/slatectx/$SLATECTX_CONTEXT_NAME-imv.pid"
imagefile="$XDG_RUNTIME_DIR/slatectx/$SLATECTX_CONTEXT_NAME-imv.image"
imv_server() {
imv "$@" &
IMV_PID="$!"
printf "%s" "$IMV_PID" > "$pidfile"
wait "$IMV_PID"
rm "$pidfile" || true
}
imv_client() {
imv-msg "$(head -n 1 "$pidfile")" "$@"
}
launch_server=yes
image_path=""
while [[ "$#" -gt 0 ]]; do
case "$1" in
--only-if-already-open)
launch_server=""
shift 1 ;;
--file)
[[ -z "$image_path" ]] && image_path="$2"
shift 2 ;;
*)
[[ -z "$image_path" ]] && image_path="$1"
shift 1 ;;
esac
done
[[ -z "$image_path" ]] && image_path="/dev/null"
if [[ -n "$image_path" ]] ; then
# link current file to this specific location to avoid dealing with esacped sequences ro spaces in the image path
ln -sf "$image_path" "$imagefile"
else
# remove symlink to imagefile
rm "$imagefile"
imagefile=""
fi
if [[ -e "$pidfile" ]] ; then
if imv_client close all ; then
imv_client open "$imagefile"
exit 0
fi
fi
[[ -n "$launch_server" ]] && imv_server -- "$imagefile" &