#!/usr/bin/env bash
set -euo pipefail

# This scripts checks the environment file for placeholder values and replaces them with generated keys or system variables.

APP_DIR="$(readlink -f "$(dirname "${BASH_SOURCE[0]}")/..")"
APP_DATA_DIR="$APP_DIR/data"
DESIRED_OWNER="1000:1000"
ENV_FILE="$APP_DIR/settings.env"

# Check if the file exists
if [ ! -f "$ENV_FILE" ]; then
    echo "Error: Environment file not found at $ENV_FILE"
    exit 1
fi

# Check if the FERNET_KEY value is set to "changeme"
if grep -Eq '^FERNET_KEY= *changeme$' "$ENV_FILE"; then
    echo "Placeholder value for FERNET_KEY detected. Generating a new key..."

    # Generate new FERNET key
    FERNET_KEY=$(python3 -c "from cryptography.fernet import Fernet; print(Fernet.generate_key().decode())")

    # Replace the placeholder with the new key
    sed -i -E 's|^FERNET_KEY= *changeme$|FERNET_KEY="'"$FERNET_KEY"'"|' "$ENV_FILE"

    echo "Updated FERNET_KEY in $ENV_FILE"
else
    echo "FERNET_KEY is already set. No changes made."
fi

# Avahi may advertise umbrel-2.local after a name collision while the kernel
# hostname (and DEVICE_DOMAIN_NAME) remains umbrel. Use the reachable LAN name.
avahi_hostname=$(busctl --timeout=2 call org.freedesktop.Avahi / org.freedesktop.Avahi.Server GetHostNameFqdn 2>/dev/null |
    sed -n 's/^s "\([a-zA-Z0-9.-]*\)"$/\1/p') || avahi_hostname=""
avahi_hostname="${avahi_hostname%.}"

# Production authentication requires HTTPS. Follow changes to Umbrel-generated
# LAN names while preserving custom hostnames, ports, and other settings.
python3 - "$ENV_FILE" "$DEVICE_DOMAIN_NAME" "$avahi_hostname" "${APP_DATA_DIR}/.endurain-host" <<'PYTHON'
from pathlib import Path
import re
import sys

path = Path(sys.argv[1])
# Older packages did not record the generated address. Recognize the device's
# default name and Avahi collision suffixes, e.g. umbrel.local / umbrel-5.local.
device_name = sys.argv[2].removesuffix(".local")
legacy_host = re.compile(rf"https?://{re.escape(device_name)}(?:-[0-9]+)?\.local:8865/?")
local_host = f"https://{sys.argv[3] or sys.argv[2]}:8865"
state_path = Path(sys.argv[4])
previous_host = state_path.read_text().strip() if state_path.is_file() else ""
managed_host = ""

def migrate_host(match):
    global managed_host
    prefix, quote, value, suffix = match.groups()
    if value == "changeme" or value == previous_host or legacy_host.fullmatch(value):
        if value == "changeme":
            value, quote = local_host, quote or '"'
        elif sys.argv[3]:
            value = local_host
        # If Avahi is temporarily unavailable, retain the last known LAN name.
        managed_host = value.replace("http://", "https://", 1)
        value = managed_host
    elif value.startswith("http://"):
        value = "https://" + value[len("http://"):]
    return f"{prefix}{quote}{value}{quote}{suffix}"

original = path.read_text()
updated = re.sub(r"^(ENDURAIN_HOST=[ \t]*)([\"']?)([^\"'\s]+)\2([^\r\n]*)$",
                 migrate_host, original, flags=re.MULTILINE)
if updated != original:
    path.write_text(updated)
    print("Updated Endurain's HTTPS address.")
if managed_host and managed_host != previous_host:
    state_path.parent.mkdir(parents=True, exist_ok=True)
    state_path.write_text(managed_host + "\n")
PYTHON

# Updates do not copy data directories from the package. Prepare Redis storage
# for the user in the pinned Redis image before the new sidecar starts.
mkdir -p "${APP_DATA_DIR}/redis"
chown 999:1000 "${APP_DATA_DIR}/redis"

set_correct_permissions() {
	local -r path="${1}"

	# Check if the path exists (file or directory)
	if [[ -e "${path}" ]]; then
		owner=$(stat -c "%u:%g" "${path}")

		if [[ "${owner}" != "${DESIRED_OWNER}" ]]; then
			chown "${DESIRED_OWNER}" "${path}"
			echo "Changed ownership of ${path} to ${DESIRED_OWNER}"
		fi
	else
		echo "Skipping ${path} - path does not exist"
	fi
}

# Handle directory restructuring for version 0.13.0
restructure_directories() {
	local -r app_dir="${APP_DATA_DIR}/app"
	
	# Check if we need to do the restructuring
	if [[ -d "${app_dir}/files" && ! -d "${app_dir}/activity_files" ]]; then
		echo "Restructuring directories for Endurain v0.13.0..."
		
		# Rename files directory to activity_files
		if mv "${app_dir}/files" "${app_dir}/activity_files" 2>/dev/null; then
			echo "Renamed ${app_dir}/files to ${app_dir}/activity_files"
		else
			echo "Failed to rename ${app_dir}/files to ${app_dir}/activity_files"
		fi
	fi
	
	# Create new directories if they don't exist
	mkdir -p "${app_dir}/activity_media"
	echo "Created ${app_dir}/activity_media directory"
	
	mkdir -p "${app_dir}/server_images"
	echo "Created ${app_dir}/server_images directory"
	
	# Move logs directory from app/logs to logs (at root level)
	if [[ -d "${app_dir}/logs" && ! -d "${APP_DATA_DIR}/logs" ]]; then
		if mv "${app_dir}/logs" "${APP_DATA_DIR}/logs" 2>/dev/null; then
			echo "Moved ${app_dir}/logs to ${APP_DATA_DIR}/logs"
		else
			echo "Failed to move ${app_dir}/logs to ${APP_DATA_DIR}/logs"
		fi
	elif [[ ! -d "${APP_DATA_DIR}/logs" ]]; then
		# Create logs directory if it doesn't exist
		mkdir -p "${APP_DATA_DIR}/logs"
		echo "Created ${APP_DATA_DIR}/logs directory"
	fi
}

# Perform directory restructuring
if [[ -d "${APP_DATA_DIR}/app" ]]; then
	restructure_directories
fi

# Set permissions recursively on the entire /app directory and its subdirectories
if [[ -d "${APP_DATA_DIR}/app" ]]; then
	chown -R "${DESIRED_OWNER}" "${APP_DATA_DIR}/app"
	echo "Set recursive ownership of ${APP_DATA_DIR}/app to ${DESIRED_OWNER}"
fi

# Set permissions recursively on the entire /logs directory and its subdirectories
if [[ -d "${APP_DATA_DIR}/logs" ]]; then
	chown -R "${DESIRED_OWNER}" "${APP_DATA_DIR}/logs"
	echo "Set recursive ownership of ${APP_DATA_DIR}/logs to ${DESIRED_OWNER}"
fi

# Endurain 0.21 only starts on a database whose 0.20 data migrations have all
# finished, and refuses otherwise. Installs that skipped 0.20 (the store shipped
# 0.19.x until October 2026) would be stuck, so run 0.20.2 once to finish them.
# This is best effort: if it fails, 0.21 still refuses safely and the next start
# retries. Fresh installs and installs that are already migrated skip it.
BRIDGE_IMAGE="ghcr.io/endurain-project/endurain:v0.20.2@sha256:045af8835f82df64203b4e025baa2c603512da9c2e17babd0044533452cc20d6"
BRIDGE_CONTAINER="endurain_v020_bridge"
BRIDGE_DONE_FILE="${APP_DATA_DIR}/.v020-migrations-done"

# Prints how many 0.20 data migrations are still pending (0 when there are no
# users yet, or when 0.21 has already retired the migrations table).
pending_migrations() {
	local result
	result=$(docker exec endurain_db_1 psql -U endurain -d endurain -tAc \
		"SELECT CASE WHEN NOT EXISTS (SELECT 1 FROM users) THEN 0 ELSE 9 - (SELECT count(*) FROM migrations WHERE executed AND id IN (1,2,3,4,5,6,8,9,10)) END" 2>/dev/null) || result=0
	[[ "${result}" =~ ^[0-9]+$ ]] || result=0
	echo "${result}"
}

bridge_v020() {
	"${UMBREL_ROOT}/scripts/app" compose "${APP_ID}" up --detach --wait db || return 1
	[[ "$(pending_migrations)" -gt 0 ]] || return 0

	echo "Finishing Endurain 0.20 data migrations before starting 0.21. This can take a while on large libraries..."
	local network bridge_env
	network=$(docker inspect -f '{{range $name, $_ := .NetworkSettings.Networks}}{{$name}}{{end}}' endurain_db_1) || return 1
	bridge_env=$(mktemp)
	# docker run keeps quotes that Compose strips from settings.env, so strip them here.
	sed -E "s/^([A-Za-z_][A-Za-z0-9_]*)=[[:space:]]*([\"']?)(.*)\2[[:space:]]*$/\1=\3/" "${ENV_FILE}" > "${bridge_env}"
	docker rm -f "${BRIDGE_CONTAINER}" >/dev/null 2>&1 || true
	docker run --detach --name "${BRIDGE_CONTAINER}" --network "${network}" \
		--env-file "${bridge_env}" \
		-e DB_TYPE=postgres -e DB_HOST=endurain_db_1 -e DB_PORT=5432 -e DB_PASSWORD=endurain \
		-e SECRET_KEY="${APP_PASSWORD}" -e BEHIND_PROXY=true -e DEPLOYMENT_PROFILE=local -e WEB_WORKERS=1 \
		-v "${APP_DATA_DIR}/app:/app/backend/data" -v "${APP_DATA_DIR}/logs:/app/backend/logs" \
		"${BRIDGE_IMAGE}" >/dev/null
	local status=$?
	rm -f "${bridge_env}"
	[[ ${status} -eq 0 ]] || return 1

	# Wait up to 2 hours, or until 0.20 stops on its own.
	for _ in $(seq 1 720); do
		[[ "$(pending_migrations)" -eq 0 ]] && break
		[[ "$(docker inspect -f '{{.State.Running}}' "${BRIDGE_CONTAINER}" 2>/dev/null)" == "true" ]] || break
		sleep 10
	done
	docker logs --tail 20 "${BRIDGE_CONTAINER}" 2>&1 || true
	docker rm -f "${BRIDGE_CONTAINER}" >/dev/null 2>&1 || true
	[[ "$(pending_migrations)" -eq 0 ]]
}

if [[ ! -f "${BRIDGE_DONE_FILE}" ]]; then
	if [[ ! -d "${APP_DATA_DIR}/db/pgdata" ]]; then
		touch "${BRIDGE_DONE_FILE}"
	elif bridge_v020; then
		echo "Endurain 0.20 data migrations are complete."
		touch "${BRIDGE_DONE_FILE}"
	else
		echo "Could not finish the Endurain 0.20 data migrations. Endurain 0.21 will refuse to start; restart the app to retry."
	fi
fi

exit 0
