#!/bin/bash

git config user.email "jonathan.franco.2103@gmail.com"
git config user.name "CI"
git remote remove ci
git remote add ci https://oauth2:"${PUBLISH_TOKEN}"@gitlab.com/rglw/public/base/rglw.git
git checkout main
git reset --hard origin/main

cargo install toml-cli
cargo install rglw_bumper

OLD_PACKAGE_VERSION=$(toml get Cargo.toml package.version --raw)
OLD_DEPENDENCIES_VERSION=$(toml get Cargo.toml workspace.package.version --raw)
LAST_COMMIT=$(git log -1 | tr --delete "\n")
PACKAGE_VERSION=$(rglw_bumper commit="${LAST_COMMIT}" version="${OLD_PACKAGE_VERSION}")
DEPENDENCIES_VERSION=$(rglw_bumper commit="${LAST_COMMIT}" version="${OLD_DEPENDENCIES_VERSION}")

toml set Cargo.toml package.version "${PACKAGE_VERSION}" > Cargo.toml.modified
mv Cargo.toml.modified Cargo.toml

toml set Cargo.toml workspace.package.version "${DEPENDENCIES_VERSION}" > Cargo.toml.modified
mv Cargo.toml.modified Cargo.toml

ROOT=$(pwd)
UTIL=${ROOT}/rglw_codegen_utils
CODEGEN=${ROOT}/rglw_codegen
LOG=${ROOT}/rglw_log

sed -i "s/version = \"${OLD_DEPENDENCIES_VERSION}\"/version = \"${DEPENDENCIES_VERSION}\"/g" "${UTIL}"/Readme.md
sed -i "s/version = \"${OLD_DEPENDENCIES_VERSION}\"/version = \"${DEPENDENCIES_VERSION}\"/g" "${CODEGEN}"/Readme.md
sed -i "s/version = \"${OLD_DEPENDENCIES_VERSION}\"/version = \"${DEPENDENCIES_VERSION}\"/g" "${LOG}"/Readme.md
sed -i "s/version = \"${OLD_PACKAGE_VERSION}\"/version = \"${PACKAGE_VERSION}\"/g" Readme.md

git add .
git commit -m "Version ${PACKAGE_VERSION} Release. [ci skip]"
git push ci main

git tag -a "${PACKAGE_VERSION}" -m "Version ${PACKAGE_VERSION}"

git push ci --tags