#!/bin/bash

# Category : Cloud Storage & File Sharing
# Description : Nextcloud HPB - High-Performance Backend (c/u/s/r/i):

installNextcloud_hpb()
{
    local config_variables="$1"

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

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

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

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

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

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

		((menu_number++))
        echo ""
        echo "---- $menu_number. Setting up install Nextcloud HPD 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 random_secret_1=$(openssl rand -hex 32)
        result=$(sudo sed -i "s|SECRET1HERE|${random_secret_1}|" "$containers_dir$app_name/docker-compose.yml")
        checkSuccess "Updating the TURN_SECRET in docker-compose.yml."
        local random_secret_2=$(openssl rand -hex 32)
        result=$(sudo sed -i "s|SECRET2HERE|${random_secret_2}|" "$containers_dir$app_name/docker-compose.yml")
        checkSuccess "Updating the SIGNALING_SECRET in docker-compose.yml."
        local random_secret_3=$(openssl rand -hex 32)
        result=$(sudo sed -i "s|SECRET3HERE|${random_secret_3}|" "$containers_dir$app_name/docker-compose.yml")
        checkSuccess "Updating the INTERNAL_SECRET in docker-compose.yml."

        # Ask user for nextcloud domain
        local nextcloud_domain=""
        while [[ -z "$nextcloud_domain" ]]; do
            isQuestion "Enter your Nextcloud domain e.g (nextcloud.yourdomain.com): "
            read -p "" nextcloud_domain
            if [[ -z "$nextcloud_domain" ]]; then
                echo "Please enter a valid domain."
            fi
        done
        result=$(sudo sed -i "s|NEXTCLOUDDOMAINHERE|${nextcloud_domain}|" "$containers_dir$app_name/docker-compose.yml")
        checkSuccess "Updating the NEXTCLOUD_URL in 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 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 ""

        # Lets output the nextcloud talk config based on the scructure below
        #echo ""
        #echo "Please add the following to your Nextcloud config.php file :"
        #echo ""
        #echo "    'talk' => ["
        #echo "        'hpb' => ["
        #echo "            'url' => 'http://$host_setup:$usedport3',"
        #echo "            'jwt_secret' => '$random_secret_1',"
        #echo "            'jwt_issuer' => '$random_secret_2',"
        #echo "            'jwt_audience' => '$random_secret_3',"
        #echo "        ],"
        #echo "    ],"
        #echo "";

        echo "";
        echo "    TURN_SECRET=$random_secret_1"
        echo "    SIGNALING_SECRET=$random_secret_2"
        echo "    INTERNAL_SECRET=$random_secret_3"
        echo "    TALK_PORT=$usedport1"
        echo "";

        menuShowFinalMessages $app_name;

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