#!/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
        packages_to_upgrade="ddev"
        if [[ -f "/usr/bin/ddev-hostname.exe" ]]; then
          packages_to_upgrade="$packages_to_upgrade ddev-wsl2"
        fi
        if command -v apt-get; then
          echo "You seem to have an apt-installed DDEV, upgrade with 'sudo apt-get update && sudo apt-get install --only-upgrade ${packages_to_upgrade}'"
        elif [ -f /etc/arch-release ]; then
          echo "You seem to have an AUR-installed DDEV, upgrade with your AUR helper, for example, 'yay -Syu ddev-bin'"
        elif command -v dnf; then
          echo "You seem to have dnf-installed DDEV, upgrade with 'sudo dnf upgrade --refresh ${packages_to_upgrade}'"
        fi
      fi
      ;;

    "/usr/local/bin/ddev")
      if [ ! -L /usr/local/bin/ddev ]; then
        printf "DDEV appears to have been installed with a script, run it again for upgrade.\ncurl -fsSL https://ddev.com/install.sh | bash\n"
      elif command -v brew; then
        echo "DDEV appears to have been installed with Homebrew, upgrade with 'brew update && brew upgrade ddev/ddev/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/ddev/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://docs.ddev.com/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://docs.ddev.com/en/stable/users/install/ddev-installation/\n"
