#!/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

ROOT=$(pwd)

DEPENDENCIES_VERSION=$(toml get Cargo.toml workspace.package.version --raw)

UTIL=${ROOT}/rglw_codegen_utils
CODEGEN=${ROOT}/rglw_codegen
LOG=${ROOT}/rglw_log
CONFIGURATION=${ROOT}/rglw_configuration

cd "${UTIL}" || exit 1
cargo publish
cd "${CONFIGURATION}" || exit 1
cargo publish
cd "${LOG}" || exit 1
cargo publish
cd "${CODEGEN}" || exit 1
toml set Cargo.toml dependencies.rglw_codegen_utils.version "${DEPENDENCIES_VERSION}" > Cargo.toml.modified
mv Cargo.toml.modified Cargo.toml
cargo publish --allow-dirty
cd "${ROOT}" || exit 1
toml set Cargo.toml dependencies.rglw_codegen.version "${DEPENDENCIES_VERSION}" > Cargo.toml.modified
mv Cargo.toml.modified Cargo.toml
toml set Cargo.toml dependencies.rglw_log.version "${DEPENDENCIES_VERSION}" > Cargo.toml.modified
mv Cargo.toml.modified Cargo.toml
toml set Cargo.toml dependencies.rglw_codegen_utils.version "${DEPENDENCIES_VERSION}" > Cargo.toml.modified
mv Cargo.toml.modified Cargo.toml
toml set Cargo.toml dependencies.rglw_configuration.version "${DEPENDENCIES_VERSION}" > Cargo.toml.modified
mv Cargo.toml.modified Cargo.toml
cargo publish --allow-dirty