#!/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 --brief --dereference --mime-type -- "$@")

if [ "${pppid#*fzf-url/}" = "fzf-url.sh" ]; then
  case "$(uname -s)" 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" || exec mutt "$1"
      ;;
    # html)
    #   exec browser -- "$1"
    #   ;;
    *)
      case "$1" in
        *.webm | *.mp4 | *.mkv)
          exec mpv "$1"
          ;;
        *.vim*)
          if command -v cot >/dev/null 2>&1; then
            exec cot "$@"
          else
            exec "${EDITOR:-vim}" "$@"
          fi
          ;;
        *)
          case "$(uname -s)" in
            Linux)
              if file --brief --dereference --mime-type -- "$1" | grep -i 'image' >/dev/null 2>&1; then
                if command -v swayimg >/dev/null 2>&1; then
                  exec swayimg -s fit "$@"
                else
                  exec chafa "$@"
                fi
              elif file --brief --dereference --mime-type -- "$1" | grep 'application/pdf' >/dev/null 2>&1; then
                if env | grep -w 'SSH_CLIENT\|SSH_TTY' >/dev/null; then
                  exec p -- "$@"
                else
                  exec /usr/bin/xdg-open "$@"
                fi
              elif file --brief --dereference --mime-type -- "$1" | grep -E 'text/*|inode/x-empty' >/dev/null 2>&1; then
                if file --brief --dereference --mime-type -- "$1" | grep -E 'text/html|text/htm' >/dev/null 2>&1; then
                  if env | grep -w 'SSH_CLIENT\|SSH_TTY' >/dev/null; then
                    exec reader --markdown-output "${@}" | less
                  else
                    exec browser "$@"
                  fi
                else
                  exec ${EDITOR:-vim} "$@"
                fi
              elif 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
              ;;
            Darwin)
              if file --brief --dereference --mime-type -- "$1" | grep -w 'application/pdf' >/dev/null 2>&1; then
                exec mupdf-gl "$@" >/dev/null 2>/dev/null &
                # exec open -a Preview "$@"
              elif file --brief --dereference --mime-type -- "$1" | grep -i 'image' >/dev/null 2>&1; then
                exec chafa "$@"
              elif file --brief --dereference --mime-type -- "$1" | grep -E 'video/mp4' >/dev/null 2>&1; then
                exec mpv "$@"
              elif file --brief --dereference --mime-type -- "$1" | grep -E 'text/*|inode/x-empty' >/dev/null 2>&1; then
                if file --brief --dereference --mime-type -- "$1" | grep -E 'text/html|text/htm' >/dev/null 2>&1; then
                  exec browser "$@"
                  # exec open -g "$@"
                else
                  exec nv "$@"
                fi
              else
                exec nv "$@"
                # exec cot "$@"
                # exec open -g "$@"
              fi
              ;;
            *)
              printf '%s\n' "$(uname) unsupported machine."
              ;;
          esac
          ;;
      esac
      ;;
  esac
fi

# case "${1%%:*}" in
#   http | https) exec {app/web_browser} "$1" ;;
#   mailto) exec {app/mail_composer} "$1" ;;
# esac
# case "$(echo "${1##*.}" | tr '[:upper:]' '[:lower:]')" in
#   asc | txt) exec {app/text_editor} "$1" ;;
#   htm | html) exec {app/web_browser} "$1" ;;
#   cbr | cbz | epub | pdf) exec {app/document_viewer} "$1" ;;
#   jpg | jpeg | jpe | jfif | png | gif | svg) exec {app/image_viewer} "$1" ;;
# esac

# case "${1%%:*}" in
#   http | https) exec browser "$1" ;;
#   *.pdf) exec zathura "$1" ;;
#   *) exec /usr/bin/xdg-open "$@" ;;
# esac
