#!/usr/bin/env sh

set -e
set -ux

bashdest="$(pkg-config --variable=completionsdir bash-completion)"
# bashdest=/usr/share/bash-completion/completions
bashuserdest=${HOME}/.local/share/bash-completion

zshdest=/usr/share/zsh/vendor-completions
zfuncdest=${HOME}/.config/zsh/.zfunc

run() {
  echo '$' "$@"
  "$@"
}

make_dir() {
  run mkdir -p "$1"
}

if [ ! -d "${bashuserdest}" ]; then
  make_dir "${bashuserdest}"
fi

if [ ! -d "${zfuncdest}" ]; then
  make_dir "${zfuncdest}"
fi

# aichat
# if command -v aichat >/dev/null 2>&1; then
#   aichaturl=https://raw.githubusercontent.com/sigoden/aichat/refs/heads/main/scripts/completions
#   curl -fsSL "${aichaturl}"/aichat.zsh -o "${zfuncdest}"/_aichat
#   curl -fsSL "${aichaturl}"/aichat.bash -o "${bashuserdest}"/aichat
# fi

# chafa
# if command -v chafa >/dev/null 2>&1; then
#   chafaurl=https://raw.githubusercontent.com/hpjansson/chafa/refs/heads/master/tools/completions
#   curl -fsSL "${chafaurl}"/zsh-completion.zsh -o "${zfuncdest}"/_chafa
# fi

# delta
if command -v delta >/dev/null 2>&1 && [ ! -f "${zshdest}"/_delta ]; then
  delta --generate-completion zsh >|"${zfuncdest}"/_delta
  # delta --generate-completion bash | sudo tee "${bashdest}"/delta >/dev/null
  # deltaurl=https://raw.githubusercontent.com/dandavison/delta/main/etc/completion
  # sudo curl -fsSL "${deltaurl}"/completion.zsh -o "${zfuncdest}"/_delta
fi

# fd
if command -v fdfind >/dev/null 2>&1 && [ ! -f "${zshdest}"/_fd ]; then
  fdurl=https://raw.githubusercontent.com/sharkdp/fd/refs/heads/master/contrib/completion
  curl -fsSL "${fdurl}"/_fd -o "${zfuncdest}"/_fd
fi

# git
if command -v git >/dev/null 2>&1; then
  giturl=https://raw.githubusercontent.com/git/git/master/contrib/completion
  curl -fsSL "${giturl}"/git-completion.bash -o "${bashuserdest}"/git-completion.bash
  curl -fsSL "${giturl}"/git-completion.bash -o "${zfuncdest}"/git-completion.bash
  curl -fsSL "${giturl}"/git-completion.zsh -o "${zfuncdest}"/_git
fi

# eza
if command -v eza >/dev/null 2>&1 && [ ! -f "${zshdest}"/_eza ]; then
  # ezaburl=https://raw.githubusercontent.com/eza-community/eza/refs/heads/main/completions/bash
  ezazurl=https://raw.githubusercontent.com/eza-community/eza/refs/heads/main/completions/zsh
  curl -fsSL "${ezazurl}"/_eza -o "${zfuncdest}"/_eza
  # sudo curl -fsSL "${ezaburl}"/eza -o "${bashdest}"/eza
fi

# notes
# if command -v notes >/dev/null 2>&1; then
#   notesurl=https://raw.githubusercontent.com/pimterry/notes/refs/heads/master
#   curl -fsSL "${notesurl}"/notes.bash_completion -o "${bashuserdest}"/notes
# fi

# pa
# if command -v pa >/dev/null 2>&1; then
#   paurl=https://raw.githubusercontent.com/biox/pa/refs/heads/main/contrib
#   curl -fsSL "${paurl}"/pa-completion.bash -o "${bashuserdest}"/pa
# fi

# ramalama
if command -v ramalama >/dev/null 2>&1; then
  ramaurl=https://raw.githubusercontent.com/containers/ramalama/refs/heads/main/completions
  curl -fsSL "${ramaurl}"/zsh/vendor-completions/_ramalama -o "${zfuncdest}"/_ramalama
fi

# xh - httpie-in-rust
if command -v xh >/dev/null 2>&1; then
  xhurl=https://raw.githubusercontent.com/ducaale/xh/refs/heads/master/completions
  curl -fsSL "${xhurl}/"_xh -o "${zfuncdest}"/_xh
  # sudo curl -fsSL "${xhurl}"/xh.bash -o "${bashdest}"/xh
fi

# caddy/xcaddy
if command -v caddy >/dev/null 2>&1; then
  caddy completion zsh >|"${zfuncdest}"/_caddy
fi
if command -v xcaddy >/dev/null 2>&1; then
  xcaddy completion zsh >|"${zfuncdest}"/_xcaddy
fi

# cargo/rustup
if command -v rustup >/dev/null 2>&1; then
  rustup completions zsh >|"${zfuncdest}"/_rustup
  rustup completions bash >|"${bashuserdest}"/rustup
  # rustup completions zsh cargo >|"${zfuncdest}"/_cargo
  # rustup completions bash cargo >|"${bashuserdest}"/cargo
fi

# fzf
# if command -v fzf >/dev/null 2>&1; then
#   fzf --bash >|"${bashuserdest}"/fzf-completion
# fi

# linkdingcli
if command -v linkding >/dev/null 2>&1; then
  linkding --show-completion >|"${zfuncdest}"/_linkding
fi

# jc
if command -v jc >/dev/null 2>&1; then
  jc -Z >|"${zfuncdest}"/_jc
  jc --bash-comp >|"${bashuserdest}"/jc
fi

# dblab
if command -v dblab >/dev/null 2>&1; then
  dblab completion zsh >|"${zfuncdest}"/_dblab
fi

# dagu
if command -v dagu >/dev/null 2>&1; then
  dagu completion zsh >|"${zfuncdest}"/_dagu
fi

# doggo
if command -v doggo >/dev/null 2>&1; then
  doggo completions zsh >|"${zfuncdest}"/_doggo
fi

# glow
if command -v glow >/dev/null 2>&1; then
  glow completion zsh >|"${zfuncdest}"/_glow
  glow completion bash >|"${bashuserdest}"/glow
fi

# gopass
if command -v gopass >/dev/null 2>&1; then
  gopass completion zsh >|"${zfuncdest}"/_gopass
  # gopass completion bash | sudo tee "${bashdest}"/gopass >/dev/null
fi

# hgrep
if command -v hgrep >/dev/null 2>&1; then
  hgrep --generate-completion-script zsh >|"${zfuncdest}"/_hgrep
  hgrep --generate-completion-script bash >|"${bashuserdest}"/hgrep
fi

# mods
if command -v mods >/dev/null 2>&1; then
  mods completion zsh >|"${zfuncdest}"/_mods
fi

# ov
if command -v ov >/dev/null 2>&1; then
  ov --completion zsh >|"${zfuncdest}"/_ov
  ov --completion bash >|"${bashuserdest}"/ov
fi

# pipx
if command -v pipx >/dev/null 2>&1; then
  register-python-argcomplete pipx >|"${zfuncdest}"/_pipx
  # register-python-argcomplete pipx >|"${bashuserdest}"/pipx
fi

# podman
if command -v podman >/dev/null 2>&1; then
  podman completion zsh >|"${zfuncdest}"/_podman
  podman completion bash >|"${bashuserdest}"/podman
fi

# procs
if command -v procs >/dev/null 2>&1 && ! command -v /usr/bin/procs >/dev/null 2>&1; then
  procs --gen-completion-out zsh >|"${zfuncdest}"/_procs
  procs --gen-completion-out bash >|"${bashuserdest}"/procs
fi

# qgrep
if command -v qgrep >/dev/null 2>&1; then
  qgrepurl=https://raw.githubusercontent.com/zeux/qgrep/refs/heads/master/shell-completion/bash
  curl -sSfL "${qgrepurl}"/qgrep -o "${bashuserdest}"/qgrep
fi

# sqliteutils
if command -v sqlite-utils >/dev/null 2>&1; then
  _SQLITE_UTILS_COMPLETE=zsh_source sqlite-utils >|"${zfuncdest}"/_sqlite-utils
  # _SQLITE_UTILS_COMPLETE=bash_source sqlite-utils >| ${bashuserdest}"/sqlite-utils
fi

# tailscale
if command -v tailscale >/dev/null 2>&1; then
  tailscale completion zsh >|"${zfuncdest}"/_tailscale
  # tailscale completion bash | sudo tee "${bashdest}"/tailscale >/dev/null
  tailscale completion bash | sudo tee "${bashdest}"/tailscale >/dev/null
  sudo chmod 644 "${bashdest}"/tailscale
fi

# termframe
if command -v termframe >/dev/null 2>&1; then
  termframe --shell-completions zsh >|"${zfuncdest}"/_termframe
fi

# toot
if command -v toot >/dev/null 2>&1; then
  _TOOT_COMPLETE=zsh_source toot >|"${zfuncdest}"/_toot
fi

# uv
if command -v uv >/dev/null 2>&1; then
  uv generate-shell-completion bash >|"${bashuserdest}"/uv
  uvx --generate-shell-completion bash >|"${bashuserdest}"/uvx
  uv generate-shell-completion zsh >|"${zfuncdest}"/_uv
  uvx --generate-shell-completion=zsh >|"${zfuncdest}"/_uvx
fi

{
  set +ux
} 2>/dev/null
