#!/bin/sh

set -eu

exec_dash_e() {
  program="$1"
  shift
  test "$#" -eq 0 && exec "${program}"
  exec "${program}" -e "$*"
}

exec_positional() {
  program="$1"
  shift
  test "$#" -eq 0 && exec "${program}"
  exec "${program}" -- "$@"
}

exec_operational() {
  program="$1"
  shift
  if test "$#" -eq 0; then
    nohup "${program}" >/dev/null 2>&1 &
  else
    nohup "${program}" -- "$@" >/dev/null 2>&1 &
    # nohup "${program}" -- "$@" 2>/dev/null 1>&2 &
    # nohup "${program}" -- "$@" >/dev/null 2>&1 </dev/null &
  fi
}

exec_directional() {
  program="$1"
  shift
  if test "$#" -eq 0; then
    nohup "${program}" >/dev/null 2>&1 &
  else
    dir=
    if [ "$1" = "-d" ]; then
      dir="$2"
      echo "${dir}"
      shift 2
    fi
    # kitty --working-directory="${dir}" -e zsh -c "nvim \"$1\""
    exec "${program}" --working-directory "${dir}" "$@"
  fi
  # nohup "${program}" -- "$@" >/dev/null 2>&1 &
  # nohup "${program}" -- "$@" 2>/dev/null 1>&2 &
  # nohup "${program}" -- "$@" >/dev/null 2>&1 </dev/null &
}

case "$(uname)" in
  Darwin)
    # exec_directional "$@"
    if command -v wezterm >/dev/null 2>&1; then
      if [ "$#" -gt 1 ]; then
        exec wezterm start --cwd "${1:-${PWD}}" "${2-}" >/dev/null 2>&1 &
      else
        exec wezterm start --cwd "${1:-${PWD}}" >/dev/null 2>&1 &
      fi
      # wezterm start --position 10,20 --cwd $PWD
    elif command -v kitty >/dev/null 2>&1; then
      if [ "$#" -gt 1 ]; then
        exec kitty --working-directory "${1:-${PWD}}" "${2-}" >/dev/null 2>&1 &
      else
        exec kitty --working-directory "${1:-${PWD}}" >/dev/null 2>&1 &
      fi
    fi
    ;;
  *)
    if [ "${XDG_SESSION_TYPE:-}" = "wayland" ] \
      || [ -n "${SWAYSOCK-}" ] || [ -n "${WAYLAND_DISPLAY-}" ]; then
      command -v kitty >/dev/null 2>&1 && exec_positional kitty "$@"
      command -v foot >/dev/null 2>&1 && exec_positional foot "$@"
      command -v wezterm >/dev/null 2>&1 && exec_positional wezterm "$@"
      command -v lxterminal >/dev/null 2>&1 && exec_positional lxterminal "$@"
    else
      command -v st >/dev/null 2>&1 && exec_dash_e st "$@"
      command -v kitty >/dev/null 2>&1 && exec_dash_e kitty "$@"
      command -v wezterm >/dev/null 2>&1 && exec_dash_e wezterm "$@"
      command -v xfce4-terminal >/dev/null 2>&1 && exec_dash_e xfce4-terminal "$@"
      command -v mate-terminal >/dev/null 2>&1 && exec_dash_e mate-terminal "$@"
      command -v gnome-terminal >/dev/null 2>&1 && exec_dash_e gnome-terminal "$@"
      command -v terminator >/dev/null 2>&1 && exec_dash_e terminator "$@"
      command -v sakura >/dev/null 2>&1 && exec_dash_e sakura "$@"
      command -v xterm >/dev/null 2>&1 && exec_dash_e xterm "$@"
    fi
    ;;
esac

# exists kitty && exec_positional kitty "$@"
# exists gnome-terminal && exec_positional gnome-terminal "$@"
#
# exists xfce4-terminal && exec_dash_e xfce4-terminal "$@"
# exists mate-terminal && exec_dash_e mate-terminal "$@"
# exists terminator && exec_dash_e terminator "$@"
# exists sakura && exec_dash_e sakura "$@"
# exists xterm && exec_dash_e xterm "$@"

# command -v kitty >/dev/null && exec kitty --single-instance "$@"
# urxvtc "$@"
# status="$?"
# test "${status}" -eq 2 && urxvtd -q -o -f && { sleep 1; exec "$0" "$@"; }
# exit "${status}"
