#!/bin/bash

# Set variables
IMAGE_NAME="nise-replay-viewer"
IMAGE_REGISTRY="git.gengo.tech/nuff"
IMAGE_VERSION="latest"

rm -rf dist/

# Clean and build Angular project
source /usr/share/nvm/init-nvm.sh
nvm use 18.19
npm run build
if [ $? -ne 0 ]; then
    echo "ng build failed"
    exit 1
fi

# Build and push Docker image
docker build . -t $IMAGE_NAME:$IMAGE_VERSION
if [ $? -ne 0 ]; then
    echo "Docker build failed"
    exit 1
fi

docker tag $IMAGE_NAME:$IMAGE_VERSION $IMAGE_REGISTRY/$IMAGE_NAME:$IMAGE_VERSION
docker push $IMAGE_REGISTRY/$IMAGE_NAME:$IMAGE_VERSION
if [ "$?" != "0" ]; then
    echo "Error: Failed to push $IMAGE_REGISTRY/$IMAGE_NAME:$IMAGE_VERSION"
    exit 1
fi

echo "Docker image pushed successfully to $IMAGE_REGISTRY/$IMAGE_NAME:$IMAGE_VERSION"
