#!/usr/bin/env bash

set -euo pipefail

# 控制台模块根目录
CONSOLE_ROOT="/opt/sysb/modules/robot_console"
# 当前版本目录（通过本脚本所在路径推导）
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
CONSOLE_CURR_VERSION_PATH="$(cd "${SCRIPT_DIR}/.." && pwd)"

LATEST_LINK="${CONSOLE_ROOT}/latest"

echo "当前版本目录: ${CONSOLE_CURR_VERSION_PATH}"
echo "current 软链: ${LATEST_LINK}"

if [ ! -e "${LATEST_LINK}" ]; then
  echo "current 软链不存在，继续执行升级流程..."
else
  # 解析 current 软链实际指向
  TARGET_PATH="$(readlink -f "${LATEST_LINK}")"
  echo "current 实际指向: ${TARGET_PATH}"

  if [ "${TARGET_PATH}" = "${CONSOLE_CURR_VERSION_PATH}" ]; then
    echo "current 已指向本版本，无需升级。"
    exit 0
  fi
fi

echo "开始执行配置合并与软链更新..."

# 定义文件路径
BASE_TEMPLATE="${LATEST_LINK}/robot_console/software_template.yaml"
LOCAL_SOFTWARE="${LATEST_LINK}/robot_console/software.yaml"
NEW_TEMPLATE="${CONSOLE_CURR_VERSION_PATH}/robot_console/software_template.yaml"
OUT_SOFTWARE="${CONSOLE_CURR_VERSION_PATH}/robot_console/software.yaml"

# 清理临时文件的函数
cleanup_temp_files() {
  echo "清理临时文件 ..."
  rm -f "${LOCAL_SOFTWARE}"
  # rm -f "${BASE_TEMPLATE}"
  # rm -f "${NEW_TEMPLATE}"
  rm -f "${OUT_SOFTWARE}"
}

# 解密 latest 的配置文件（如果存在加密文件）
if [ -f "${LATEST_LINK}/robot_console/software.enc" ]; then
  echo "解密 latest 的 software.enc ..."
  python3 /opt/sandbox/sysb-deployment/scripts/software_cipher.py --mode deobfuscate \
    --input "${LATEST_LINK}/robot_console/software.enc" \
    --output "${LOCAL_SOFTWARE}"
elif [ ! -f "${LOCAL_SOFTWARE}" ]; then
  echo "警告: ${LOCAL_SOFTWARE} 不存在，且没有对应的 .enc 文件"
  cleanup_temp_files
  exit 1
fi

if [ -f "${LATEST_LINK}/robot_console/software_template.enc" ]; then
  echo "解密 latest 的 software_template.enc ..."
  python3 /opt/sandbox/sysb-deployment/scripts/software_cipher.py --mode deobfuscate \
    --input "${LATEST_LINK}/robot_console/software_template.enc" \
    --output "${BASE_TEMPLATE}"
elif [ ! -f "${BASE_TEMPLATE}" ]; then
  echo "警告: ${BASE_TEMPLATE} 不存在，且没有对应的 .enc 文件"
  cleanup_temp_files
  exit 1
fi

# 解密当前版本的 software_template（如果存在加密文件）
if [ -f "${CONSOLE_CURR_VERSION_PATH}/robot_console/software_template.enc" ]; then
  echo "解密当前版本的 software_template.enc ..."
  python3 /opt/sandbox/sysb-deployment/scripts/software_cipher.py --mode deobfuscate \
    --input "${CONSOLE_CURR_VERSION_PATH}/robot_console/software_template.enc" \
    --output "${NEW_TEMPLATE}"
elif [ ! -f "${NEW_TEMPLATE}" ]; then
  echo "错误: ${NEW_TEMPLATE} 不存在，且没有对应的 .enc 文件"
  cleanup_temp_files
  exit 1
fi

# 执行三方合并
echo "执行三方合并 software.yaml ..."
python3 /opt/sandbox/sysb-deployment/scripts/three_way_merge.py \
  --base="${BASE_TEMPLATE}" \
  --local="${LOCAL_SOFTWARE}" \
  --new="${NEW_TEMPLATE}" \
  --out="${OUT_SOFTWARE}"

# 混淆合并后的 software.yaml
if [ -f "${OUT_SOFTWARE}" ]; then
  echo "混淆合并后的 software.yaml ..."
  python3 /opt/sandbox/sysb-deployment/scripts/software_cipher.py --mode obfuscate \
    --input "${OUT_SOFTWARE}" \
    --output "${CONSOLE_CURR_VERSION_PATH}/robot_console/software.enc"
  
  # 删除临时明文文件
  cleanup_temp_files
else
  echo "错误: 合并后的 software.yaml 不存在"
  cleanup_temp_files
  exit 1
fi

echo "更新模块软链 ..."
python3 /opt/sandbox/sysb-deployment/scripts/update_module_symlinks.py \
  /opt/sandbox/sysb-deployment/metadata/robot_console/manifest.yaml

echo "robot_console 升级完成。"


