# cmux Cloud devbox interactive shell setup (/etc/cmux/bashrc).
# Sourced from /etc/bash.bashrc, /etc/skel/.bashrc, /root/.bashrc, and
# /home/cmux/.bashrc.
case $- in *i*) ;; *) return ;; esac
# A login shell can reach this file through both system and user rc files.
if [[ ${__cmux_bashrc_loaded:-} ]]; then
  # Ubuntu's per-user rc can replace the system PS1 between the two sources.
  [ -r /etc/cmux/prompt.bash ] && . /etc/cmux/prompt.bash
  return
fi
__cmux_bashrc_loaded=1

# PTYs from the sandbox API do not reliably inherit these.
export LANG="${LANG:-C.UTF-8}"
export USER="${USER:-$(id -un)}"
export HOME="${HOME:-$(getent passwd "$(id -u)" | cut -d: -f6)}"

# Keep a persistent HOME's terminfo from shadowing cmux's capabilities. Login
# shells source the same file through /etc/profile.d; this covers non-login
# interactive shells opened by the session daemon.
[ -f /etc/profile.d/cmux-terminfo.sh ] && . /etc/profile.d/cmux-terminfo.sh

# First interactive shell on a fresh HOME (the persistent volume shadows the
# image's /root): seed history so ble.sh ghost text completes agent launches.
if [ ! -f "$HOME/.bash_history" ] && [ -f /etc/cmux/seed-history ]; then
  cp /etc/cmux/seed-history "$HOME/.bash_history" 2>/dev/null || true
fi

# Seed ble.sh's per-TERM tput caches (baked by the Dockerfile for the TERMs
# cmux uses) so no shell prints "ble/term.sh: updating tput cache ... done"
# into the pane before its first prompt. ble.sh uses ~/.cache only when it
# already exists (else it falls back to <blesh>/cache.d/<uid>, seeded in the
# image). Per file, not per directory: a durable home carries the cache dir of
# an older image (other ble version dir, or entries stale against a newer
# lib/init-term.sh — ble.sh regenerates and prints unless the cache file is
# NEWER than that file), so copy each seed entry that is missing or stale.
# Plain cp (not -a) stamps now, which always beats init-term.sh.
if [ -d /etc/cmux/blesh-cache-seed/blesh ]; then
  __cmux_cache_base="${XDG_CACHE_HOME:-$HOME/.cache}"
  for __cmux_seed in /etc/cmux/blesh-cache-seed/blesh/*/term.*; do
    [ -f "$__cmux_seed" ] || continue
    __cmux_dst="$__cmux_cache_base/${__cmux_seed#/etc/cmux/blesh-cache-seed/}"
    if [ ! -f "$__cmux_dst" ] || [ /usr/local/share/blesh/lib/init-term.sh -nt "$__cmux_dst" ]; then
      mkdir -p "${__cmux_dst%/*}" 2>/dev/null \
        && cp "$__cmux_seed" "$__cmux_dst" 2>/dev/null \
        || true
    fi
  done
  unset __cmux_cache_base __cmux_seed __cmux_dst
fi

if [ -f /usr/local/share/blesh/ble.sh ] && [ -z "${BLE_VERSION:-}" ]; then
  # Attach at the first prompt, after the user's .bashrc can customize PS1.
  # ble.sh uses XDG_RUNTIME_DIR for per-process temporary files. Cloud VMs
  # expose /run/user/<uid> there, but systemd can remove that directory when a
  # desktop/session owner disconnects while a durable terminal keeps running.
  # Give ble.sh a private, boot-scoped runtime root instead. /tmp is cleaned
  # on reboot, so killed shells cannot accumulate durable per-process files;
  # unlike /run/user it also survives a desktop/session teardown. Restore the
  # caller's value so other programs retain the VM's normal desktop session.
  __cmux_blesh_runtime_dir="/tmp/cmux-blesh-runtime-${UID}"
  __cmux_blesh_had_xdg_runtime_dir=${XDG_RUNTIME_DIR+x}
  __cmux_blesh_saved_xdg_runtime_dir=${XDG_RUNTIME_DIR-}
  __cmux_blesh_runtime_ready=
  if mkdir -m 700 "$__cmux_blesh_runtime_dir" 2>/dev/null; then
    __cmux_blesh_runtime_ready=1
  elif [ -d "$__cmux_blesh_runtime_dir" ] && [ ! -L "$__cmux_blesh_runtime_dir" ] \
    && [ -O "$__cmux_blesh_runtime_dir" ] && [ "$(stat -c %a "$__cmux_blesh_runtime_dir" 2>/dev/null)" = 700 ]; then
    __cmux_blesh_runtime_ready=1
  fi
  if [[ $__cmux_blesh_runtime_ready ]]; then
    export XDG_RUNTIME_DIR="$__cmux_blesh_runtime_dir"
  else
    # If HOME is temporarily unavailable, make ble.sh choose its /tmp
    # fallback rather than binding a live shell to a session-owned directory.
    unset XDG_RUNTIME_DIR
  fi
  source /usr/local/share/blesh/ble.sh --attach=prompt
  if [[ $__cmux_blesh_had_xdg_runtime_dir ]]; then
    export XDG_RUNTIME_DIR="$__cmux_blesh_saved_xdg_runtime_dir"
  else
    unset XDG_RUNTIME_DIR
  fi
  unset __cmux_blesh_had_xdg_runtime_dir __cmux_blesh_saved_xdg_runtime_dir
  unset __cmux_blesh_runtime_dir __cmux_blesh_runtime_ready
  bleopt prompt_eol_mark= exec_errexit_mark= exec_elapsed_mark= exec_exit_mark=
  # Minimal integration: token highlighting off entirely (the stock faces
  # paint colored backgrounds under mistyped commands and mid-word input,
  # which reads as noise on dark themes). Keep only the history ghost text,
  # foreground gray.
  bleopt highlight_syntax= highlight_filename= highlight_variable=
  ble-face auto_complete=fg=245
  # Alt+Backspace (Option+Delete on macOS) word delete. Once ble.sh identifies
  # the terminal from its DA2 reply it turns on xterm modifyOtherKeys, and from
  # then on it neither keeps the legacy ESC DEL binding nor decodes the
  # modifyOtherKeys form (CSI 27;3;127~) back to M-C-?, so the key does nothing
  # (ble.sh nightly, also reproducible under tmux). Keep the terminal on the
  # legacy encoding, which Ghostty and the cmux cloud pane send by default, and
  # bind both backspace forms explicitly.
  bleopt term_modifyOtherKeys_internal=0 term_modifyOtherKeys_external=0
  ble-bind -f 'M-C-?' kill-backward-cword
  ble-bind -f 'M-C-h' kill-backward-cword
fi

# Users can override PS1 later in ~/.bashrc. The default reads the current
# machine name with Bash builtins and no Git scan.
[ -r /etc/cmux/prompt.bash ] && . /etc/cmux/prompt.bash

HISTSIZE=50000
HISTFILESIZE=50000
shopt -s histappend

alias ls='ls --color=auto'
alias grep='grep --color=auto'
alias g=git
