#!/bin/bash

# Prometheus install hooks — drop the prometheus.yml template alongside the
# compose, and 0777 the storage dirs so the container can write its TSDB
# regardless of the host UID mapping.

prometheus_install_post_compose()
{
    local app_name="$1"

    local result
    result=$(createFolders "loud" $docker_install_user "$containers_dir$app_name/$app_name")
    checkSuccess "Created $app_name folder in $app_name"

    result=$(createTouch "$containers_dir$app_name/$app_name/$app_name.yml" $docker_install_user)
    checkSuccess "Created $app_name.yml file for $app_name"

    result=$(copyResource "$app_name" "$app_name.yml" "$app_name" | runInstallWrite -a "$logs_dir/$docker_log_file" 2>&1)
    checkSuccess "Copying $app_name.yml to containers folder."
}

prometheus_install_post_start()
{
    local app_name="$1"

    if [ -f "${containers_dir}prometheus/prometheus/prometheus.yml" ]; then
        updateFileOwnership "${containers_dir}prometheus/prometheus/prometheus.yml" $docker_install_user $docker_install_user
    fi
    if [ -d "${containers_dir}prometheus/prometheus" ]; then
        local result
        result=$(sudo chmod -R 777 "${containers_dir}prometheus/prometheus")
        checkSuccess "Set permissions to prometheus folder."
    fi
    if [ -d "${containers_dir}prometheus/prom_data" ]; then
        local result
        result=$(sudo chmod -R 777 "${containers_dir}prometheus/prom_data")
        checkSuccess "Set permissions to prom_data folder."
    fi
}
