#!/bin/bash

# Trilium install hooks — wait for first-boot config.ini, retune port, restart.

trilium_install_post_start()
{
    local app_name="$1"

    ((menu_number++))
    echo ""
    echo "---- $menu_number. Updating default port and restarting $app_name"
    echo ""

    local trilium_timeout=10
    local trilium_counter=0
    while [ ! -f "$containers_dir$app_name/trilium-data/config.ini" ]; do
        if [ "$trilium_counter" -ge "$trilium_timeout" ]; then
            isNotice "File not found after 10 seconds. Exiting..."
            return 0
        fi
        isNotice "Waiting for the file to appear..."
        read -t 1
        trilium_counter=$((trilium_counter + 1))
    done

    local result
    result=$(runFileOp sed -i "s|port=8080|port=$usedport1|g" "$containers_dir$app_name/trilium-data/config.ini")
    checkSuccess "Configured $app_name from default 8080 to $usedport1"

    dockerComposeRestart $app_name
}
