#!/usr/bin/env bash

#ddev-generated

## Description: Explain how to upgrade DDEV
## Usage: self-upgrade
## Example: "ddev self-upgrade"
## CanRunGlobally: true

# split the loop output below by newlines
IFS=$'\n'
for ddev_path in $(which -a ddev | uniq); do
  case $ddev_path in
    "/usr/bin/ddev")
      if [[ ${OSTYPE} = "linux-gnu"* ]]; then
        if command -v apt-get; then echo "You seem to have an apt-installed DDEV, upgrade with 'sudo apt-get update && sudo apt-get upgrade -y ddev'";
        elif [ -f /etc/arch-release ] && command -v yay >/dev/null ; then echo "You seem to have yay-installed DDEV (AUR), upgrade with 'yay -Syu ddev-bin'";
        elif command -v dnf; then echo "You seem to have dnf-installed DDEV, upgrade with 'sudo dnf install --refresh ddev'"; fi
      fi
      ;;

    "/usr/local/bin/ddev")
      if [ ! -L /usr/local/bin/ddev ]; then
        printf "DDEV appears to have been installed with install_ddev.sh, you can run that script again to update.\ncurl -fsSL https://raw.githubusercontent.com/ddev/ddev/main/scripts/install_ddev.sh | bash\n"
      elif command -v brew; then
        echo "DDEV appears to have been installed with Homebrew, upgrade with 'brew update && brew upgrade ddev'"
      fi
      ;;

    "/opt/homebrew/bin/ddev" | "/home/linuxbrew/.linuxbrew/bin/ddev")
      if [ -L "$(which ddev)" ] && command -v brew; then
        echo "DDEV appears to have been installed with Homebrew, upgrade with 'brew update && brew upgrade ddev'"
      fi
      ;;

    "/c/Program Files/DDEV/ddev")
      printf "DDEV was either installed with\nchoco install -y ddev\nor with the installer package.\n"
      echo "You can upgrade with 'choco upgrade -y ddev'"
      echo "Or by downloading the Windows installer from https://github.com/ddev/ddev/releases"
      ;;

    *)
      echo "Unable to determine how you installed DDEV, but you can remove $ddev_path and reinstall with one of the techniques in https://ddev.readthedocs.io/en/stable/users/install/ddev-installation/"

  esac
done
printf "\n'ddev --version' will show you the version you currently have installed.\n"
printf "'which ddev' will show you where that executable is on your filesystem.\n"
printf "See docs for more info: \nhttps://ddev.readthedocs.io/en/stable/users/install/ddev-installation/\n"
