#!/bin/sh
# vim: set ts=4:
#
# This script ensures that lua interpreter and lua modules specified in Rocksfile are installed.
#
# Environment variables:
#   LUA_VERSION    : Required version of the lua interpreter (e.g. lua-5.1, luajit-2.1). If this
#                    version is not available on PATH and USE_SYSTEM_LUA != 'yes', then it will be
#                    installed, otherwise it fails.
#                    If not set, then the script checks if lua-5.1, or luajit-2 is available.
#                    If not and USE_SYSTEM_LUA != 'yes', then luajit-2.1 will be installed,
#                    otherwise it fails.
#
#   USE_SYSTEM_LUA : Set to 'yes' if you want to use system provided lua. Default is to install
#                    lua locally in .env directory.
set -e

DEFAULT_VERSION='luajit-2.1'
HEREROCKS_URI='https://raw.githubusercontent.com/mpeterv/hererocks/0.20.0/hererocks.py'
show_path_warn=


#======================  Functions  ======================#

find_lua_exec() {
	local cmd; for cmd in lua luajit; do
		if exists "$cmd"; then
			command -v "$cmd"
			return 0
		fi
	done
	return 1
}

check_lua_version() {
	local luabin="$1"
	local ver="$($luabin -v 2>&1 | cut -d' ' -f1-2 | tr '[A-Z] ' '[a-z]-' || echo '')"

	if [ "$LUA_VERSION" ]; then
		case "$ver" in
			$LUA_VERSION*) return 0;;
			*) return 1;;
		esac
	else
		case "$ver" in
			lua-5.1* | luajit-2*) return 0;;
			*) return 1;;
		esac
	fi
}

install_lua() {
	local version=$(printf %s "$1" | tr - =)

	curl -o "$TEMP_DIR/hererocks.py" "$HEREROCKS_URI"
	python "$TEMP_DIR/hererocks.py" "$VENV_DIR" --luarocks=^ --$version
}

install_rock() {
	local name="$1"
	luarocks --mversion show "$name" >/dev/null 2>&1 || luarocks install "$name"
}


#========================  Main  =========================#

# Go to the project's root directory.
cd "$(dirname "$0")/.."

. script/common.sh

mkdir -p "$VENV_DIR" "$TEMP_DIR"

if yesno "$USE_SYSTEM_LUA"; then

	if ! check_lua_version "$(find_lua_exec)"; then
		die "${LUA_VERSION:-lua 5.1 or luajit 2} is not installed and USE_SYSTEM_LUA=$USE_SYSTEM_LUA"

	elif ! exists luarocks; then
		die "luarocks is not installed and USE_SYSTEM_LUA=$USE_SYSTEM_LUA"
	fi

else
	setup_path  # add .env/bin to PATH

	if ! check_lua_version "$VENV_DIR/bin/lua" || [ ! -x "$VENV_DIR/bin/luarocks" ]; then
		version="${LUA_VERSION:-$DEFAULT_VERSION}"

		echo "==> Installing $version and luarocks into $VENV_DIR..."
		install_lua $version
		echo ''
	fi
fi

echo '==> Installing lua modules...'
while read -r rockname; do
	install_rock "$rockname" || die "Failed to install rock $rockname."
done < Rocksfile
