#! /bin/sh
echo "execute"


ssh-agent bash -c 'ssh-add /root/.ssh/openAI-realtimeapi-reservations; git pull origin main'

docker build -t ai-menu-translator .
#docker restart 87
#docker run --rm -p 4004:4000 order.bg-angular-universal


# Define variables
CONTAINER_NAME="ai-menu-translator"
PORT=8078

# Stop and remove any existing container with the same name
RUNNING_CONTAINER=$(docker ps -q --filter "name=$CONTAINER_NAME")

if [ -n "$RUNNING_CONTAINER" ]; then
    echo "Stopping and removing existing container ($RUNNING_CONTAINER)..."
    docker stop $RUNNING_CONTAINER
    docker rm $RUNNING_CONTAINER
fi

# If port is still in use (sometimes container might not release it immediately)
if lsof -i tcp:$PORT; then
    echo "Port $PORT is still in use, releasing it..."
    fuser -k $PORT/tcp
fi

# Run a new container using the latest image
echo "Running new container with updated image..."
docker run --rm -d -p $PORT:8000 --name $CONTAINER_NAME ai-menu-translator
#docker run --rm -p 8078:8000 --name ai-menu-translator ai-menu-translator

echo "New container started successfully"
