#!/bin/bash

set -x

# ================= 配置参数 =================
SRC_DIR="/NxGBSMedia"

# ================ 步骤 1: 拷贝文件 ================
echo "=== Step 1: Copying files from image ==="

# 创建临时目录和容器
rm -rf "./vg_gbs_media_build"
mkdir -p "./vg_gbs_media_build"

docker create --name temp_container vg_gbs_media:2.1.1 >/dev/null 2>&1
docker cp temp_container:/NxGBSMedia ./vg_gbs_media_build/NxGBSMedia
docker rm temp_container >/dev/null 2>&1

# ================ 步骤 2: 写 Dockerfile ================
echo "=== Step 2: Creating Dockerfile ==="

cat <<EOF >./vg_gbs_media_build/Dockerfile
FROM vgaccess/jove:2.0.0

COPY ./NxGBSMedia /NxGBSMedia

ENV LD_LIBRARY_PATH=\${LD_LIBRARY_PATH:+\$LD_LIBRARY_PATH:}/NxGBSMedia/lib
ENV LC_ALL=C.UTF-8

EXPOSE 8814

WORKDIR /NxGBSMedia

VOLUME ["/log"]

ENTRYPOINT ["bin/NxGBSMedia", "-log_dir=/log", "-max_log_size=10"]
EOF

# ================ 步骤 3: 构建新镜像 ================
cd ./vg_gbs_media_build || exit 1

echo "=== Step 3: Building new Docker image ==="
docker build -t vg_gbs_media:2.1.1 .

# ================ 完成提示 ================
echo "✅ Done! New image created:"
docker images | grep "vg_gbs_media:2.1.1"
