#!/usr/bin/env bash
# 在 105 上挂载本机 MindSpace（需 Mac 反向隧道 :19999 已建立）
set -euo pipefail

H5_DIR="${H5_DIR:-/root/tkmind_go/ui/h5}"
MOUNT_ROOT="${MOUNT_ROOT:-/mnt/memind-shared}"
TUNNEL_PORT="${MEMIND_TUNNEL_REMOTE_PORT:-19999}"

mkdir -p "${MOUNT_ROOT}/MindSpace" "${MOUNT_ROOT}/data/mindspace"
grep -q '^user_allow_other' /etc/fuse.conf 2>/dev/null || echo user_allow_other >> /etc/fuse.conf

if ! command -v rclone >/dev/null; then
  cd /tmp
  curl -fsSL -o rclone.zip https://downloads.rclone.org/rclone-current-linux-amd64.zip
  unzip -o rclone.zip
  cp rclone-*-linux-amd64/rclone /usr/local/bin/
  chmod +x /usr/local/bin/rclone
fi

mkdir -p /root/.config/rclone
cat >/root/.config/rclone/rclone.conf <<'EOF'
[memind-mac]
type = sftp
host = 127.0.0.1
port = 19999
user = john
key_file = /root/.ssh/id_ed25519
shell_type = unix
EOF

ssh-keyscan -p "${TUNNEL_PORT}" 127.0.0.1 >> /root/.ssh/known_hosts 2>/dev/null || true

for i in $(seq 1 30); do
  if ss -tln | grep -q ":${TUNNEL_PORT} "; then break; fi
  sleep 2
done
if ! ss -tln | grep -q ":${TUNNEL_PORT} "; then
  echo "错误: 本机反向隧道 ${TUNNEL_PORT} 未就绪，请先在 Mac 运行 scripts/install-memind-mac-tunnel.sh" >&2
  exit 1
fi

rclone lsd memind-mac:/Users/john/Project/Memind/MindSpace >/dev/null

if [[ -d "${H5_DIR}/MindSpace" && ! -L "${H5_DIR}/MindSpace" ]]; then
  mv "${H5_DIR}/MindSpace" "${H5_DIR}/MindSpace.bak.local-$(date +%Y%m%d)"
fi
if [[ -d "${H5_DIR}/data/mindspace" && ! -L "${H5_DIR}/data/mindspace" ]]; then
  mv "${H5_DIR}/data/mindspace" "${H5_DIR}/data/mindspace.bak.local-$(date +%Y%m%d)"
fi

cat >/etc/systemd/system/memind-shared-mindspace.mount.service <<EOF
[Unit]
Description=Rclone mount Mac MindSpace publish workspace
After=network-online.target
Wants=network-online.target

[Service]
Type=simple
ExecStartPre=/bin/bash -c 'until ss -tln | grep -q ":${TUNNEL_PORT} "; do sleep 2; done'
ExecStartPre=/bin/bash -c 'fusermount -uz ${MOUNT_ROOT}/MindSpace 2>/dev/null || true; mkdir -p ${MOUNT_ROOT}/MindSpace'
ExecStart=/usr/local/bin/rclone mount memind-mac:/Users/john/Project/Memind/MindSpace ${MOUNT_ROOT}/MindSpace --config /root/.config/rclone/rclone.conf --allow-other --allow-non-empty --vfs-cache-mode writes --vfs-write-back 0 --dir-cache-time 10s --poll-interval 5s --attr-timeout 1s
ExecStop=/bin/fusermount -uz ${MOUNT_ROOT}/MindSpace
Restart=always
RestartSec=5

[Install]
WantedBy=multi-user.target
EOF

cat >/etc/systemd/system/memind-shared-storage.mount.service <<EOF
[Unit]
Description=Rclone mount Mac MindSpace asset storage
After=memind-shared-mindspace.mount.service
Requires=memind-shared-mindspace.mount.service

[Service]
Type=simple
ExecStartPre=/bin/bash -c 'fusermount -uz ${MOUNT_ROOT}/data/mindspace 2>/dev/null || true; mkdir -p ${MOUNT_ROOT}/data/mindspace'
ExecStart=/usr/local/bin/rclone mount memind-mac:/Users/john/Project/Memind/data/mindspace ${MOUNT_ROOT}/data/mindspace --config /root/.config/rclone/rclone.conf --allow-other --allow-non-empty --vfs-cache-mode writes --vfs-write-back 0 --dir-cache-time 10s --poll-interval 5s --attr-timeout 1s
ExecStop=/bin/fusermount -uz ${MOUNT_ROOT}/data/mindspace
Restart=always
RestartSec=5

[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload
systemctl enable memind-shared-mindspace.mount.service memind-shared-storage.mount.service
systemctl restart memind-shared-mindspace.mount.service
sleep 2
systemctl restart memind-shared-storage.mount.service
sleep 2

ln -sfn "${MOUNT_ROOT}/MindSpace" "${H5_DIR}/MindSpace"
mkdir -p "${H5_DIR}/data"
ln -sfn "${MOUNT_ROOT}/data/mindspace" "${H5_DIR}/data/mindspace"

ENV_FILE="${H5_DIR}/.env"
if [[ -f "${ENV_FILE}" ]]; then
  if grep -q '^MINDSPACE_STORAGE_ROOT=' "${ENV_FILE}"; then
    sed -i "s|^MINDSPACE_STORAGE_ROOT=.*|MINDSPACE_STORAGE_ROOT=${MOUNT_ROOT}/data/mindspace|" "${ENV_FILE}"
  else
    echo "MINDSPACE_STORAGE_ROOT=${MOUNT_ROOT}/data/mindspace" >>"${ENV_FILE}"
  fi
  if grep -q '^MEMIND_SHARED_ROOT=' "${ENV_FILE}"; then
    sed -i "s|^MEMIND_SHARED_ROOT=.*|MEMIND_SHARED_ROOT=${MOUNT_ROOT}|" "${ENV_FILE}"
  else
    echo "MEMIND_SHARED_ROOT=${MOUNT_ROOT}" >>"${ENV_FILE}"
  fi
fi

systemctl restart goose-h5 2>/dev/null || true

echo "✅ 105 MindSpace 已挂载本机共享目录 ${MOUNT_ROOT}"
ls -la "${H5_DIR}/MindSpace" | head -5
