#!/usr/bin/env bash # VoidLibC Linux installer. # Installs voidlink + voidloader, then registers the .vle binfmt_misc entry. # Must be run as root (or with sudo). set -euo pipefail INSTALL_BIN="${INSTALL_BIN:-/usr/local/bin}" BINFMT_CONF_DIR="${BINFMT_CONF_DIR:-/etc/binfmt.d}" MAGIC_DIR="${MAGIC_DIR:-/usr/share/file/magic}" BINFMT_PROC="/proc/sys/fs/binfmt_misc" BINFMT_ENTRY_NAME="vlc" # --- Resolve build artifacts --- SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" BUILD_DIR="${BUILD_DIR:-${SCRIPT_DIR}/../../build}" VOIDLOADER="${BUILD_DIR}/voidloader/voidloader" VOIDLINK="${BUILD_DIR}/voidlink/voidlink" if [[ ! -f "$VOIDLOADER" ]]; then echo "error: voidloader not found at $VOIDLOADER" >&2 echo " Set BUILD_DIR to your cmake build directory." >&2 exit 1 fi if [[ ! -f "$VOIDLINK" ]]; then echo "error: voidlink not found at $VOIDLINK" >&2 echo " Set BUILD_DIR to your cmake build directory." >&2 exit 1 fi # --- Install binaries --- echo "Installing voidlink → ${INSTALL_BIN}/voidlink" install -Dm755 "$VOIDLINK" "${INSTALL_BIN}/voidlink" echo "Installing voidloader → ${INSTALL_BIN}/voidloader" install -Dm755 "$VOIDLOADER" "${INSTALL_BIN}/voidloader" # --- Register binfmt_misc --- # 1. Persistent (systemd-binfmt.service reads /etc/binfmt.d on boot). if [[ -d "$BINFMT_CONF_DIR" ]]; then echo "Installing binfmt.d config → ${BINFMT_CONF_DIR}/vlc.conf" install -Dm644 "${SCRIPT_DIR}/binfmt.d/vlc.conf" \ "${BINFMT_CONF_DIR}/vlc.conf" # Substitute actual interpreter path in case it differs from the default. sed -i "s|/usr/local/bin/voidloader|${INSTALL_BIN}/voidloader|g" \ "${BINFMT_CONF_DIR}/vlc.conf" fi # 2. Immediate (without needing a reboot or systemctl restart). if [[ -d "$BINFMT_PROC" ]]; then # Remove stale entry if present (ignore errors if not registered yet). if [[ -f "${BINFMT_PROC}/${BINFMT_ENTRY_NAME}" ]]; then echo "Removing stale binfmt_misc entry '${BINFMT_ENTRY_NAME}'" echo -1 > "${BINFMT_PROC}/${BINFMT_ENTRY_NAME}" 2>/dev/null || true fi echo "Registering binfmt_misc entry '${BINFMT_ENTRY_NAME}'" # Magic: V=0x56 L=0x4C C=0x43 \0=0x00 — null escaped as \x00. printf ':vlc:M:0:VLC\x00::%s:\n' "${INSTALL_BIN}/voidloader" \ > "${BINFMT_PROC}/register" echo " binfmt_misc: .vlc files will now execute via voidloader" else echo "warning: ${BINFMT_PROC} not mounted; binfmt_misc not active." >&2 echo " Run: mount binfmt_misc -t binfmt_misc /proc/sys/fs/binfmt_misc" >&2 echo " Then re-run this installer, or reboot after installing the binfmt.d config." >&2 fi # --- Install file(1) magic database entry --- if [[ -d "$MAGIC_DIR" ]]; then echo "Installing file(1) magic definition → ${MAGIC_DIR}/vlc" install -Dm644 "${SCRIPT_DIR}/magic/vlc.magic" "${MAGIC_DIR}/vlc" # Recompile the system magic database so `file` picks it up. # file -C -m DIR reads all magic sources from DIR and writes magic.mgc # into the current working directory. We need to cd to the parent so # the compiled database lands next to the magic/ directory where file(1) # expects it. if command -v file &>/dev/null; then MAGIC_PARENT="$(dirname "$MAGIC_DIR")" MAGIC_BASE="$(basename "$MAGIC_DIR")" echo "Recompiling magic database in ${MAGIC_PARENT}..." if ( cd "$MAGIC_PARENT" && file -C -m "$MAGIC_BASE" ) 2>/dev/null; then echo " file(1) now recognizes VoidLibC files" else echo " warning: magic database recompile failed" >&2 echo " Try manually: cd ${MAGIC_PARENT} && sudo file -C -m ${MAGIC_BASE}" >&2 fi fi else echo "warning: ${MAGIC_DIR} not found; file(1) magic not installed." >&2 echo " Install file(1) and re-run, or copy tools/vlc.magic manually." >&2 echo " System-wide: sudo cp tools/vlc.magic ${MAGIC_DIR}/vlc" >&2 echo " cd $(dirname '${MAGIC_DIR}') && sudo file -C -m $(basename '${MAGIC_DIR}')" >&2 fi echo "" echo "VoidLibC installed successfully." echo " voidlink: ${INSTALL_BIN}/voidlink" echo " voidloader: ${INSTALL_BIN}/voidloader" echo "" echo "Test: chmod +x hello.vle && ./hello.vle" echo " voidlink dump hello.vls"