#!/bin/bash

# Category : system
# Description : Olivetin - Web Interface for Linux (c/u/s/r/i):

installOlivetin()
{
    if [[ "$olivetin" == *[cCtTuUsSrRiI]* ]]; then
        dockerConfigSetupToContainer silent olivetin;
        local app_name=$CFG_OLIVETIN_APP_NAME
		initializeAppVariables $app_name;
    fi

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

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

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

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

    if [[ "$olivetin" == *[iI]* ]]; then
        echo ""
        echo "##########################################"
        echo "###          Install $app_name"
        echo "##########################################"
        echo ""

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

        dockerConfigSetupToContainer "loud" "$app_name" "install";
        isSuccessful "Install 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;

		((menu_number++))
        echo ""
        echo "---- $menu_number. Setting up $app_name config.yaml"
        echo ""

        result=$(rm -rf "$containers_dir$app_name/config/config.yaml")
        checkSuccess "Removing old config.yaml file"

        local result=$(copyResource "$app_name" "config.yaml" "config")
        checkSuccess "Copying the config.yaml for $app_name"

        configSetupFileWithData $app_name "/config/config.yaml";

		((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. Setting up LibrePortal SSH Connectivity"
        echo ""

        if [[ $CFG_SSHKEY_LIBREPORTAL_ENABLED == "true" ]]; then
            local ed_ssh_file_path=$(find $ssh_dir/public -name "*libreportal.pub" -print -quit)

            local result=$(copyFile "loud" "$ed_ssh_file_path" "$containers_dir$app_name/ssh" "$docker_install_user")
            checkSuccess "Copying LibrePortal SSH Key to $app_name"

            dockerCommandRun "docker exec $app_name mkdir /opt/OliveTinSshKeys"
            checkSuccess "Creating SSK Key folder in docker container for $app_name"
        else
            dockerCommandRun "docker exec $app_name dnf install -y sshpass"
            checkSuccess "Installing SSHPass as not using SSH Keys for $app_name"

            # Access to CLI
            updateConfigOption "CFG_DOCKER_MANAGER_ENABLED" "true"
            installDockerManagerUser;
        fi

        dockerComposeRestart $app_name;

        ((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. 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
    olivetin=n
}
