#!/bin/bash

# Category : Cloud Storage & File Sharing
# Description : Nextcloud Record - Talk Recording Backend (c/u/s/r/i):

installNextcloud_record()
{
    local config_variables="$1"

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

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

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

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

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

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

		((menu_number++))
        echo ""
        echo "---- $menu_number. Setting up install folders 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 RECORDING_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 INTERNAL_SECRET in docker-compose.yml."

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

        local hpb_domain=""
        while [[ -z "$hpb_domain" ]]; do
            isQuestion "Enter your High-Performance Backend domain e.g (talk.yourdomain.com): "
            read -p "" hpb_domain
            if [[ -z "$hpb_domain" ]]; then
                echo "Please enter a valid domain."
            fi
        done
        result=$(sudo sed -i "s|HPBDOMAINHERE|${hpb_domain}|" "$containers_dir$app_name/docker-compose.yml")
        checkSuccess "Updating the HPB_DOMAIN 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 ""

        echo "";
        echo "    RECORDING_SECRET=$random_secret_1"
        echo "    INTERNAL_SECRET=$random_secret_2"
        echo "";

        menuShowFinalMessages $app_name;

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