#!/bin/sh
# vim: set ts=4:
#
# This script ensures that lua interpreter and lua modules specified
# in Rocksfile are installed.
#
# Environment variables:
#   LUA_VERSION : Version of the Lua interpreter (e.g. lua-5.3, luajit-2.0) to
#                 be installed. Default is lua-5.1.
#
set -eu

DEFAULT_LUA_VERSION='lua-5.1'
HEREROCKS_URI='https://raw.githubusercontent.com/mpeterv/hererocks/0.16.0/hererocks.py'
HEREROCKS_SHA256='8baac6389ebfb986493f897fafe447fec4a0ab0b69767af7c78824b9bfd69357'

install_rock() {
	local name="$1"
	local args="$name"

	case "$name" in
		*/*) args="--server=https://luarocks.org/manifests/${name%/*} ${name#*/}";;
	esac

	luarocks install $args
}


cd "$(dirname "$0")/.."
. script/utils.sh

mkdir -p "$VENV_DIR" "$TEMP_DIR"

if [ ! -x "$VENV_DIR"/bin/luarocks ]; then
	version="$(printf %s "${LUA_VERSION:-$DEFAULT_LUA_VERSION}" | tr - =)"

	einfo "Installing $version and luarocks into $VENV_DIR"
	wgets "$HEREROCKS_URI" "$HEREROCKS_SHA256" "$TEMP_DIR"
	python "$TEMP_DIR"/hererocks.py "$VENV_DIR" --luarocks=^ --$version
	printf '\n'
fi

. ./.envrc

einfo 'Installing lua modules'
cat Rocksfile | sed -En 's/^([^#]+).*/\1/p' | while read -r rockname; do
	luarocks --mversion show "${rockname#*/}" >/dev/null 2>&1 \
		|| install_rock "$rockname" \
		|| die "Failed to install rock $rockname."
done
