#!/bin/sh

set -eu

readonly sock="${HOME%/}/.opener.sock"

if [ -n "${SSH_CLIENT:-}" ]; then
  if [ -S "${sock}" ]; then
    if [ -p /dev/stdin ]; then
      cat - | nc -U "${sock}"
    else
      echo "${@}" | nc -U "${sock}"
    fi

    exit 0
  fi
fi

if command -v xdg-open >/dev/null; then
  xdg-open "$@"
else
  /usr/bin/open "$@"
fi
