#!/bin/sh
set -e

root_dir=$(dirname "$0")
root_dir=$(cd "$root_dir/.." && pwd)
if [ ! -f "$root_dir/LICENSE.txt" ]; then
	echo "Couldn't find the root dir"
	exit 1
fi
luacrate_dir="$root_dir/src/rust/loona"
luasrc_dir="$root_dir/src/rust/iced-x86-lua"
cargo_toml="$luasrc_dir/Cargo.toml"

full_check=y
set_rustflags=y

new_func() {
	echo
	echo "****************************************************************"
	echo "$1"
	echo "****************************************************************"
	echo
}

patchci_verify_not_patched() {
	# msys grep fails if we use $
	if ! grep -E '^#pathci' "$cargo_toml" 2>&1 > /dev/null; then
		echo "Cargo.toml is patched"
		exit 1
	fi
}

patchci_verify_patched() {
	# msys grep fails if we use $
	if grep -E '^#pathci' "$cargo_toml" 2>&1 > /dev/null; then
		echo "Cargo.toml is not patched"
		exit 1
	fi
}

# We must reference the current iced-x86 code so we can't use the crates.io crate
# since it hasn't been pushed yet. We patch lua/Cargo.toml to point to the current
# source code.
#
# The sdist's Cargo.toml must be the original file though or they won't be able
# to build it once it's been published.
patchci_patch() {
	patchci_verify_not_patched

	curr_dir=$(pwd)
	cd "$root_dir"

	if [ "$OSTYPE" = "msys" ]; then
		iced_x86_dir="$(pwd -W)/src/rust/iced-x86"
	else
		iced_x86_dir="$(pwd)/src/rust/iced-x86"
	fi
	if [ ! -d "$iced_x86_dir" ]; then
		echo "Dir does not exist: $iced_x86_dir"
		exit 1
	fi

	sed -i -e "s&^#pathci$&path = \"$iced_x86_dir\"&" "$cargo_toml"

	cd "$curr_dir"
}

patchci_undo_patch() {
	git checkout "$cargo_toml"
}

test_lua_crate() {
	new_func "Test Lua crate"

	curr_dir=$(pwd)
	cd "$luacrate_dir"

	set -- \
		"lua5_1" \
		"lua5_2" \
		"lua5_3" \
		"lua5_4"
	for features in "$@"; do
		echo "==== CLIPPY RELEASE $features ===="
		cargo clippy --color always --release --features "$features"
	done

	echo "==== RUST FORMAT CHECK ===="
	cargo fmt -- --color always --check

	cd "$curr_dir"
}

build_test_rock() {
	(build_test_rock_lua 5 1)
	(build_test_rock_lua 5 2)
	(build_test_rock_lua 5 3)
	(build_test_rock_lua 5 4)
}

build_test_rock_lua() {
	lua_maj=$1
	lua_min=$2
	lua="lua$lua_maj.$lua_min"

	new_func "Building and testing rock (Lua $lua_maj.$lua_min)"

	cd "$luasrc_dir"

	ICED_LUA_EXTRA_FEATURES="extra_checks" luarocks make iced_x86-*.rockspec --local --lua-version $lua_maj.$lua_min

	eval $(luarocks path --lua-version $lua_maj.$lua_min)
	busted_filename=/tmp/tmp-busted-runner
	echo "require 'busted.runner'({ standalone = false })" > "$busted_filename"
	"$lua" -- "$busted_filename" -m "./tests/?.lua" tests

	luarocks remove iced_x86 --local --lua-version $lua_maj.$lua_min
}

misc_tests() {
	new_func "clippy, rustfmt, stylua"

	curr_dir=$(pwd)
	cd "$luasrc_dir"

	patchci_verify_patched

	echo "==== RUST FORMAT CHECK ===="
	cargo fmt -- --color always --check

	echo "==== LUA FORMAT CHECK ===="
	stylua --color always --check tests -f ./stylua.toml

	set -- \
		"lua5_1" \
		"lua5_2" \
		"lua5_3" \
		"lua5_4"
	for features in "$@"; do
		echo "==== CLIPPY RELEASE $features ===="
		cargo clippy --color always --release --features "$features"
	done

	cd "$curr_dir"
}

while [ "$#" -gt 0 ]; do
	case $1 in
	--quick-check) full_check=n ;;
	--no-set-rustflags) set_rustflags=n ;;
	*) echo "Unknown arg: $1"; exit 1 ;;
	esac
	shift
done

echo
echo "=================================================="
echo "Lua build"
echo "=================================================="
echo

if [ "$set_rustflags" = "y" ]; then
	export RUSTFLAGS="-D warnings"
fi

echo "rustup show"
rustup show
echo "cargo version"
cargo --version
echo "Rust version"
rustc --version
echo "luarocks version"
luarocks --version
echo "stylua version"
stylua --version

patchci_patch
test_lua_crate
build_test_rock
if [ "$full_check" = "y" ]; then
	misc_tests
fi
patchci_undo_patch
