#!/usr/bin/env bash
# Copyright 2024 PingCAP, Inc.
#
# 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.

# set -e

tag=${1:-master}
dst=${2:-.}
tmp="./tmp"

if [ $next_gen ]; then
    echo "NEXT_GEN mode enabled."
fi

mkdir -p "$dst"
mkdir -p "$tmp"

# Function to append [NextGen] to dashboard titles
append_nextgen_title() {
    local file="$1"
    if [ -f "$file" ]; then
        # Use a temporary file for the modification
        local tmpfile="${file}.tmp"
        jq '.title = .title + "-X" | .tags = .tags + ["tidb/x"] | .uid = .uid + "NG"' "$file" > "$tmpfile" && mv "$tmpfile" "$file"
        mv $file "${file%.json}.next_gen.json"
    fi
}

git clone --depth 1 -b "$tag" https://github.com/pingcap/tidb.git "$tmp/tidb"
if [ $next_gen ]; then
    cp "$tmp/tidb/pkg/metrics/nextgengrafana/"*.json "$dst/"
    # Append [NextGen] to titles of nextgen dashboards
    for file in "$tmp/tidb/pkg/metrics/nextgengrafana/"*.json; do
        basename_file=$(basename "$file")
        append_nextgen_title "$dst/$basename_file"
    done
fi
cp "$tmp/tidb/pkg/metrics/grafana/"*.json "$dst/"
cp "$tmp/tidb/br/metrics/grafana/"*.json "$dst/"

git clone --depth 1 -b "$tag" https://github.com/tikv/pd.git "$tmp/pd"
cp "$tmp/pd/metrics/grafana/"*.json "$dst/"

if [ $next_gen ]; then
    git clone --depth 1 -b "$tag" https://github.com/tidbcloud/cloud-storage-engine.git "$tmp/cse" --branch 'dedicated'
    cp "$tmp/cse/metrics/grafana/"*.json "$dst/"
    # Append [NextGen] to titles of CSE dashboards
    for file in "$tmp/cse/metrics/grafana/"*.json; do
        basename_file=$(basename "$file")
        append_nextgen_title "$dst/$basename_file"
    done
fi

git clone --depth 1 -b "$tag" https://github.com/tikv/tikv.git "$tmp/tikv"
cp "$tmp/tikv/metrics/grafana/"*.json "$dst/"

git clone --depth 1 -b "$tag" https://github.com/pingcap/tiflash.git "$tmp/tiflash"
cp "$tmp/tiflash/metrics/grafana/"*.json "$dst/"

git clone --depth 1 -b "$tag" https://github.com/pingcap/tiflow.git "$tmp/tiflow"
cp "$tmp/tiflow/metrics/grafana/"*.json "$dst/"

tiproxy_branch="$tag"
if [[ "$tag" == "master" ]]; then
    tiproxy_branch="main"
fi
git clone --depth 1 -b "$tiproxy_branch" https://github.com/pingcap/TiProxy.git "$tmp/TiProxy"
cp "$tmp/TiProxy/pkg/metrics/grafana/tiproxy_summary.json" "$dst/"

git clone --depth 1 -b "$tag" https://github.com/pingcap/ticdc.git "$tmp/ticdc"
cp "$tmp/ticdc/metrics/grafana/"*.json "$dst/"

rm -rf "$tmp"