#!/bin/bash
set -eu

STATE="$SNAP_COMMON/.startup-network.env"
LOCK="$SNAP_COMMON/.startup-network.lock"

if [ ! -s "$STATE" ]; then
    lock_owned=false
    waited=0
    while true; do
        if mkdir "$LOCK" 2>/dev/null; then
            printf '%s\n' "$$" > "$LOCK/pid"
            lock_owned=true
            break
        fi
        [ -s "$STATE" ] && break

        # A service can be killed after mkdir and before the atomic state-file
        # rename. The old empty lock then made every future invocation sleep
        # forever: snap called the shell wrapper "active", but FerretDB never
        # started and nothing listened on mongodb-port. New locks name their
        # owner; reclaim one whose process is gone. Locks made by an older snap
        # have no pid file, so reclaim those after a short grace period.
        owner="$(cat "$LOCK/pid" 2>/dev/null || true)"
        case "$owner" in
            '' )
                if [ "$waited" -ge 5 ]; then
                    echo "startup-network: removing stale legacy lock with no owner" >&2
                    rmdir "$LOCK" 2>/dev/null || true
                    waited=0
                    continue
                fi
                ;;
            *[!0-9]* ) ;;
            * )
                if ! kill -0 "$owner" 2>/dev/null; then
                    echo "startup-network: removing stale lock from process $owner" >&2
                    rm -f "$LOCK/pid"
                    rmdir "$LOCK" 2>/dev/null || true
                    waited=0
                    continue
                fi
                ;;
        esac
        waited=$((waited + 1))
        if [ "$waited" -ge 30 ]; then
            echo "startup-network: endpoint lock is still held by active process ${owner:-unknown}; refusing to wait forever" >&2
            return 1 2>/dev/null || exit 1
        fi
        sleep 1
    done
    if [ ! -s "$STATE" ]; then
        explicit_root="$(snapctl get root-url 2>/dev/null || true)"
        explicit_port="$(snapctl get port 2>/dev/null || true)"
        explicit_db_port="$(snapctl get mongodb-port 2>/dev/null || true)"
        tmp="$STATE.$$"
        if ! (
            [ -n "$explicit_root" ] && export ROOT_URL="$explicit_root" || unset ROOT_URL
            [ -n "$explicit_port" ] && export PORT="$explicit_port" || unset PORT
            if [ -n "$explicit_db_port" ]; then
                export FERRETDB_LISTEN_ADDR="127.0.0.1:$explicit_db_port"
            else
                unset FERRETDB_LISTEN_ADDR
            fi
            export WEKAN_PORT_START=80 WEKAN_FERRETDB_PORT_START=27019
            "$SNAP/bin/node" "$SNAP/bin/startup-network.cjs" posix --ferretdb
        ) > "$tmp" || ! mv -f "$tmp" "$STATE"; then
            rm -f "$tmp"
            [ "$lock_owned" = true ] && rm -f "$LOCK/pid"
            [ "$lock_owned" = true ] && rmdir "$LOCK" 2>/dev/null || true
            return 1 2>/dev/null || exit 1
        fi
    fi
    [ "$lock_owned" = true ] && rm -f "$LOCK/pid"
    [ "$lock_owned" = true ] && rmdir "$LOCK" 2>/dev/null || true
fi

. "$STATE"
export PORT ROOT_URL FERRETDB_LISTEN_ADDR MONGO_URL

# The shared ports stay stable while the two services are running, but the
# active Ethernet/WLAN address can change between starts. Refresh only the web
# URL on every service invocation; an explicit snap setting still wins.
explicit_root="$(snapctl get root-url 2>/dev/null || true)"
if [ -n "$explicit_root" ]; then
    ROOT_URL="$explicit_root"
else
    unset ROOT_URL
    eval "$(PORT="$PORT" "$SNAP/bin/node" "$SNAP/bin/startup-network.cjs" posix)"
fi
export ROOT_URL
