#!/bin/bash
# ============================================================
# GBS Application Build & Push Script (Shell version)
# Run on master node: 192.168.20.100
#
# Usage:
#   ./build.sh <app-name> <env-prefix> <jar-file> [version-suffix]
#
# Example:
#   ./build.sh jjb-saas-safety-eval ota /tmp/safety-eval.jar 1
#
# Result image:
#   jjb-registry-registry.cn-hangzhou.cr.aliyuncs.com/ali_img_ns/prod-aly-ota-dragon-jjb-saas-safety-eval:ota-20260626-1
# ============================================================

set -e

# ---- Parameters ----
APP_NAME=${1:?"Usage: $0 <app-name> <env-prefix> <jar-file> [version-suffix]"}
ENV_PREFIX=${2:?"Usage: $0 <app-name> <env-prefix> <jar-file> [version-suffix]"}
JAR_FILE=${3:?"Usage: $0 <app-name> <env-prefix> <jar-file> [version-suffix]"}
VERSION_SUFFIX=${4:-1}

# ---- Constants ----
ACR_REGISTRY="jjb-registry-registry.cn-hangzhou.cr.aliyuncs.com"
ACR_NAMESPACE="ali_img_ns"
ACR_USER="10952138@qq.com"
ACR_PASS='idurCT!rIq9EzISD'
DATE_TAG=$(date +%Y%m%d)
IMAGE_TAG="${ENV_PREFIX}-${DATE_TAG}-${VERSION_SUFFIX}"
FULL_IMAGE="${ACR_REGISTRY}/${ACR_NAMESPACE}/prod-aly-${ENV_PREFIX}-dragon-${APP_NAME}:${IMAGE_TAG}"
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"

# Source environment
source /etc/profile.d/java.sh 2>/dev/null || true
source /etc/profile.d/maven.sh 2>/dev/null || true

echo "=========================================="
echo "  GBS Docker Build & Push"
echo "=========================================="
echo "App:      ${APP_NAME}"
echo "Tag:      ${IMAGE_TAG}"
echo "JAR:      ${JAR_FILE}"
echo "Image:    ${FULL_IMAGE}"
echo "=========================================="

# ---- Step 1: Verify JAR ----
if [ ! -f "${JAR_FILE}" ]; then
    echo "ERROR: JAR file not found: ${JAR_FILE}"
    exit 1
fi
echo "[1/4] JAR verified: $(ls -lh ${JAR_FILE} | awk '{print $5}')"

# ---- Step 2: Prepare build context ----
BUILD_DIR="/tmp/docker-build-${APP_NAME}-$$"
echo "[2/4] Preparing build context at ${BUILD_DIR}..."
mkdir -p ${BUILD_DIR}/target
cp -r /opt/jdk1.8.0_202 ${BUILD_DIR}/
cp ${JAR_FILE} ${BUILD_DIR}/target/
cp ${SCRIPT_DIR}/Dockerfile ${BUILD_DIR}/
echo "  Build context size: $(du -sh ${BUILD_DIR} | awk '{print $1}')"

# ---- Step 3: Docker build ----
echo "[3/4] Building Docker image..."
docker build -t "${FULL_IMAGE}" "${BUILD_DIR}"
echo "  Image size: $(docker images --format '{{.Size}}' ${FULL_IMAGE})"

# ---- Step 4: Push to ACR ----
echo "[4/4] Pushing to ACR..."
echo "${ACR_PASS}" | docker login --username="${ACR_USER}" --password-stdin "${ACR_REGISTRY}" 2>/dev/null
docker push "${FULL_IMAGE}"

# ---- Cleanup ----
rm -rf "${BUILD_DIR}"

echo ""
echo "=========================================="
echo "  BUILD COMPLETE"
echo "=========================================="
echo "Image: ${FULL_IMAGE}"
echo ""
echo "Deploy to K8s:"
echo "  /usr/bin/kubectl create deployment ${APP_NAME} --image=${FULL_IMAGE} -n jjb-dragon --replicas=1"
echo "  /usr/bin/kubectl set image deployment/${APP_NAME} ${APP_NAME}=${FULL_IMAGE} -n jjb-dragon"
echo "=========================================="
