#!/bin/sh
set -e

# 由环境变量生成 frpc.toml 配置
mkdir -p /etc/frp
# FRP_CUSTOM_DOMAINS 格式：逗号分隔的域名，如 "domain1.com,domain2.com"
# 脚本自动加引号
_domains=""
IFS=","
for _d in ${FRP_CUSTOM_DOMAINS}; do
  _d=$(echo "$_d" | xargs)
  if [ -n "$_domains" ]; then
    _domains="$_domains, "
  fi
  _domains="$_domains\"$_d\""
done
unset IFS

cat > /etc/frp/frpc.toml <<EOF
serverAddr = "${FRP_SERVER_ADDR}"
serverPort = ${FRP_SERVER_PORT:-7000}

auth.token = "${FRP_AUTH_TOKEN}"

[[proxies]]
name = "${FRP_PROXY_NAME:-qa-web}"
type = "http"
localIP = "${FRP_LOCAL_IP:-gateway}"
localPort = ${FRP_LOCAL_PORT:-80}
customDomains = [${_domains}]
EOF

echo "=== frpc config generated ==="
cat /etc/frp/frpc.toml

exec frpc -c /etc/frp/frpc.toml
