#!/usr/bin/env bash set -o errexit set -o nounset set -o pipefail SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" # shellcheck source=00-platform-guard.inc source "$SCRIPT_DIR/00-platform-guard.inc" SOURCE_FILE="$SCRIPT_DIR/../files/bashrc.d/ailab.sh" PROFILE_DIR="/root/.bashrc.d" PROFILE_FILE="$PROFILE_DIR/ailab.sh" BASHRC="/root/.bashrc" SOURCE_LINE='[[ -f /root/.bashrc.d/ailab.sh ]] && source /root/.bashrc.d/ailab.sh' backup_file() { local path="$1" local backup backup="${path}.$(date '+%Y%m%d-%H%M%S').bak" install -m 0644 "$path" "$backup" printf 'INFO: backed up %s to %s\n' "$path" "$backup" } if ((EUID != 0)); then printf 'CRITICAL: shell profile setup must run as root\n' >&2 exit 2 fi require_supported_ubuntu if [[ ! -r "$SOURCE_FILE" ]]; then printf 'CRITICAL: shell profile source is missing: %s\n' "$SOURCE_FILE" >&2 exit 2 fi install -d -m 0755 "$PROFILE_DIR" if [[ ! -f "$PROFILE_FILE" ]] || ! cmp -s "$SOURCE_FILE" "$PROFILE_FILE"; then if [[ -f "$PROFILE_FILE" ]]; then backup_file "$PROFILE_FILE" fi install -m 0644 "$SOURCE_FILE" "$PROFILE_FILE" printf 'OK: installed %s\n' "$PROFILE_FILE" else printf 'OK: shell profile is already current\n' fi if [[ ! -f "$BASHRC" ]]; then install -m 0644 /dev/null "$BASHRC" fi source_count="$(grep -Fxc "$SOURCE_LINE" "$BASHRC" || true)" if [[ "$source_count" != "1" ]]; then tmp_bashrc="$(mktemp)" trap 'rm -f "$tmp_bashrc"' EXIT grep -Fvx "$SOURCE_LINE" "$BASHRC" >"$tmp_bashrc" || true printf '\n%s\n' "$SOURCE_LINE" >>"$tmp_bashrc" backup_file "$BASHRC" install -m 0644 "$tmp_bashrc" "$BASHRC" printf 'OK: configured %s to source the AI lab profile exactly once\n' "$BASHRC" else printf 'OK: %s already sources the AI lab profile exactly once\n' "$BASHRC" fi