#!/usr/bin/env sh

# shellcheck disable=SC2312

set -eu

IFS=$(printf ' \n\t')

print_header() { printf "\n\e[1;33m%s${1}\e[0m\n\n"; }
print_green() { printf "\033[0;32m%s\e[0m\n" "$*" >&2; }
print_yellow() { printf "\033[0;33m%s\e[0m\n" "$*" >&2; }
print_blue() { printf "\033[0;34m%s\e[0m\n" "$*" >&2; }
print_magenta() { printf "\033[0;35m%s\e[0m\n" "$*" >&2; }
print_white() { printf "\033[0;37m%s\e[0m\n" "$*" >&2; }

start_time=$(date '+%s')

print_header "updating stuff..."

# cargo
# if [ "$(uname -s)" = "Linux" ] && command -v rustup >/dev/null 2>&1; then
if command -v rustup >/dev/null 2>&1; then
  print_white "==> Updating rust..."
  rustup update stable
fi
if command -v "cargo-install-update" >/dev/null 2>&1; then
  print_white "updating rust..."
  cargo install-update --all --git
elif command -v cargo >/dev/null 2>&1; then
  print_white "updating rust..."
  cargo_git="$(cargo install --list 2>/dev/null | grep "https"  | awk '{ print $1 }')"
  cargo_list() {
    cargo install --list 2>/dev/null | grep -v "${cargo_git}" | sed -e 's/[[:space:]].*//' -e '/^$/ d'
  }
  cargo_list | while read -r f; do
    cargo install "${f}"
  done
  #cargo_list="$(cargo install --list 2>/dev/null | sed -e 's/[[:space:]].*//' -e '/^$/ d')"
  #cargo install "${cargo_list}"
  #cargo install "$(cargo install --list | grep -E '^[a-z0-9_-]+ v[0-9.]+:$' | cut -f1 -d' ')"
fi

if command -v cargo-cache >/dev/null 2>&1; then
  cargo cache --autoclean
fi

# shellcheck disable=SC2312
if [ "$(uname -s)" = "Linux" ]; then
  # run updates in the background, so they should slow me down
  BACKGROUND="ionice -c 3 -t nice"

  if command -v nixos-rebuild >/dev/null; then
    sudo nixos-rebuild switch --upgrade-all
  elif command -v pacman >/dev/null; then
    sudo pacman -Syu
  elif command -v pkg >/dev/null; then
    pkg upgrade
  fi

  if command -v apt >/dev/null 2>&1; then
    print_blue "apt packages..."
    # apt-get update -qq -y
    # sudo apt-get --fix-missing install
    sudo apt --fix-broken install
    ${BACKGROUND} sudo apt-get autoremove --yes
    ${BACKGROUND} sudo apt-get update --yes
    ${BACKGROUND} sudo apt-get upgrade --yes
    ${BACKGROUND} sudo apt-get autoclean --yes
  fi

  # flatpak
  if command -v flatpak >/dev/null 2>&1; then
    print_magenta "cleaning flatpak..."
    flatpak --user repair
    flatpak --user update -y
    flatpak --user uninstall --unused -y
  fi

  # snap
  if command -v snap >/dev/null; then
    # confirm that snap works before proceeding
    if snap interfaces >/dev/null 2>&1; then
      sudo snap refresh
    fi
  fi
fi

# go
if command -v go >/dev/null 2>&1 && [ -n "${GOPATH}" ]; then
  printf '%s\n' "updating go tools..."
  unset GOPROXY
  /bin/ls -1 "${HOME}"/.local/share/go/bin/ | while read -r bin; do
    go version -m "${HOME}"/.local/share/go/bin/"${bin}" \
      | grep '^[[:space:]]path' \
      | awk '{ print $2 }' \
      | grep '^github.com' \
      | sort \
      | uniq \
      | xargs -I{} go install {}@latest
  done
fi

# python
pip3 install --upgrade pip 2>/dev/null

# pipx
if command -v pipx >/dev/null 2>&1; then
  print_green "updating pipx..."
  # pipx upgrade-all
  pipx upgrade-all --include-injected
fi

# uv
if command -v uv >/dev/null 2>&1; then
  print_magenta "updating uv..."
  uv self update
  uv tool upgrade --all
  uv cache prune
fi

# tinty
if command -v tinty >/dev/null 2>&1; then
  print_yellow "updating themes..."
  tinty update
  tinty sync
fi

# tldr
if command -v tldr >/dev/null 2>&1; then
  print_white "updating tealdeer..."
  tldr --config "${HOME%/}/.config/tlrc/config.toml" --update
fi

# newsboat
# if command -v newsboat >/dev/null 2>&1; then
#   print_white "updating news..."
#   newsboat -x reload
# fi

# nvim/vim
# if command -v nvim >/dev/null 2>&1; then
#   print_green "updating nvim plugs..."
#   nvim --noplugin +PlugUpdate +qall
#   nvim --noplugin +PlugClean! +qall
# else
#   print_green "updating vim plugs..."
#   vim --noplugin +PlugUpdate +qall
#   vim --noplugin +PlugClean! +qall
# fi

end_time=$(date '+%s')
# shellcheck disable=SC2250
elapsed=$(echo "$end_time - $start_time" | bc)
printf '%s\n' "time elapsed: ${elapsed} seconds"
