# /bin/bash

echo "===================================="
echo "Incrementing build version..."
echo "===================================="
VERSION_FILE=./version

NEW_VERSION=$(cat $VERSION_FILE | awk -F. -v OFS=. 'NF==1{print ++$NF}; NF>1{$NF=sprintf("%0*d", length($NF), ($NF+1)); print}')
echo $NEW_VERSION > $VERSION_FILE

echo "New version is "$NEW_VERSION

echo "===================================="
echo "Bumping client version..."
echo "===================================="
FILE="./frontend/mweb/pubspec.yaml"
if sed --version >/dev/null 2>&1; then
  # GNU sed
  sed -i "s/^version: .*/version: ${NEW_VERSION}+1/" "$FILE"
else
  # BSD/macOS sed
  sed -i '' -e "s/^version: .*/version: ${NEW_VERSION}+1/" "$FILE"
fi

set -euo pipefail

# update version file(s) here
# e.g.: ./ci/scripts/common/update_version_file.sh

if ! git diff --quiet; then
  git add .
  git commit -m "chore: bump build version [skip ci]"
  git push origin HEAD:main
fi
