#!/bin/bash
# Partial deployment script - bumps main container version by 0.001

DEPLOYMENT_FILE="argocd/deployment.yaml"

# Check if file exists
if [ ! -f "$DEPLOYMENT_FILE" ]; then
    echo "Error: $DEPLOYMENT_FILE not found"
    exit 1
fi

# Extract current version of main container (labhelper, not labhelper-data-loader)
CURRENT_VERSION=$(grep -E "image: git.baumann.gr/adebaumann/vui:[0-9]" "$DEPLOYMENT_FILE" | grep -v "data-loader" | sed -E 's/.*:([0-9.]+)/\1/')

if [ -z "$CURRENT_VERSION" ]; then
    echo "Error: Could not find current version"
    exit 1
fi

# Calculate new version (add 0.001), preserve leading zero
NEW_VERSION=$(echo "$CURRENT_VERSION + 0.001" | bc | sed 's/^\./0./')

# Update the deployment file (only the main container, not the data-loader)
sed -i "s|image: git.baumann.gr/adebaumann/vui:$CURRENT_VERSION|image: git.baumann.gr/adebaumann/vui:$NEW_VERSION|" "$DEPLOYMENT_FILE"

# Update the configmap version to match the main container
CONFIGMAP_FILE="argocd/configmap.yaml"
sed -i "s|VERSION: \"$CURRENT_VERSION\"|VERSION: \"$NEW_VERSION\"|" "$CONFIGMAP_FILE"

echo "Partial deployment prepared:"
echo "  Main container: $CURRENT_VERSION -> $NEW_VERSION"
echo "  ConfigMap VERSION: $NEW_VERSION"
