#!/bin/sh
#
# dmenu/pick/fzf/fzy wrapper whether it is called from terminal
# options:
#   -p "prompt"
#
# ---
# see: https://si3t.ch/code/match/

# dmenu options
l=15
fn="OpusOne-12:antialias=true:autohint=true"
# b&w
nb="#000"
nf="#888"
sb="#fff"
sf="#000"

# shellcheck disable=SC2220
while getopts 'p:' c; do
  case ${c} in
    p) p="${OPTARG}" ;;
  esac
done

# while [ "$#" -gt 0 ]; do
#   case "$1" in
#     p) p="${OPTARG}" ;;
#   esac
# done

if [ -n "${TERM}" ]; then
  # run from commandline
  if command -v fzy >/dev/null 2>&1; then
    fzy -p "${p}"
  elif command -v fzf >/dev/null 2>&1; then
    fzf --prompt="${p}"
  else
    pick
  fi
else
  if command -v fuzzel >/dev/null 2>&1; then
    fuzzel --dmenu -i -l "${l}" -p "${p}"
  else
    # run from dmenu
    dmenu -i -l "${l}" -fn "${fn}" -nb "${nb}" -nf "${nf}" -sb "${sb}" -sf "${sf}" -p "${p}"
  fi
fi
