#!/bin/bash

# Jitsi Meet install hooks — Jitsi ships its docker layout as a tagged
# release zip on GitHub, so we download + unpack it before compose setup.
# Then mass-edit the .env, generate passwords, and rewire the nginx ports.

jitsimeet_install_post_setup()
{
    local app_name="$1"
    local git_url="$CFG_JITSIMEET_GIT"

    ((menu_number++))
    echo ""
    echo "---- $menu_number. Downloading latest GitHub release"
    echo ""

    local latest_tag
    latest_tag=$(git ls-remote --refs --sort="version:refname" --tags "$git_url" | cut -d/ -f3- | tail -n1)
    echo "The latest tag is: $latest_tag"

    local result
    result=$(createFolders "loud" $docker_install_user $containers_dir$app_name)
    checkSuccess "Creating $app_name container installation folder"
    result=$(cd $containers_dir$app_name && runFileOp rm -rf $containers_dir$app_name/$latest_tag.zip)
    checkSuccess "Deleting zip file to prevent conflicts"
    result=$(createTouch $containers_dir$app_name/$latest_tag.txt $docker_install_user && echo "Installed \"$latest_tag\" on \"$backupDate\"!" > $latest_tag.txt)
    checkSuccess "Create logging txt file"

    result=$(runFileOp wget -O $containers_dir$app_name/$latest_tag.zip $git_url/archive/refs/tags/$latest_tag.zip)
    checkSuccess "Downloading tagged zip file from GitHub"
    result=$(runFileOp unzip -o $containers_dir$app_name/$latest_tag.zip -d $containers_dir$app_name)
    checkSuccess "Unzip downloaded file"
    result=$(runFileOp mv $containers_dir$app_name/docker-jitsi-meet-$latest_tag/* $containers_dir$app_name)
    checkSuccess "Moving all files from zip file to install directory"
    result=$(runFileOp rm -rf $containers_dir$app_name/$latest_tag.zip && runFileOp rm -rf $containers_dir$app_name/$latest_tag/)
    checkSuccess "Removing downloaded zip file as no longer needed"
}

jitsimeet_install_post_compose()
{
    local app_name="$1"

    ((menu_number++))
    echo ""
    echo "---- $menu_number. Setting up .env file for setup"
    echo ""

    dockerSetupEnvFile

    local result
    result=$(runFileOp sed -i "s|CONFIG=~/.jitsi-meet-cfg|CONFIG=$containers_dir$app_name/.jitsi-meet-cfg|g" $containers_dir$app_name/.env)
    checkSuccess "Updating .env file with new install path"

    result=$(runFileOp sed -i "s|#PUBLIC_URL=https://meet.example.com|PUBLIC_URL=https://$host_setup|g" $containers_dir$app_name/.env)
    checkSuccess "Updating .env file with Public URL to $host_setup"

    result=$(runFileOp sed -i "s|HTTP_PORT=8000|HTTP_PORT=$usedport1|g" $containers_dir$app_name/.env)
    checkSuccess "Updating .env file with HTTP_PORT to $usedport1"

    result=$(runFileOp sed -i "s|HTTPS_PORT=8443|HTTPS_PORT=$usedport2|g" $containers_dir$app_name/.env)
    checkSuccess "Updating .env file with HTTP_PORT to $usedport2"

    # Defaults missing from the shipped .env (see jitsi/docker-jitsi-meet
    # commit 12051700562d…). Append them here so the install boots.
    result=$(echo "XMPP_DOMAIN=meet.jitsi" | runFileWrite -a "$containers_dir$app_name/.env")
    checkSuccess "Updating .env file with missing option : XMPP_DOMAIN"

    result=$(echo "XMPP_SERVER=xmpp.meet.jitsi" | runFileWrite -a "$containers_dir$app_name/.env")
    checkSuccess "Updating .env file with missing option : XMPP_SERVER"

    result=$(echo "JVB_PORT=$usedport4" | runFileWrite -a "$containers_dir$app_name/.env")
    checkSuccess "Updating .env file with missing option : JVB_PORT"

    result=$(echo "JVB_TCP_MAPPED_PORT=$usedport5" | runFileWrite -a "$containers_dir$app_name/.env")
    checkSuccess "Updating .env file with missing option : JVB_TCP_MAPPED_PORT"

    result=$(echo "JVB_TCP_PORT=$usedport5" | runFileWrite -a "$containers_dir$app_name/.env")
    checkSuccess "Updating .env file with missing option : JVB_TCP_PORT"

    result=$(cd "$containers_dir$app_name" && runFileOp ./gen-passwords.sh)
    checkSuccess "Running Jitsi Meet gen-passwords.sh script"
}

jitsimeet_install_post_start()
{
    local app_name="$1"

    ((menu_number++))
    echo ""
    echo "---- $menu_number. Adjusting $app_name docker system files for port changes."
    echo ""

    local result
    result=$(runFileOp sed -i "s|80|$usedport1|g" $containers_dir$app_name/web/rootfs/defaults/default)
    checkSuccess "Updating NGINX default site port 80 to $usedport1"

    result=$(runFileOp sed -i "s|443|$usedport2|g" $containers_dir$app_name/web/rootfs/defaults/default)
    checkSuccess "Updating NGINX default site port 443 to $usedport2"

    dockerComposeRestart $app_name
}
