#!/bin/bash
set -e

# 进入脚本所在目录
cd "$(dirname "$0")"

echo "======================================================="
echo "开始构建并推送到 registry.cn-qingdao.aliyuncs.com/fzxs/"
echo "======================================================="

echo ">> 构建 server 镜像..."
if ! docker build -t registry.cn-qingdao.aliyuncs.com/fzxs/aurak-server:latest -f ./server/Dockerfile ./server; then
    echo "server 构建失败！请检查 Docker 是否运行以及构建环境。"
    exit 1
fi

echo ">> 构建 web 镜像..."
if ! docker build -t registry.cn-qingdao.aliyuncs.com/fzxs/aurak-web:latest --build-arg VITE_API_BASE_URL=/api -f ./web/Dockerfile .; then
    echo "web 构建失败！请检查 Docker 是否运行以及构建环境。"
    exit 1
fi

echo ">> 构建 libreoffice 镜像..."
if ! docker build -t registry.cn-qingdao.aliyuncs.com/fzxs/aurak-libreoffice:latest -f ./libreoffice-server/Dockerfile ./libreoffice-server; then
    echo "libreoffice 构建失败！请检查 Docker 是否运行以及构建环境。"
    exit 1
fi

echo ">> 推送 server 镜像..."
if ! docker push registry.cn-qingdao.aliyuncs.com/fzxs/aurak-server:latest; then
    echo "推送 server 失败！请检查是否已登录阿里云镜像仓库:"
    echo "docker login --username=YOUR_USERNAME registry.cn-qingdao.aliyuncs.com"
    exit 1
fi

echo ">> 推送 web 镜像..."
if ! docker push registry.cn-qingdao.aliyuncs.com/fzxs/aurak-web:latest; then
    echo "推送 web 失败！请检查是否已登录阿里云镜像仓库:"
    exit 1
fi

echo ">> 推送 libreoffice 镜像..."
if ! docker push registry.cn-qingdao.aliyuncs.com/fzxs/aurak-libreoffice:latest; then
    echo "推送 libreoffice 失败！请检查是否已登录阿里云镜像仓库:"
    exit 1
fi

echo "======================================================="
echo "镜像构建并推送成功！"
echo "======================================================="
