#!/usr/bin/env bash # docker/devcontainer/post-create.sh v.2.3.0 # This script runs after the Dev Container is created to set up the dev container environment. # # Usage: # post-create.sh <--bun|--node> [--plugin|--matterbridge] [--dev|--main] # # --bun | --node runtime of the image; required. # --plugin plugin repository: also installs Matterbridge from the selected branch, # links it, builds the plugin frontend when present and adds the plugin. # --matterbridge Matterbridge repository. # --dev | --main branch used for the Matterbridge install; only with --plugin, default --dev. # # The dev container images copy this script to /usr/local/bin, so devcontainer.json can call it # from there: # "postCreateCommand": "bash /usr/local/bin/post-create.sh --node" # "postCreateCommand": "bash /usr/local/bin/post-create.sh --bun --plugin --dev" # "postCreateCommand": "bash /usr/local/bin/post-create.sh --node --matterbridge" set -euo pipefail MODE="" PLUGIN=false MATTERBRIDGE=false BRANCH="dev" for arg in "$@"; do case "$arg" in --bun) MODE="bun" ;; --node) MODE="node" ;; --plugin) PLUGIN=true ;; --matterbridge) MATTERBRIDGE=true ;; --dev) BRANCH="dev" ;; --main) BRANCH="main" ;; *) echo "Unknown argument: $arg" >&2 echo "Usage: post-create.sh <--bun|--node> [--plugin|--matterbridge] [--dev|--main]" >&2 exit 1 ;; esac done if [ -z "$MODE" ]; then echo "Usage: post-create.sh <--bun|--node> [--plugin|--matterbridge] [--dev|--main]" >&2 exit 1 fi TARGET="project" if [ "$PLUGIN" = true ]; then TARGET="plugin" echo "Welcome to Matterbridge Plugin Dev Container (post-create.sh)" else if [ "$MATTERBRIDGE" = true ]; then TARGET="Matterbridge" fi echo "Welcome to Matterbridge Dev Container (post-create.sh)" fi DISTRO=$(awk -F= '/^PRETTY_NAME=/{gsub(/"/, "", $2); print $2}' /etc/os-release) CODENAME=$(awk -F= '/^VERSION_CODENAME=/{print $2}' /etc/os-release) echo "Distro: $DISTRO ($CODENAME)" echo "User: $(whoami)" echo "Hostname: $(hostname)" echo "Architecture: $(uname -m)" echo "Kernel Version: $(uname -r)" echo "Uptime: $(uptime -p || echo 'unavailable')" echo "Date: $(date)" if [ "$MODE" = "bun" ]; then echo "Bun version: $(bun -v)" echo "Bun location: $(command -v bun)" echo "Bun cache: ${HOME}/.bun/install/cache" echo "Bun global prefix: ${BUN_INSTALL:-$HOME/.bun}/install/global/node_modules" else echo "Node.js version: $(node -v)" echo "Node.js location: $(command -v node)" echo "Npm version: $(npm -v)" echo "Npm cache: $(npm config get cache)" echo "Npm global prefix: $(npm root -g)" fi echo "" STEP=1 step() { echo $'\033[36m'"[$(date '+%Y-%m-%d %H:%M:%S')]"$'\033[0m' "${STEP}.post-create - $1" STEP=$((STEP + 1)) } # Ensure required directories exist and are owned by the current user workspace_paths=("$PWD/node_modules" "$PWD/.cache") home_paths=("$HOME/.claude" "$HOME/.codex" "$HOME/.gemini" "$HOME/.agents" "$HOME/.bash-cache" "$HOME/.npm" "$HOME/.bun" "$HOME/.bun/install/cache" "$HOME/.vscode-server/extensions") if [ "$PLUGIN" = true ] || [ "$MATTERBRIDGE" = true ]; then home_paths+=("$HOME/Matterbridge" "$HOME/.matterbridge" "$HOME/.mattercert") fi step "Creating directories..." sudo mkdir -p "${workspace_paths[@]}" "${home_paths[@]}" # Create directories if they don't exist # Only chown apps/frontend/node_modules if it exists (e.g. mounted as a volume); do not create it if [ -d "$PWD/apps/frontend/node_modules" ]; then workspace_paths+=("$PWD/apps/frontend/node_modules") fi step "Setting permissions..." # Only chown paths that are not already owned by the current user. The image pre-creates the # home paths, so fresh volumes are seeded correctly and this is a no-op; the workspace volumes # still need it on first create, but they are empty then, so the recursion is instant. for path in . "${workspace_paths[@]}" "${home_paths[@]}"; do [ -e "$path" ] || continue if [ "$(stat -c %u "$path")" != "$(id -u)" ]; then # Skip sockets (e.g. .git/fsmonitor--daemon.ipc) and only touch what is actually wrong; # a failure on the bind-mounted workspace must not abort the setup. sudo find "$path" -xdev ! -type s ! -user "$(id -u)" -exec chown -h "$(id -u):$(id -g)" {} + \ || echo "Warning: could not change ownership of some files in $path" # Transfer ownership to the current user fi done step "Marking the workspace as a git safe directory..." # On macOS and Windows the bind-mounted workspace can report a different owner than the container # user, so git refuses to work in it ("detected dubious ownership"). Add it once to avoid duplicates. if command -v git >/dev/null 2>&1; then if ! git config --global --get-all safe.directory 2>/dev/null | grep -Fxq "$PWD"; then git config --global --add safe.directory "$PWD" fi fi if [ "$PLUGIN" = true ]; then step "Building Matterbridge from the ${BRANCH} branch..." # The branch defaults to dev; pass --main to install the stable branch. SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" INSTALL_SCRIPT="${SCRIPT_DIR}/install-matterbridge.sh" if [ ! -f "$INSTALL_SCRIPT" ]; then INSTALL_SCRIPT="/usr/local/bin/install-matterbridge.sh" fi if [ ! -f "$INSTALL_SCRIPT" ]; then INSTALL_SCRIPT=".devcontainer/install-matterbridge.sh" fi bash "$INSTALL_SCRIPT" "$BRANCH" "--$MODE" fi step "Installing the ${TARGET} dependencies..." if [ "$MODE" = "bun" ]; then [ -f package-lock.json ] && mv package-lock.json package-lock.json.bak || true bun install [ -f package-lock.json.bak ] && mv package-lock.json.bak package-lock.json || true else npm install --no-fund --no-audit fi if [ "$PLUGIN" = true ]; then step "Linking Matterbridge..." if [ "$MODE" = "bun" ]; then if ! bun link matterbridge; then step "Retrying link with elevated permissions..." sudo bun link matterbridge sudo chown -R bun:bun ./node_modules fi else if ! npm link matterbridge --no-fund --no-audit; then step "Retrying link with elevated permissions..." sudo npm link matterbridge --no-fund --no-audit sudo chown -R node:node ./node_modules fi fi fi step "Building the ${TARGET}..." if [ "$MODE" = "bun" ]; then bun run build else npm run build fi if [ "$PLUGIN" = true ] || [ "$MATTERBRIDGE" = true ]; then step "Checking for the ${TARGET} frontend..." if [ -f apps/frontend/package.json ]; then step "Building the ${TARGET} frontend..." cd apps/frontend # When apps/frontend is a workspace (matterbridge), its dependencies are already installed by the root install. if node -e "process.exit(require('../../package.json').workspaces?.includes('apps/frontend') ? 0 : 1)"; then if [ "$MODE" = "bun" ]; then bun run build; else npm run build; fi elif [ "$MODE" = "bun" ]; then [ -f package-lock.json ] && mv package-lock.json package-lock.json.bak || true bun install && bun run build [ -f package-lock.json.bak ] && mv package-lock.json.bak package-lock.json || true else npm install --no-fund --no-audit && npm run build fi cd ../.. fi fi if [ "$PLUGIN" = true ]; then step "Adding the plugin to Matterbridge..." if [ "$MODE" = "bun" ]; then bun run add else npm run add fi fi step "Checking for outdated packages..." if [ "$MODE" = "bun" ]; then bun outdated || true else npm outdated || true fi step "Post create setup completed!"