#!/usr/bin/env bash

# ensure consistent behaviour
src_dir="$(dirname "$(realpath "$0")")"
source $src_dir/utils/_env.sh

# stop failing early, because we wouldn't do anything else than fail
set +e

# check that all dependencies are installed correctly
echo -e "${yellow}Checking that all dependencies are properly installed...${reset}"
poetry install --dry-run --only main,dev,lint | grep "0 install" > /dev/null
check_rv $?
echo

# early exit when dependencies are not installed
if test "$aggregated_rv" -ne "0"; then
	echo -e "${red}Dependencies are not properly installed. Run this command to fix it:${reset}"
	echo -e "  ${red}poetry install${reset}"
	exit 1
fi

# check that setup.py is not behind pyproject.toml
echo -e "${yellow}Checking setup.py${reset}"
python scripts/poe-tasks/utils/create_setup.py | diff - setup.py
check_rv $?
python setup.py --help > /dev/null
check_rv $?
echo

# check python/knot_resolver/constants.py
echo -e "${yellow}python/knot_resolver/constants.py${reset}"
meson_setup_configure > /dev/null
diff python/knot_resolver/constants.py $build_dir/python/knot_resolver/constants.py
check_rv $?
echo

# check that doc/_static/config.schema.json is the latest
echo -e "${yellow}Checking doc/_static/config.schema.json${reset}"
python -m knot_resolver.client schema | diff - doc/_static/config.schema.json
check_rv $?
echo

# fancy messages at the end :)
fancy_message

# exit with the aggregate return value
exit $aggregated_rv
