#!/bin/bash
source ../environment.sh

# 检查并停止/删除已存在的容器
if docker ps -a | grep -q ${NGINX_NAME}; then
    echo "停止并删除已存在的 ${NGINX_NAME} 容器..."
    docker stop ${NGINX_NAME} >/dev/null 2>&1
    docker rm ${NGINX_NAME} >/dev/null 2>&1
fi

# 准备 docker run 命令
DOCKER_RUN_CMD="docker run --pull always -d \
--name ${NGINX_NAME} \
--network ${NETWORK} \
--add-host HOSTNAMEIP:host-gateway \
-p ${NGINX_HTTP_PORT}:80 \
-p ${NGINX_HTTPS_PORT}:443 \
--env TZ=Asia/Shanghai \
--memory ${NGINX_MEMORY} \
--volume ${STREAM_DATA}:/data/recording \
--volume ${SRS_DATA}:/data/srs"

# 如果配置了证书，添加证书挂载
# if [ ! -z "${PEM_PATH}" ] && [ ! -z "${KEY_PATH}" ]; then
#     echo "检测到证书配置，将挂载证书文件..."
#     if [ ! -f "${PEM_PATH}" ] || [ ! -f "${KEY_PATH}" ]; then
#         echo "错误：证书文件不存在！"
#         exit 1
#     fi
#     DOCKER_RUN_CMD="${DOCKER_RUN_CMD} \
# --volume ${PEM_PATH}:/etc/nginx/t-aaron.com.pem \
# --volume ${KEY_PATH}:/etc/nginx/t-aaron.com.key"
# fi


# 如果配置了证书，添加证书挂载
if [ ! -z "${DSP_PEM_PATH}" ] && [ ! -z "${DSP_KEY_PATH}" ]; then
    echo "检测到DSP证书配置，将挂载证书文件..."
    # if [ ! -f "${DSP_PEM_PATH}" ] || [ ! -f "${DSP_KEY_PATH}" ]; then
    #     echo "错误：DSP证书文件不存在！"
    #     echo "PEM文件路径: ${DSP_PEM_PATH}"
    #     echo "KEY文件路径: ${DSP_KEY_PATH}"
    #     exit 1
    # fi
    DOCKER_RUN_CMD="${DOCKER_RUN_CMD} \
--mount type=bind,source=${DSP_PEM_PATH},target=/etc/nginx/dsp.pem,readonly \
--mount type=bind,source=${DSP_KEY_PATH},target=/etc/nginx/dsp.key,readonly"
fi

if [ ! -z "${HHZ_PEM_PATH}" ] && [ ! -z "${HHZ_KEY_PATH}" ]; then
    echo "检测到HHZ证书配置，将挂载证书文件..."
    # if [ ! -f "${HHZ_PEM_PATH}" ] || [ ! -f "${HHZ_KEY_PATH}" ]; then
    #     echo "错误：HHZ证书文件不存在！"
    #     echo "PEM文件路径: ${HHZ_PEM_PATH}"
    #     echo "KEY文件路径: ${HHZ_KEY_PATH}"
    #     exit 1
    # fi
    DOCKER_RUN_CMD="${DOCKER_RUN_CMD} \
--mount type=bind,source=${HHZ_PEM_PATH},target=/etc/nginx/hhz.pem,readonly \
--mount type=bind,source=${HHZ_KEY_PATH},target=/etc/nginx/hhz.key,readonly"
fi

if [ ! -z "${MINIO_PEM_PATH}" ] && [ ! -z "${MINIO_KEY_PATH}" ]; then
    echo "检测到MINIO证书配置，将挂载证书文件..."
    # if [ ! -f "${MINIO_PEM_PATH}" ] || [ ! -f "${MINIO_KEY_PATH}" ]; then
    #     echo "错误：MINIO证书文件不存在！"
    #     echo "PEM文件路径: ${MINIO_PEM_PATH}"
    #     echo "KEY文件路径: ${MINIO_KEY_PATH}"
    #     exit 1
    # fi
    DOCKER_RUN_CMD="${DOCKER_RUN_CMD} \
--mount type=bind,source=${MINIO_PEM_PATH},target=/etc/nginx/minio.pem,readonly \
--mount type=bind,source=${MINIO_KEY_PATH},target=/etc/nginx/minio.key,readonly"
fi

if [ ! -z "${OIDC_PEM_PATH}" ] && [ ! -z "${OIDC_KEY_PATH}" ]; then
    echo "检测到OIDC证书配置，将挂载证书文件..."
    # if [ ! -f "${OIDC_PEM_PATH}" ] || [ ! -f "${OIDC_KEY_PATH}" ]; then
    #     echo "错误：OIDC证书文件不存在！"
    #     echo "PEM文件路径: ${OIDC_PEM_PATH}"
    #     echo "KEY文件路径: ${OIDC_KEY_PATH}"
    #     exit 1
    # fi
    DOCKER_RUN_CMD="${DOCKER_RUN_CMD} \
--mount type=bind,source=${OIDC_PEM_PATH},target=/etc/nginx/oidc.pem,readonly \
--mount type=bind,source=${OIDC_KEY_PATH},target=/etc/nginx/oidc.key,readonly"
fi

if [ ! -z "${SRS_PEM_PATH}" ] && [ ! -z "${SRS_KEY_PATH}" ]; then
    echo "检测到SRS证书配置，将挂载证书文件..."
    # if [ ! -f "${SRS_PEM_PATH}" ] || [ ! -f "${SRS_KEY_PATH}" ]; then
    #     echo "错误：SRS证书文件不存在！"
    #     echo "PEM文件路径: ${SRS_PEM_PATH}"
    #     echo "KEY文件路径: ${SRS_KEY_PATH}"
    #     exit 1
    # fi
    DOCKER_RUN_CMD="${DOCKER_RUN_CMD} \
--mount type=bind,source=${SRS_PEM_PATH},target=/etc/nginx/srs.pem,readonly \
--mount type=bind,source=${SRS_KEY_PATH},target=/etc/nginx/srs.key,readonly"
fi

echo "镜像名字------"
echo ${NGINX_IMAGE}

# 添加镜像名称并运行容器
DOCKER_RUN_CMD="${DOCKER_RUN_CMD} ${NGINX_IMAGE}"

# 执行 docker run 命令
echo "启动 Nginx 容器..."
echo ${DOCKER_RUN_CMD}
eval ${DOCKER_RUN_CMD}

# 检查容器是否成功启动
if [ $? -eq 0 ]; then
    echo "Nginx 容器启动成功！"
    docker ps | grep ${NGINX_NAME}
else
    echo "错误：Nginx 容器启动失败！"
    exit 1
fi