#!/bin/bash

ENV_FILE="supOS-CE/.env"

# 要设置的新值
NEW_PROTOCOL="https"
NEW_DOMAIN="supos-ce-instance5.supos.app"
NEW_PORT="8443"
NEW_VOLUMES_PATH="/volumes/supos/data"
NEW_OS_AUTH_ENABLE="false"

git -C supOS-CE reset --hard HEAD || exit 1
git -C supOS-CE clean -fd || exit 1
git -C supOS-CE pull || exit 1
echo "✅ git 目录已重置并更新"

# 放置证书
cp /root/.acme.sh/supos-ce-instance5.supos.app/supos-ce-instance5.supos.app.key /root/supOS-CE/mount/kong/certificationfile/private.key
cp /root/.acme.sh/supos-ce-instance5.supos.app/fullchain.cer /root/supOS-CE/mount/kong/certificationfile/fullchain.cer

# 使用 sed 执行替换操作（带正则匹配，兼容空格）
sed -i -E \
  -e "s|^ENTRANCE_PROTOCOL *=.*|ENTRANCE_PROTOCOL=${NEW_PROTOCOL}|" \
  -e "s|^ENTRANCE_DOMAIN *=.*|ENTRANCE_DOMAIN=${NEW_DOMAIN}|" \
  -e "s|^ENTRANCE_PORT *=.*|ENTRANCE_PORT=${NEW_PORT}|" \
  -e "s|^VOLUMES_PATH *=.*|VOLUMES_PATH=${NEW_VOLUMES_PATH}|" \
  -e "s|^OS_AUTH_ENABLE *=.*|OS_AUTH_ENABLE=${NEW_OS_AUTH_ENABLE}|" \
  "$ENV_FILE"

echo "✅ .env 文件已更新"

sed -i 's/export ENABLE_PORTAINER=menu/export ENABLE_PORTAINER=none/g' supOS-CE/bin/init/generate-kong-property.sh

echo "✅ portainer前端已关闭，避免用户捣乱"


# 自动执行 startup.sh，跳过交互
echo "🚀 正在自动运行 startup.sh ..."

(
  sleep 5
  echo ""        # 第一次回车，保留 ENTRANCE_DOMAIN
  sleep 2
  echo "2"       # 选择自定义安装服务
  sleep 2
  echo "n"       # Step1 不安装 fuxa
  sleep 2
  echo "y"       # Step2 安装 grafana
  sleep 2
  echo "y"       # Step3 安装 minio
  sleep 2
  echo "n"       # Step4 不安装 ELK
  sleep 2
  echo "n"       # Step5 不安装MCP-Client
  sleep 2
  echo "n"       # Step6 不安装Gitea
  sleep 2
  echo "2"       # Step7 选择 TimescaleDB
) | bash supOS-CE/bin/startup.sh

# 配置Gitea OAuth2
#bash supOS-CE/bin/init/gitea-init.sh
#bash supOS-CE/bin/init/gitea-runner-init.sh 

##去掉GenerativeUI
#ROUTE_ID="a2060aa0-88e5-4247-9635-f93438bbdd84"
#KONG_ADMIN_URL="http://kong:8001"
#ORIGINAL_TAGS=$(docker exec nodered curl -s "${KONG_ADMIN_URL}/routes/${ROUTE_ID}" | jq -c '.tags')
#echo "原始 tags: $ORIGINAL_TAGS"
#NEW_TAGS=$(echo "$ORIGINAL_TAGS" | jq -c 'map(select(. != "menu"))')
#echo "计划更新的 tags: $NEW_TAGS"
#docker exec nodered curl -sS -X PATCH "${KONG_ADMIN_URL}/routes/${ROUTE_ID}" -H 'Content-Type: application/json' -d "{\"tags\": $NEW_TAGS}"
#
##去掉mcp client
#ROUTE_ID="6239b86d-4ef1-48d9-b512-7b05306ab705"
#KONG_ADMIN_URL="http://kong:8001"
#ORIGINAL_TAGS=$(docker exec nodered curl -s "${KONG_ADMIN_URL}/routes/${ROUTE_ID}" | jq -c '.tags')
#echo "原始 tags: $ORIGINAL_TAGS"
#NEW_TAGS=$(echo "$ORIGINAL_TAGS" | jq -c 'map(select(. != "menu"))')
#echo "计划更新的 tags: $NEW_TAGS"
#docker exec nodered curl -sS -X PATCH "${KONG_ADMIN_URL}/routes/${ROUTE_ID}" -H 'Content-Type: application/json' -d "{\"tags\": $NEW_TAGS}"
#
##去掉genApps
#ROUTE_ID="1dfa8b71-07e8-41a5-a07d-536fa0e295a2"
#KONG_ADMIN_URL="http://kong:8001"
#ORIGINAL_TAGS=$(docker exec nodered curl -s "${KONG_ADMIN_URL}/routes/${ROUTE_ID}" | jq -c '.tags')
#echo "原始 tags: $ORIGINAL_TAGS"
#NEW_TAGS=$(echo "$ORIGINAL_TAGS" | jq -c 'map(select(. != "menu"))')
#echo "计划更新的 tags: $NEW_TAGS"
#docker exec nodered curl -sS -X PATCH "${KONG_ADMIN_URL}/routes/${ROUTE_ID}" -H 'Content-Type: application/json' -d "{\"tags\": $NEW_TAGS}"
#
##去掉Portainer
ROUTE_ID="fb690c3a-e09e-4fcf-aad1-5d357c1938ec"
KONG_ADMIN_URL="http://kong:8001"
ORIGINAL_TAGS=$(docker exec nodered curl -s "${KONG_ADMIN_URL}/routes/${ROUTE_ID}" | jq -c '.tags')
echo "原始 tags: $ORIGINAL_TAGS"
NEW_TAGS=$(echo "$ORIGINAL_TAGS" | jq -c 'map(select(. != "menu"))')
echo "计划更新的 tags: $NEW_TAGS"
docker exec nodered curl -sS -X PATCH "${KONG_ADMIN_URL}/routes/${ROUTE_ID}" -H 'Content-Type: application/json' -d "{\"tags\": $NEW_TAGS}"
