#!/bin/sh # Installs LuaXE on Linux x86_64 from https://luaxe.dev # # curl -fsSL https://luaxe.dev/install | sh # # Puts lxe in ${LXE_HOME:-$HOME/.lxe}/bin and symlinks ~/.local/bin/lxe, # installing the LuaJIT runtime (libluajit-5.1.so) next to it. Run it again to # update; no root privileges are needed. Remove everything again with: # # rm -f ~/.local/bin/lxe "$HOME/.lxe/bin/lxe" "$HOME/.lxe/bin/libluajit-5.1.so" # # Optional environment variables (they belong on sh's side of the pipe): # curl -fsSL https://luaxe.dev/install | LUAXE_URL=http://localhost:4321 sh # LUAXE_URL=http://localhost:4321 download from another server # LXE_HOME=/opt/lxe lxe's home (bin/), as lxe itself reads it set -eu base=${LUAXE_URL:-https://luaxe.dev} case "$base" in */) base=$(expr "$base" : '\(.*[^/]\)/*$' || true) ;; esac fail() { printf '%s\n' "install: $*" >&2 exit 1 } [ "$(uname -s 2>/dev/null || echo unknown)" = Linux ] || \ fail "LuaXE's Linux build only runs on Linux x86_64" [ "$(uname -m 2>/dev/null || echo unknown)" = x86_64 ] || \ fail "LuaXE's Linux build only runs on Linux x86_64" if command -v curl >/dev/null 2>&1; then fetch() { curl -fsSL --max-time 120 "$1" -o "$2"; } elif command -v wget >/dev/null 2>&1; then fetch() { wget -q -O "$2" "$1"; } else fail "need curl or wget to download LuaXE" fi if command -v sha256sum >/dev/null 2>&1; then file_hash() { sha256sum "$1" | cut -d ' ' -f 1; } elif command -v shasum >/dev/null 2>&1; then file_hash() { shasum -a 256 "$1" | cut -d ' ' -f 1; } else fail "need sha256sum or shasum to verify the download" fi home=${LXE_HOME:-${HOME:?'install: HOME is not set'}/.lxe} case "$home" in /*) : ;; *) fail "LXE_HOME must be an absolute path" ;; esac bin_dir=$home/bin link_dir=$HOME/.local/bin link=$link_dir/lxe printf 'Installing LuaXE to %s\n' "$bin_dir" mkdir -p "$bin_dir" "$link_dir" || fail "could not create $bin_dir or $link_dir" tmp_dir=$(mktemp -d 2>/dev/null || mktemp -d -t luaxe) || fail "could not make a temporary directory" trap 'rm -rf "$tmp_dir"' EXIT INT TERM HUP sha_lxe=$tmp_dir/lxe.sha256 sha_rt=$tmp_dir/libluajit.sha256 bin_tmp=$tmp_dir/lxe rt_tmp=$tmp_dir/libluajit-5.1.so # Every checksum first: a server that is half updated changes nothing. fetch "$base/get/linux-x86_64/sha256" "$sha_lxe" || fail "could not download the lxe checksum" fetch "$base/runtime/linux-x86_64/libluajit-5.1.so/sha256" "$sha_rt" || fail "could not download the runtime checksum" want_lxe=$(tr -d ' \t\r\n' < "$sha_lxe") want_rt=$(tr -d ' \t\r\n' < "$sha_rt") case "$want_lxe" in *[!0-9a-fA-F]*|'') fail "$base/get/linux-x86_64/sha256 did not return a checksum" ;; esac case "$want_rt" in *[!0-9a-fA-F]*|'') fail "$base/runtime/linux-x86_64/libluajit-5.1.so/sha256 did not return a checksum" ;; esac case ${#want_lxe} in 64) : ;; *) fail "$base/get/linux-x86_64/sha256 did not return a checksum" ;; esac case ${#want_rt} in 64) : ;; *) fail "$base/runtime/linux-x86_64/libluajit-5.1.so/sha256 did not return a checksum" ;; esac printf ' downloading lxe\n' fetch "$base/get/linux-x86_64" "$bin_tmp" || fail "could not download lxe" printf ' downloading libluajit-5.1.so\n' fetch "$base/runtime/linux-x86_64/libluajit-5.1.so" "$rt_tmp" || fail "could not download libluajit-5.1.so" got_lxe=$(file_hash "$bin_tmp") got_rt=$(file_hash "$rt_tmp") [ "$(printf '%s' "$got_lxe" | tr 'A-Z' 'a-z')" = "$(printf '%s' "$want_lxe" | tr 'A-Z' 'a-z')" ] || \ fail "lxe failed its checksum (expected $want_lxe, got $got_lxe)" [ "$(printf '%s' "$got_rt" | tr 'A-Z' 'a-z')" = "$(printf '%s' "$want_rt" | tr 'A-Z' 'a-z')" ] || \ fail "libluajit-5.1.so failed its checksum (expected $want_rt, got $got_rt)" if [ -f "$bin_dir/lxe" ] && [ "$(file_hash "$bin_dir/lxe")" = "$got_lxe" ] && \ [ -f "$bin_dir/libluajit-5.1.so" ] && [ "$(file_hash "$bin_dir/libluajit-5.1.so")" = "$got_rt" ]; then printf ' lxe is up to date\n' else mv -f "$bin_tmp" "$bin_dir/lxe" || fail "could not install $bin_dir/lxe" mv -f "$rt_tmp" "$bin_dir/libluajit-5.1.so" || fail "could not install $bin_dir/libluajit-5.1.so" fi chmod +x "$bin_dir/lxe" || fail "could not make $bin_dir/lxe executable" if [ -e "$link" ] && [ ! -L "$link" ]; then fail "$link exists and is not a symlink; move it aside and run this again" fi ln -sf "$bin_dir/lxe" "$link" || fail "could not link $link" printf '\nLuaXE is installed.\n' printf ' lxe --help show the toolchain commands\n' printf ' docs: %s/docs\n' "$base" case ":$PATH:" in *":$link_dir:"*) : ;; *) printf 'Add %s to PATH, for example:\n export PATH="%s:$PATH"\n' "$link_dir" "$link_dir" ;; esac printf 'Uninstall: rm -f %s %s %s\n' "$link" "$bin_dir/lxe" "$bin_dir/libluajit-5.1.so" trap - EXIT INT TERM HUP rm -rf "$tmp_dir"