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

if [ "$OSTYPE" = "msys" ]; then
	is_windows=y
else
	is_windows=n
fi

if [ "$is_windows" = "y" ]; then
	python=python
else
	python=python3
fi
full_check=y
set_rustflags=y

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

build_and_check() {
	new_func "Build and check"
	curr_dir=$(pwd)
	cd "$root_dir/src/rust/iced-x86-js"

	echo "==== FORMAT CHECK ===="
	cargo fmt -- --color always --check
	echo "==== CLIPPY instr_api decoder encoder block_encoder instr_create op_code_info instr_info gas intel masm nasm fast_fmt mvex ===="
	cargo clippy --color always --target wasm32-unknown-unknown --no-default-features --features "instr_api decoder encoder block_encoder instr_create op_code_info instr_info gas intel masm nasm fast_fmt mvex"
	echo "==== CLIPPY instr_api decoder encoder block_encoder instr_create op_code_info instr_info gas intel masm nasm fast_fmt mvex bigint ===="
	cargo clippy --color always --target wasm32-unknown-unknown --no-default-features --features "instr_api decoder encoder block_encoder instr_create op_code_info instr_info gas intel masm nasm fast_fmt mvex bigint"

	set -- \
		"decoder" \
		"decoder mvex" \
		"instr_api decoder" \
		"encoder" \
		"instr_api encoder" \
		"encoder block_encoder" \
		"instr_create" \
		"encoder instr_create" \
		"encoder op_code_info" \
		"instr_api encoder op_code_info" \
		"instr_info" \
		"instr_api instr_info" \
		"gas" \
		"intel" \
		"masm" \
		"nasm" \
		"fast_fmt" \
		"instr_api nasm"
	for features in "$@"; do
		echo "==== $features ===="
		cargo check --color always --target wasm32-unknown-unknown --no-default-features --features "$features"
	done

	set -- \
		"no_vex" \
		"no_evex" \
		"no_xop" \
		"no_d3now" \
		"no_vex no_evex no_xop no_d3now"
	for features in "$@"; do
		echo "==== $features ===="
		cargo check --color always --target wasm32-unknown-unknown --features "$features"
	done

	cd "$curr_dir"
}

test_the_code() {
	new_func "Test the code"
	curr_dir=$(pwd)
	cd "$root_dir/src/rust/iced-x86-js"

	wasm-pack build --mode force --target nodejs -- --features "mvex"
	cd src/tests
	npm install
	npm test
	cd ../..

	cd "$curr_dir"
}

gen_release() {
	new_func "Generate npm bin"
	curr_dir=$(pwd)
	cd "$root_dir/src/rust/iced-x86-js"

	rm -rf pkg/
	wasm-pack build --mode force --target nodejs -- --features "mvex"
	mkdir pkg/tests
	cp ../../../LICENSE.txt pkg
	cp src/tests/*.js pkg/tests

	cd pkg
	npm_package_json="$root_dir/src/rust/iced-x86-js/pkg/package.json"
	test_package_json="$root_dir/src/rust/iced-x86-js/src/tests/package.json"
	$python "$root_dir/build/npm-fix-json.py" "$npm_package_json" "$test_package_json"
	npm pack
	mkdir -p release
	cp iced-x86-*.tgz release/
	cd release
	npm publish iced-x86-*.tgz --dry-run
	cd ../..

	cd "$curr_dir"
}

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

echo
echo "=================================================="
echo "JavaScript 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 "wasm-pack version"
wasm-pack --version
echo "node version"
node --version
echo "npm version"
npm --version
echo "rustup show"
rustup show
echo "Python version"
$python --version

if [ "$full_check" = "y" ]; then
	build_and_check
fi
test_the_code
gen_release
