#!/bin/bash

# Category : Networking
# Description : Gluetun - VPN client for routing other containers (c/u/s/r/i):

installGluetun()
{
    local config_variables="$1"

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

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

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

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

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

    if [[ "$gluetun" == *[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 ""
        echo "---- $menu_number. Setting up the $app_name docker-compose.yml file."
        echo ""

        dockerComposeSetupFile $app_name;

        monitoringToggleAppConfig "$app_name" "docker-compose.yml";

        ((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;

        ((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 the WebUI config file."
        echo ""

        webuiContainerSetup $app_name install;

        ((menu_number++))
        echo ""
        echo "---- $menu_number. Refreshing Gluetun provider snapshot."
        echo ""

        appWebuiRefresh_gluetun;

        ((menu_number++))
        echo ""
        echo "---- $menu_number. Re-attaching gluetun-routed apps (post-recreate)."
        echo ""

        # Gluetun was just (re)created — every existing routed app holds a
        # stale container ID in its network_mode. Reattach them now so the
        # user doesn't have to chase silent netns drift later.
        appGluetunRoutedRecreate

        ((menu_number++))
        echo ""
        echo "---- $menu_number. Routing existing apps through Gluetun (optional)."
        echo ""

        gluetunRouteExistingAppsPrompt;

		((menu_number++))
		echo ""
		echo "---- $menu_number. Refreshing monitoring integration."
		echo ""

		monitoringRefreshAll;

        ((menu_number++))
        echo ""
        echo "---- $menu_number. You can find $app_name files at $containers_dir$app_name"
        echo ""

        menuShowFinalMessages "$app_name";

        menu_number=0
        cd
    fi
    gluetun=n
}
