#!/usr/bin/env sh

# shellcheck disable=SC2312

# if this is being called by tmux-fzf-url, then copy it instead of opening it
pppid="$(ps -o args= "$(ps -o ppid= "${PPID}" | awk '{ print $1 }')")"

mime=$(file -L -i -- "${@}" | awk -F": " '{ print $2 }')
# image/png; charset=binary

if echo "${pppid#*}" | grep -Eq 'uv run *' >/dev/null; then
  case "$(uname)" in
    Darwin) exec open -g "$@" ;;
    *) exec reader --markdown-output "${@}" | less ;;
  esac
elif [ "${pppid#*fzf-url/}" = "fzf-url.sh" ]; then
  case "$(uname)" in
    Darwin)
      exec pbcopy "$1"
      ;;
    *)
      if command -v pbcopy >/dev/null 2>&1; then
        exec pbcopy "$1"
      elif command -v wl-copy >/dev/null 2>&1; then
        exec wl-copy --trim-newline "$1"
      else
        exec xsel --clipboard --input "$1"
      fi
      ;;
  esac
else
  case "${1%%:*}" in
    mailto) exec aerc "$1" 2>/dev/null || exec mutt "$1" 2>/dev/null ;;
    # html) exec browser -- "$1" ;;
    *)
      case "$1" in
        *.jpg | *.png)
          exec timg "$1" \
              || timeout 4 chafa --fill block --symbols block --colors 256 \
              --size 80x"$2" "$1" 2>/dev/null
          ;;
        *.webm | *.mp4 | *.mkv)
          exec mpv "$1"
          ;;
        *.vim*)
          if command -v cot >/dev/null 2>&1; then
            exec cot "$@"
          else
            exec "${EDITOR:-vi}" "$@"
          fi
          ;;
        *)
          case "$(uname -s)" in
            Linux*)
              case "${mime}" in
                image/*)
                  if command -v swayimg >/dev/null 2>&1; then
                    exec swayimg -s fit "$@"
                  else
                    exec timg "$@" 2>/dev/null || exec chafa "$@" 2>/dev/null
                  fi
                  ;;
                application/pdf*)
                  if env | grep -w 'SSH_CLIENT\|SSH_TTY' >/dev/null; then
                    exec p -- "$@" 2>/dev/null || exec less -- "$@"
                  else
                    exec /usr/bin/xdg-open "$@"
                  fi
                  ;;
                text/* | inode/x-empty*)
                  case "${mime}" in
                    text/html* | text/htm* | text/xml*)
                      if env | grep -w 'SSH_CLIENT\|SSH_TTY' >/dev/null; then
                        exec reader --markdown-output "${@}" | less
                      else
                        exec browser "$@"
                      fi
                      ;;
                    *)
                      exec ${EDITOR:-vi} "$@"
                      ;;
                  esac
                  ;;
                *)
                  if echo "$1" | grep -E '(https?|ftp|file)://[-A-Za-z0-9\+&@#/%?=~_|!:,.;]*[-A-Za-z0-9\+&@#/%=~_|]' >/dev/null; then
                    if env | grep -w 'SSH_CLIENT\|SSH_TTY\|VIMRUNTIME' >/dev/null; then
                      exec reader --markdown-output "${@}" | less
                    else
                      exec browser "$@"
                    fi
                  else
                    # printf '%s' "$@" | yank
                    exec /usr/bin/xdg-open "$@"
                  fi
                  ;;
              esac
              ;;
            Darwin)
              case "${mime}" in
                application/pdf*)
                  if command -v mupdf-gl >/dev/null 2>&1; then
                    nohup exec mupdf-gl "$@" >/dev/null 2>/dev/null &
                  else
                    nohup open -a Preview "$@" 2>/dev/null
                  fi
                  ;;
                image*)
                  exec timg "$@" 2>/dev/null || exec chafa "$@" 2>/dev/null
                  ;;
                video/mp4*)
                  exec mpv "$@"
                  ;;
                text/* | inode/x-empty*)
                  case "${mime}" in
                    text/html* | text/htm* | text/xml*)
                      exec browser "$@" 2>/dev/null || exec open -g "$@"
                      ;;
                    *)
                      exec vim "$@"
                      ;;
                  esac
                  ;;
                *)
                  # exec cot "$@"
                  exec open -g "$@"
                  ;;
              esac
              ;;
            *)
              printf '%s\n' "$(uname) unsupported machine." >&2
              exit 1
              ;;
          esac
          ;;
      esac
      ;;
  esac
fi
