#!/bin/bash
# devvm layer: W3BS OS (https://w3bs.org/os). Install it with
#
#   mkdir -p ~/.config/devvm/layers
#   curl -fsSL https://w3bs.org/os/devvm/w3bs.sh -o ~/.config/devvm/layers/w3bs.sh
#   curl -fsSL https://w3bs.org/os/devvm/w3bs.policy -o ~/.config/devvm/layers/w3bs.policy
#   devvm --from debian:13 --layers w3bs      # W3BS OS 1.2 is Debian 13
#
# devvm runs it once, as the VM's user (who has sudo), when it builds the image. devvm's own
# shared tools give the VM node, bun, pnpm and the coding agents (devvm --tools node,bun), so
# this layer adds only W3BS: the release file, motd, PATH and the w3bs CLI. The VM keeps
# bash, where devvm's prompt lives. Source: https://github.com/profullstack/w3bs.org/tree/main/os/vm
set -euo pipefail

# Alpine by its package manager, as devvm's own prelude does ($devvm_libc is not in every
# devvm release).
if command -v apk >/dev/null 2>&1; then
	# Alpine, such as devvm --from w3bs.org/os:server: W3BS OS's Docker edition is already
	# W3BS OS, so it needs only the w3bs CLI in this user's home (musl build).
	curl -fsSL https://w3bs.org/install.sh | W3BS_YES=1 sh
else
	curl -fsSL https://w3bs.org/os/convert.sh -o /tmp/w3bs-os-convert
	sudo bash /tmp/w3bs-os-convert --part system --edition devvm --user "$(id -un)" --keep-shell
	bash /tmp/w3bs-os-convert --part user --no-dev-tools
	rm -f /tmp/w3bs-os-convert
fi
# shellcheck disable=SC2016 # written for ~/.profile to expand at login
grep -qs 'local/bin' ~/.profile || echo 'PATH="$HOME/.local/bin:$PATH"' >>~/.profile
"$HOME/.local/bin/w3bs" --version
