#!/bin/bash

usage() {
	echo "Usage: $(basename "$0") -v"
	echo
	echo "This is a fake lua command just to get lua version used by starship."
	echo "Do not use it as a real lua command."
}

version() {
	if [[ "$1" =~ "nvim" ]]; then
		lua_verion=$(nvim -v | grep 'LuaJIT')
		echo "${lua_verion}"
	else
		if command -v lua >/dev/null 2>&1; then
			lua -v
		elif command -v lua5.4 >/dev/null 2>&1; then
			lua5.4 -v
		elif command -v lua5.3 >/dev/null 2>&1; then
			lua5.3 -v
		elif command -v lua5.2 >/dev/null 2>&1; then
			lua5.2 -v
		elif command -v lua5.1 >/dev/null 2>&1; then
			lua5.1 -v
		elif command -v luajit >/dev/null 2>&1; then
			luajit -v
		else
			echo "No lua binary found"
			exit 1
		fi
	fi
}

[ $# -ne 1 ] && usage && exit 1
case $1 in
-v | --version)
	version "$PWD"
	;;
*)
	usage
	exit 1
	;;
esac
