#!/bin/bash

# Category : Development & Version Control
# Description : Prometheus - Monitoring and Alerting (c/u/s/r/i):

installPrometheus()
{
    local config_variables="$1"

    if [[ "$prometheus" == *[cCtTuUsSrRiI]* ]]; then
        dockerConfigSetupToContainer silent prometheus;
        local app_name=$CFG_PROMETHEUS_APP_NAME
		initializeAppVariables $app_name;
    fi

    if [[ "$prometheus" == *[cC]* ]]; then
        editAppConfig $app_name;
    fi

	if [[ "$prometheus" == *[uU]* ]]; then
		dockerUninstallApp $app_name;
	fi

	if [[ "$prometheus" == *[sS]* ]]; then
		dockerComposeDown $app_name;
	fi

	if [[ "$prometheus" == *[rR]* ]]; then
        dockerComposeRestart $app_name;
	fi

    if [[ "$prometheus" == *[iI]* ]]; then
        isHeader "Install $app_name"

		((menu_number++))
        echo ""
        echo "---- $menu_number. Setting up install folder and config file for $app_name."
        echo ""

        dockerConfigSetupToContainer "loud" "$app_name" "install" "$config_variables";
        isSuccessful "Install folders and Config files have been setup for $app_name."

        ((menu_number++))
        echo ""


		((menu_number++))
        echo ""
        echo "---- $menu_number. Setting up the $app_name docker-compose.yml file."
        echo ""

        dockerComposeSetupFile $app_name;

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

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

		local 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."

		((menu_number++))
        echo ""
        echo "---- $menu_number. Updating file permissions before starting."
        echo ""

		fixPermissionsBeforeStart $app_name;

		((menu_number++))
        echo ""
        echo "---- $menu_number. Running the docker-compose.yml to install and start $app_name"
        echo ""

		dockerComposeUpdateAndStartApp $app_name install;

        # Prometheus
        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=$(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=$(sudo chmod -R 777 "${containers_dir}prometheus/prom_data")
            checkSuccess "Set permissions to prom_data folder."
        fi

        ((menu_number++))
        echo ""
        echo "---- $menu_number. Running Application specific updates (if required)"
        echo ""

        appUpdateSpecifics $app_name;

		((menu_number++))
        echo ""
        echo "---- $menu_number. Running Headscale setup (if required)"
        echo ""

		setupHeadscale $app_name;

		((menu_number++))
		echo ""
        echo "---- $menu_number. Adding $app_name to the Apps Database table."
        echo ""

		databaseInstallApp $app_name;

		((menu_number++))
		echo ""
		echo "---- $menu_number. Updating WebUI config file."
		echo ""

		webuiContainerSetup $app_name install;

		((menu_number++))
		echo ""
		echo "---- $menu_number. Gathering monitoring scrape targets from installed apps."
		echo ""

		# Re-gather every monitoring-enabled app's scrape fragment into
		# scrape.d/ — so a fresh (or re-)install of Prometheus picks up the
		# apps that already had CFG_<APP>_MONITORING=true.
		monitoringRefreshPrometheus;

		((menu_number++))
		echo ""
		echo "---- $menu_number. You can find $app_name files at $containers_dir$app_name"
        echo ""
        echo "    You can now navigate to your $app_name service using any of the options below : "
        echo ""

        menuShowFinalMessages $app_name;

		menu_number=0
        #sleep 3s
        cd
    fi
    prometheus=n
}
