#!/bin/bash

set -euo pipefail

# Enforce date-based versioning (YY.MM.DD) before publishing.
# Remove leading zeros from month and day to comply with cargo fmt requirements
year=$(date +%y)
month=$(date +%-m)
day=$(date +%-d)
today_raw="${year}-${month}-${day}"
expected_version=${today_raw//-/.}

read_versions=$(python - <<'PY'
import sys


def load_toml(path: str):
		try:
				import tomllib  # Python 3.11+
		except ModuleNotFoundError:  # Fallback for older Python
				import tomli as tomllib  # type: ignore
		with open(path, "rb") as f:
				return tomllib.load(f)


workspace = load_toml("Cargo.toml")
core = load_toml("core/Cargo.toml")
tui = load_toml("tui/Cargo.toml")

workspace_version = workspace.get("workspace", {}).get("package", {}).get("version")

core_pkg = core.get("package", {})
core_version = core_pkg.get("version") or workspace_version
if isinstance(core_version, dict) and core_version.get("workspace"):
	core_version = workspace_version

linutil_core_dep = tui.get("dependencies", {}).get("linutil_core")
linutil_core_dep_version = None
if isinstance(linutil_core_dep, dict):
		linutil_core_dep_version = linutil_core_dep.get("version")

print(workspace_version or "")
print(core_version or "")
print(linutil_core_dep_version or "")
PY
)

workspace_version=$(echo "$read_versions" | sed -n '1p')
core_version=$(echo "$read_versions" | sed -n '2p')
tui_dep_version=$(echo "$read_versions" | sed -n '3p')

if [[ -z "$workspace_version" || -z "$core_version" || -z "$tui_dep_version" ]]; then
	echo "Unable to determine required versions from Cargo manifests." >&2
	exit 1
fi

# Function to update version in a TOML file
update_toml_version() {
	local file=$1
	local old_version=$2
	local new_version=$3
	
	echo "Updating version in $file from $old_version to $new_version..."
	sed -i "s/version = \"$old_version\"/version = \"$new_version\"/" "$file"
}

version_updated=false

if [[ "$workspace_version" != "$expected_version" ]]; then
	echo "Workspace version $workspace_version does not match today's expected $expected_version."
	update_toml_version "Cargo.toml" "$workspace_version" "$expected_version"
	version_updated=true
fi

if [[ "$core_version" != "$expected_version" ]]; then
	echo "linutil_core package version $core_version does not match today's expected $expected_version."
	# Core uses workspace version, so if workspace was updated, this should be fine
	if [[ "$workspace_version" == "$core_version" ]]; then
		echo "Core version will be updated via workspace version."
	fi
fi

if [[ "$tui_dep_version" != "$expected_version" ]]; then
	echo "linutil_tui depends on linutil_core $tui_dep_version but expected $expected_version."
	update_toml_version "tui/Cargo.toml" "$tui_dep_version" "$expected_version"
	version_updated=true
fi

if [[ "$version_updated" == true ]]; then
	echo "Versions have been updated to $expected_version."
	echo "Please review the changes and run the script again to proceed with publishing."
	exit 0
fi

echo "Version check passed: $expected_version"

echo "Running sort and format checks..."
if ! cargo sort --check --workspace; then
	echo "cargo sort check failed. Please run 'cargo sort --workspace' to fix." >&2
	exit 1
fi

if ! cargo fmt --check; then
	echo "cargo fmt check failed. Please run 'cargo fmt' to fix." >&2
	exit 1
fi

bash sort-tomlfiles.sh
cargo test --no-fail-fast --package linutil_core
cargo build --release
echo "Checks passed."

read -r -p "Publish to crates.io? [y/N]: " answer
case "$answer" in
	[Yy]*)
		cargo publish -p linutil_core
		cargo publish -p linutil_tui
		;;
	*)
		echo "Publish skipped."
		;;
esac
