#!/bin/sh

set -eu

LINKERD2_VERSION=${LINKERD2_VERSION:-edge-26.5.1}
INSTALLROOT=${INSTALLROOT:-"${HOME}/.linkerd2"}

happyexit() {
  echo ""
  echo "*******************************************"
  echo "* This script is deprecated and no longer *"
  echo "* installs stable releases.               *"
  echo "*                                         *"
  echo "* The latest edge release has been        *"
  echo "* installed. In the future, please use    *"
  echo "*   run.linkerd.io/install-edge           *"
  echo "* for this behavior.                      *"
  echo "*                                         *"
  echo "* For stable releases, please see         *"
  echo "*  https://linkerd.io/releases/           *"
  echo "*******************************************"
  echo ""
  echo "Add the linkerd CLI to your path with:"
  echo ""
  echo "  export PATH=\$PATH:${INSTALLROOT}/bin"
  echo ""
  echo "Now run:"
  echo ""
  echo "  # install the GatewayAPI CRDs"
  echo "  kubectl apply -f https://github.com/kubernetes-sigs/gateway-api/releases/download/v1.2.1/standard-install.yaml"
  echo ""
  echo "  linkerd check --pre                         # validate that Linkerd can be installed"
  echo "  linkerd install --crds | kubectl apply -f - # install the Linkerd CRDs"
  echo "  linkerd install | kubectl apply -f -        # install the control plane into the 'linkerd' namespace"
  echo "  linkerd check                               # validate everything worked!"
  echo ""
  echo "You can also obtain observability features by installing the viz extension:"
  echo ""
  echo "  linkerd viz install | kubectl apply -f -  # install the viz extension into the 'linkerd-viz' namespace"
  echo "  linkerd viz check                         # validate the extension works!"
  echo "  linkerd viz dashboard                     # launch the dashboard"
  echo ""
  echo "Looking for more? Visit https://linkerd.io/2/tasks"
  echo ""
  exit 0
}

OS=$(uname -s)
arch=$(uname -m)
cli_arch=""
case $OS in
  CYGWIN* | MINGW64*)
    OS=windows.exe
    ;;
  Darwin)
    case $arch in
      x86_64)
        cli_arch=""
        ;;
      arm64)
        cli_arch=$arch
        ;;
      *)
        echo "There is no linkerd $OS support for $arch. Please open an issue with your platform details."
        exit 1
        ;;
    esac
    ;;
  Linux)
    case $arch in
      x86_64)
        cli_arch=amd64
        ;;
      armv8*)
        cli_arch=arm64
        ;;
      aarch64*)
        cli_arch=arm64
        ;;
      armv*)
        cli_arch=arm
        ;;
      amd64|arm64)
        cli_arch=$arch
        ;;
      *)
        echo "There is no linkerd $OS support for $arch. Please open an issue with your platform details."
        exit 1
        ;;
    esac
    ;;
  *)
    echo "There is no linkerd support for $OS/$arch. Please open an issue with your platform details."
    exit 1
    ;;
esac
OS=$(echo "$OS" | tr '[:upper:]' '[:lower:]')

tmpdir=$(mktemp -d /tmp/linkerd2.XXXXXX)
srcfile="linkerd2-cli-${LINKERD2_VERSION}-${OS}"
if [ -n "${cli_arch}" ]; then
  srcfile="${srcfile}-${cli_arch}"
fi
dstfile="${INSTALLROOT}/bin/linkerd-${LINKERD2_VERSION}"
url="https://github.com/linkerd/linkerd2/releases/download/${LINKERD2_VERSION}/${srcfile}"

if [ -e "${dstfile}" ]; then
  echo ""
  echo "Linkerd ${LINKERD2_VERSION} was already downloaded; making it the default 🎉"
  echo ""
  echo "To force re-downloading, delete '${dstfile}' then run me again."
  (
    rm -f "${INSTALLROOT}/bin/linkerd"
    ln -s "${dstfile}" "${INSTALLROOT}/bin/linkerd"
  )
  happyexit
fi

(
  cd "$tmpdir"

  echo "Downloading ${srcfile}..."
  curl --proto '=https' --tlsv1.2 -fLO "${url}"
  echo "Download complete!"
  echo ""
)

(
  mkdir -p "${INSTALLROOT}/bin"
  mv "${tmpdir}/${srcfile}" "${dstfile}"
  chmod +x "${dstfile}"
  rm -f "${INSTALLROOT}/bin/linkerd"
  ln -s "${dstfile}" "${INSTALLROOT}/bin/linkerd"
)


rm -r "$tmpdir"

echo "Linkerd ${LINKERD2_VERSION} was successfully installed 🎉"
echo ""
happyexit
