#!/usr/bin/env bash

set -o errexit
set -o nounset
set -o pipefail

function grab_manifests() {
  local prefix=${1:-}
  local kube_version=${2:-v1.17.0}
  local cert_manager_version=${3:-v0.15.0}
  local tidb_operator_version=${4:-v1.5.0-alpha.8}
  local aws_pca_version=${5:-v0.1.2}
  local vector_version=${6:-0.13.2}
  local victoria_metrics_agent_version=${7:-0.8.9}

  mkdir -p "$prefix"
  pushd "$prefix" >/dev/null

  mkdir -p "./charts"
  mkdir -p "./manifests"
  mkdir -p "./bin"

  pushd charts >/dev/null
  operator_chart="tidb-operator-${tidb_operator_version}.tgz"
  if ! test -f "$operator_chart" ; then
    curl -Lsfo "./$operator_chart" "http://charts.pingcap.org/${operator_chart}"
    cp "$operator_chart" tidb-operator.tgz
  fi
  cert_manager="cert-manager-${tidb_operator_version}.tgz"
  if ! test -f "$cert_manager" ; then
    curl -Lsfo "./$cert_manager" "https://charts.jetstack.io/charts/cert-manager-${cert_manager_version}.tgz"
    cp "$cert_manager" cert-manager.tgz
  fi
  vector="vector-${vector_version}.tgz"
  if ! test -f "$vector" ; then
    curl -Lsfo "./$vector" "https://github.com/SabaPing/helm-charts/releases/download/vector-${vector_version}-hostnetwork/vector-${vector_version}.tgz"
    cp "$vector" vector.tgz
  fi
  victoria_metrics_agent="victoria_metrics_agent-${victoria_metrics_agent_version}.tgz"
  if ! test -f "$victoria_metrics_agent" ; then
    curl -Lsfo "./$victoria_metrics_agent" "https://victoriametrics.github.io/helm-charts/packages/victoria-metrics-agent-${victoria_metrics_agent_version}.tgz"
    cp "$victoria_metrics_agent" victoria_metrics_agent.tgz
  fi
  popd

  pushd manifests >/dev/null
  crd="tidb-operator-crd-${tidb_operator_version}.yaml"
  if ! test -f "$crd" ; then
    curl -Lsfo "./$crd" "https://raw.githubusercontent.com/pingcap/tidb-operator/${tidb_operator_version}/manifests/crd.yaml"
    cp "$crd" tidb-operator-crd.yaml
  fi
  set="advanced-statefulset-crd.v1-${tidb_operator_version}.yaml"
  if ! test -f "$set" ; then
    curl -Lsfo "./$set" "https://raw.githubusercontent.com/pingcap/tidb-operator/${tidb_operator_version}/manifests/advanced-statefulset-crd.v1.yaml"
    cp "$set" advanced-statefulset-crd.v1.yaml
  fi
  cert_manager_crd="cert-manager-crd-${tidb_operator_version}.yaml"
  if ! test -f "$cert_manager_crd" ; then
    curl -Lsfo "./$cert_manager_crd" "https://github.com/jetstack/cert-manager/releases/download/${cert_manager_version}/cert-manager.crds.yaml"
    cp "$cert_manager_crd" cert-manager-crd.yaml
  fi
  clusterissuer_crd=aws-pca-clusterissuer-crd.yaml
  if ! test -f "$clusterissuer_crd" ; then
    curl -Lsfo "$clusterissuer_crd" "https://raw.githubusercontent.com/cert-manager/aws-pca-issuer/${aws_pca_version}/charts/aws-pca-issuer/crds/awspca.cert-manager.io_awspcaclusterissuers.yaml"
  fi
  issuer_crd="aws-pca-issuer-crd.yaml"
  if ! test -f $issuer_crd ; then
    curl -Lsfo "$issuer_crd" "https://raw.githubusercontent.com/cert-manager/aws-pca-issuer/${aws_pca_version}/charts/aws-pca-issuer/crds/awspca.cert-manager.io_awspcaissuers.yaml"
  fi
  popd

  # wget -O "./charts/loki-stack.tgz" "https://grafana.github.io/loki/charts/loki-stack-0.36.2.tgz"
  # wget -O ./manifests/alertmanager-deployment.yaml https://raw.githubusercontent.com/pingcap/monitoring/master/alertmanager/alertmanager-deployment.yaml
  # wget -O ./manifests/alertmanager-service.yaml https://raw.githubusercontent.com/pingcap/monitoring/master/alertmanager/alertmanger-service.yaml
  # wget -O ./manifests/alertmanager-service.yaml https://raw.githubusercontent.com/pingcap/monitoring/master/alertmanager/alertmanger-service.yaml

  os=$(uname | tr '[:upper:]' '[:lower:]')
  if [ ! -f "./bin/kubectl" ]; then
    curl -Lsfo "./bin/kubectl" "https://dl.k8s.io/release/${kube_version}/bin/$os/amd64/kubectl"
    chmod +x "./bin/kubectl"
  fi

  if [ ! -f "./bin/helm" ]; then
    version=v3.6.0
    curl -Lsfo "/tmp/helm-$version-$os-amd64.tar.gz" "https://get.helm.sh/helm-$version-$os-amd64.tar.gz"
    tar xzvf "/tmp/helm-$version-$os-amd64.tar.gz" "$os-amd64/helm"
    mv "$os-amd64/helm" "./bin/helm"
    rm "/tmp/helm-$version-$os-amd64.tar.gz"
    rmdir "$os-amd64"
  fi
}

grab_manifests "$@"
