#!/bin/bash
# =============================================================================
# Step 0: Export kgrag migration data from the SOURCE host's kgrag container.
# Run this script on the SOURCE host, for example 192.168.0.164.
# It only creates a tar.gz package and copies it out of the container.
# It does not modify Neo4j, files, or application data.
# =============================================================================

set -euo pipefail

KGRAG_CONTAINER="${KGRAG_CONTAINER:-kgrag}"
CONTAINER_APP_DIR="${CONTAINER_APP_DIR:-/app}"
CONTAINER_EXPORT_DIR="${CONTAINER_EXPORT_DIR:-/tmp/kgrag_export}"
HOST_EXPORT_DIR="${HOST_EXPORT_DIR:-/home/workspace/kgrag_export}"
EXPORT_NAME="${EXPORT_NAME:-kgrag_data_$(date +%Y%m%d%H%M%S).tar.gz}"

ITEMS=(
  files
  kg_output
  kg_snapshot.json
  fault_wenjian
  entity_registry.json
  tree_data.json
)

echo "=========================================="
echo "  kgrag source export"
echo "  container: ${KGRAG_CONTAINER}"
echo "  app dir:   ${CONTAINER_APP_DIR}"
echo "  host out:  ${HOST_EXPORT_DIR}"
echo "  package:   ${EXPORT_NAME}"
echo "=========================================="

if ! docker inspect "${KGRAG_CONTAINER}" >/dev/null 2>&1; then
  echo "ERROR: container not found: ${KGRAG_CONTAINER}" >&2
  echo "Run: docker ps | grep kgrag" >&2
  exit 1
fi

mkdir -p "${HOST_EXPORT_DIR}"

echo "[1/4] Checking source items inside container..."
docker exec "${KGRAG_CONTAINER}" bash -lc "
set -e
cd '${CONTAINER_APP_DIR}'
for item in ${ITEMS[*]}; do
  if [ -e \"\$item\" ]; then
    du -sh \"\$item\" 2>/dev/null || ls -ld \"\$item\"
  else
    echo \"WARN missing: \$item\"
  fi
done
"

echo "[2/4] Creating tar.gz inside container..."
docker exec "${KGRAG_CONTAINER}" bash -lc "
set -euo pipefail
mkdir -p '${CONTAINER_EXPORT_DIR}'
cd '${CONTAINER_APP_DIR}'
rm -f '${CONTAINER_EXPORT_DIR}/${EXPORT_NAME}' '${CONTAINER_EXPORT_DIR}/tar_error.log'
EXISTING=()
for item in ${ITEMS[*]}; do
  if [ -e \"\$item\" ]; then
    EXISTING+=(\"\$item\")
  else
    echo \"WARN missing: \$item\" >> '${CONTAINER_EXPORT_DIR}/tar_error.log'
  fi
done
if [ \"\${#EXISTING[@]}\" -eq 0 ]; then
  echo 'ERROR: no export items found' >&2
  exit 1
fi
tar -czf '${CONTAINER_EXPORT_DIR}/${EXPORT_NAME}' \"\${EXISTING[@]}\" 2>>'${CONTAINER_EXPORT_DIR}/tar_error.log'
ls -lh '${CONTAINER_EXPORT_DIR}/${EXPORT_NAME}'
"

echo "[3/4] docker cp package to host..."
docker cp "${KGRAG_CONTAINER}:${CONTAINER_EXPORT_DIR}/${EXPORT_NAME}" "${HOST_EXPORT_DIR}/${EXPORT_NAME}"
docker cp "${KGRAG_CONTAINER}:${CONTAINER_EXPORT_DIR}/tar_error.log" "${HOST_EXPORT_DIR}/tar_error_${EXPORT_NAME%.tar.gz}.log" 2>/dev/null || true

echo "[4/4] Verifying package on host..."
ls -lh "${HOST_EXPORT_DIR}/${EXPORT_NAME}"
tar -tzf "${HOST_EXPORT_DIR}/${EXPORT_NAME}" | sed -n '1,30p'
COUNT=$(tar -tzf "${HOST_EXPORT_DIR}/${EXPORT_NAME}" | wc -l)
echo "archive entries: ${COUNT}"

echo ""
echo "DONE. Next copy this file to target host, for example:"
echo "scp ${HOST_EXPORT_DIR}/${EXPORT_NAME} root@192.168.1.108:/home/workspace/kgrag_import/"
echo ""
echo "If tar_error log exists, inspect it:"
echo "ls -lh ${HOST_EXPORT_DIR}/tar_error_${EXPORT_NAME%.tar.gz}.log 2>/dev/null || true"
