#!/bin/bash

if [[ "$PATH" != *"$SNAP_INSTANCE_NAME"* ]]; then
  export PATH="${PATH/\/snap\/wekan\//\/snap\/$SNAP_INSTANCE_NAME\/}"
fi

# FerretDB Control Script
# Starts FerretDB v1 (the wekan/FerretDB fork) with its embedded pure-Go SQLite
# backend, which speaks the MongoDB wire protocol. It is the MongoDB-compatible
# alternative database, selected with:  snap set wekan database=ferretdb
#
# Only one database runs at a time. This service starts FerretDB only when
# `database` is set to ferretdb; otherwise it disables itself (mirroring how
# mongodb-control handles DISABLE_MONGODB). The configure hook flips the two
# services when the `database` setting changes.

# get wekan/mongo settings
echo "Reading snap settings..."
source $SNAP/bin/wekan-read-settings

# Maintenance mode: if $SNAP_COMMON/.wekan-maintenance exists, do NOT self-disable; start
# FerretDB regardless of the `database` setting, so an admin can run FerretDB by hand
# (`snap start wekan.ferretdb`) to reach the data over the MongoDB wire protocol. Toggle with
# `snap run wekan.maintenance on|off`. FerretDB and MongoDB share port 27019 - run one at a time.
WEKAN_MAINTENANCE="$SNAP_COMMON/.wekan-maintenance"
if [ -f "$WEKAN_MAINTENANCE" ]; then
    echo "MAINTENANCE MODE (.wekan-maintenance present): starting FerretDB directly, ignoring the"
    echo "database setting. Stop with 'snap stop wekan.ferretdb'."
fi

# The `database` snap setting is AUTHORITATIVE (see wekan-control): FerretDB runs only when
# database=ferretdb. This lets an admin keep WeKan on MongoDB (`snap run wekan.database mongodb`)
# while a migration is being fixed, instead of being force-switched to FerretDB just because a
# SQLite file exists. A successful migration sets database=ferretdb itself (finish_success).
if [ ! -f "$WEKAN_MAINTENANCE" ] && [ "ferretdb" != "${DATABASE}" ]; then
    echo "database is '${DATABASE:-mongodb}', not 'ferretdb'. Disabling ferretdb service."
    snapctl stop --disable ${SNAP_NAME}.ferretdb 2>/dev/null || true
    exit 0
fi

# The FerretDB binary is shipped inside the WeKan bundle and staged to the snap
# root by the wekan part (bundle/ferretdb -> $SNAP/ferretdb).
FERRETDB_BIN="$SNAP/ferretdb"
if [ ! -x "$FERRETDB_BIN" ]; then
    echo "ERROR: FerretDB binary not found or not executable at $FERRETDB_BIN"
    exit 1
fi

# FerretDB binds the same address WeKan's MONGO_URL points at (wekan-control
# builds mongodb://$MONGODB_BIND_IP:$MONGODB_PORT/wekan), so no other setting
# needs to change to switch backends.
BIND_IP="${MONGODB_BIND_IP:-127.0.0.1}"
PORT="${MONGODB_PORT:-27019}"

# SQLite data lives next to attachments/avatars at $SNAP_COMMON/files/db (the
# "files/<name>" layout), so it survives snap refreshes and is where the
# MongoDB 3 → FerretDB migration (migration-control) wrote it.
SQLITE_DIR="${SNAP_COMMON}/files/db"
mkdir -p "$SQLITE_DIR"

# Telemetry off: --telemetry=disable both disables AND locks it (FerretDB won't
# let it be re-enabled). DO_NOT_TRACK/FERRETDB_TELEMETRY are belt-and-suspenders.
export DO_NOT_TRACK=1 FERRETDB_TELEMETRY=disable

echo "Starting FerretDB v1 (SQLite) on ${BIND_IP}:${PORT} ..."
echo "  SQLite data dir: ${SQLITE_DIR}"
exec "$FERRETDB_BIN" \
    --handler=sqlite \
    --sqlite-url="file:${SQLITE_DIR}/" \
    --listen-addr="${BIND_IP}:${PORT}" \
    --telemetry=disable \
    --log-level=error
