#!/usr/bin/env bash

git_root=$(git rev-parse --show-toplevel)
git_version=$(git describe --tags --abbrev=0)
version=${git_version#v}

major=$(echo "${version}" | cut -d . -f1)
minor=$(echo "${version}" | cut -d . -f2)
patch=$(echo "${version}" | cut -d . -f3)
case "${1-patch}" in
    major) major=$((major + 1)) ;;
    minor) minor=$((minor + 1)) ;;
    *) patch=$((patch + 1)) ;;
esac
next_version="${major}.${minor}.${patch}"

echo "${version} -> ${next_version}"

echo "bumping openapi"
cd "${git_root}"
sed -i -e "s/${version}/${next_version}/g" openapi.yaml
sed -i -e "s/${version}/${next_version}/g" client/static/openapi/openapi.yaml
git add openapi.yaml
git add client/static/openapi/openapi.yaml

echo "bumping client"
cd "${git_root}/client"
npm version "${next_version}"
git add package-lock.json
git add package.json

echo "bumping nix"
cd "${git_root}"
nix-update --flake --version "${next_version}" --subpackage client default
git add flake.nix

git commit -m "bump: v${version} -> v${next_version}"
git push origin main
git tag -a "v${next_version}" -m "bump: v${version} -> v${next_version}"
git push origin "v${next_version}"