#!/bin/bash    

# the directory of the script
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

# the temp directory used, within $DIR
# omit the -p parameter to create a temporal directory in the default location
WORK_DIR=`mktemp -d -p "$DIR"`

# check if tmp dir was created
if [[ ! "$WORK_DIR" || ! -d "$WORK_DIR" ]]; then
  echo "Could not create temp dir"
  exit 1
fi

# deletes the temp directory
function cleanup {      
  rm -rf "$WORK_DIR"
  echo "Deleted temp working directory $WORK_DIR"
}

# register the cleanup function to be called on the EXIT signal
trap cleanup EXIT

# implementation of script starts here
KUBE_PROM_VERSION=release-0.13

cd $WORK_DIR
git clone https://github.com/prometheus-operator/kube-prometheus.git
cd kube-prometheus
git checkout $KUBE_PROM_VERSION
rm manifests/alertmanager-secret.yaml
for y in manifests/setup/*.yaml; do
	yq -y '.metadata.annotations += {"argocd.argoproj.io/sync-wave":"0"}' $y > $DIR/apps-kustomized/kube-prometheus/`basename $y`
done
for y in manifests/*.yaml; do
	yq -y '.metadata.annotations += {"argocd.argoproj.io/sync-wave":"1"}' $y > $DIR/apps-kustomized/kube-prometheus/`basename $y`
done