#!/bin/bash
set -e
set -x

# setup tiup docker
# cd contrib && git clone https://github.com/gdraheim/docker-systemctl-replacement.git && \
#     wget https://dl.min.io/server/minio/release/linux-amd64/minio && \
#     wget https://dl.min.io/client/mc/release/linux-amd64/mc
# 
# eval $(minikube docker-env)
# docker build -t minikube_autoscale_tiup:centos -f ./minikube_tiup_dockerfile .
# docker run -d -v /sys/fs/cgroup:/sys/fs/cgroup:ro --name tiup --entrypoint /bin/bash minikube_autoscale_tiup:centos
# docker exec -it tiup /bin/bash

if [ "$#" -lt 1 ]; then
    echo "usage: $0 tiflash-autoscale_home_dir"
    exit 1
fi

ashome=$1
minikubehome=$ashome/tools/minikube
namespace=tiflash-autoscale
alias mkctl='minikube kubectl --'

cd $ashome
docker build -t autoscaler:minikube_test -f ./Dockerfile .

if mkctl get namespace "$namespace" > /dev/null 2>&1; then
    echo "namespace '$namespace' already exists"
else
    echo "namespace '$namespace' doesn't exist, creating..."
    mkctl create namespace $namespace
    echo "namespace '$namespace' created"
fi
mkctl apply -f $minikubehome/minikube_autoscale.yaml
mkdir -p /tmp/pv-tiflash-autoscale-prom/
mkctl apply -f $minikubehome/prom.yaml
