#!/bin/bash
# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


# Install's all the build/test/run dependencies for prow, this meant to run on
# the k8s kubekins package which includes go/bazel/gcloud sdk/k8s and built on
# a debian container.

set -x #echo on
set -e

INSTALL_TMP_DIR=$(mktemp -d)
cd $INSTALL_TMP_DIR

# upgrade bazel
apt-get install -y apt-transport-https curl gnupg
curl -fsSL https://storage.googleapis.com/www.bazel.build/bazel-release.pub.gpg | gpg --dearmor > bazel.gpg
mv bazel.gpg /etc/apt/trusted.gpg.d/
echo "deb [arch=amd64] https://storage.googleapis.com/bazel-apt stable jdk1.8" | tee /etc/apt/sources.list.d/bazel.list

# remove the out of date helm repository, update it with the new buildkite one
# see https://helm.sh/docs/intro/install/#from-apt-debianubuntu
# TODO:vitorguidi remove this once the base ods kubekins image is rebuilt against a more recent base kubekins one
test -f /etc/apt/sources.list.d/helm-stable-debian.list && rm /etc/apt/sources.list.d/helm-stable-debian.list
curl -fsSL https://packages.buildkite.com/helm-linux/helm-debian/gpgkey | gpg --dearmor | tee /usr/share/keyrings/helm.gpg > /dev/null
echo "deb [signed-by=/usr/share/keyrings/helm.gpg] https://packages.buildkite.com/helm-linux/helm-debian/any/ any main" | tee /etc/apt/sources.list.d/helm-stable-debian.list

# everything we can get from debian packages.
apt-get update -qq
apt-get install -y \
  clang-format gettext-base jq

# Install bazelisk.
export BAZELISK_VERSION=v1.26.0
wget -q https://github.com/bazelbuild/bazelisk/releases/download/${BAZELISK_VERSION}/bazelisk-linux-amd64 -O /usr/local/bin/bazel
which bazel

# Link the kubekins install to the typical debian location to match Dev
# machines.
ln -s /google-cloud-sdk /usr/lib/google-cloud-sdk

# If we need a specific kubectl from gcr.io/k8s-testimages/kubekins-e2e
# rm /usr/local/bin/kubectl
# ln -s /google-cloud-sdk/bin/kubectl.1.18 /usr/local/bin/kubectl

gcloud auth configure-docker --quiet
bazel --version
# cleanup
cd /
rm -rf /var/lib/apt/lists/*
rm -rf "$INSTALL_TMP_DIR"
