#!/usr/bin/env nix-shell
#!nix-shell -i bash -p bash -p sane-open

# use:
# rofi-run-command <handler>.desktop [cmd [args ...]]
# if a non-empty desktop file is provided, we'll just launch that.
# else, if the cmd is `xdg-open`, then we'll treat `args` as a URI and forward that to xdg-open.
# nothing else is as-yet supported.

desktop="$1"
shift
binary="$1"
shift
binArgs=("$@")

if [ "$desktop" != .desktop ]; then
  exec sane-open --auto-keyboard --application "$desktop"
elif [ "$binary" = "xdg-open" ]; then
  # the file we want to open could be a symlink to a .desktop file, so omit `--file` and let sane-open figure that part out for itself:
  exec sane-open --auto-keyboard "${binArgs[@]}"
fi

printf "no .desktop file, and unexpected binary; not invoking: %s %s\n" "$binary" "${binArgs[*]}" >&2
exit 1
