#!/usr/bin/env bash # This is a metascript called by parent scripts to verify the execution environment. # It checks if the current shell is bash or not. If not, it terminates execution. if [ -z "${BASH_VERSION:-}" ]; then echo "Error: This script must be run using bash." >&2 exit 1 fi # Detect if the script is sourced is_sourced=false if [ -n "${BASH_SOURCE[0]:-}" ] && [ "${BASH_SOURCE[0]}" != "$0" ]; then is_sourced=true fi # Locate or download libraries so that sourced installers can use them BOOTSTRAP_DIR="${BOOTSTRAP_DIR:-$HOME/.config/bootstrap}" _SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd 2>/dev/null || pwd)" if [ -f "$_SCRIPT_DIR/lib/common.sh" ]; then # Dev/local mode: source directly from repo . "$_SCRIPT_DIR/lib/common.sh" . "$_SCRIPT_DIR/lib/platform.sh" . "$_SCRIPT_DIR/lib/shell_config.sh" elif [ -f "$BOOTSTRAP_DIR/lib/common.sh" ]; then # Installed mode: source from bootstrap dir . "$BOOTSTRAP_DIR/lib/common.sh" . "$BOOTSTRAP_DIR/lib/platform.sh" . "$BOOTSTRAP_DIR/lib/shell_config.sh" else # Standalone/remote mode: download to a temp directory and source export BOOTSTRAP_TMP_DIR BOOTSTRAP_TMP_DIR="$(mktemp -d)" trap 'rm -rf "$BOOTSTRAP_TMP_DIR"' EXIT _BASE_URL="https://git.adityagupta.dev/sortedcord/bootstrap/raw/branch/master" _LIBS=("lib/common.sh" "lib/platform.sh" "lib/shell_config.sh") _has_curl=false _has_wget=false if command -v curl >/dev/null 2>&1; then _has_curl=true elif command -v wget >/dev/null 2>&1; then _has_wget=true else echo "Error: Neither curl nor wget is installed to download libraries." >&2 exit 1 fi if [ "$_has_curl" = true ]; then _curl_args=() for _lib in "${_LIBS[@]}"; do mkdir -p "$BOOTSTRAP_TMP_DIR/$(dirname "$_lib")" _curl_args+=("-o" "$BOOTSTRAP_TMP_DIR/$_lib" "$_BASE_URL/$_lib") done curl -fsSL "${_curl_args[@]}" 2>/dev/null elif [ "$_has_wget" = true ]; then _pids=() for _lib in "${_LIBS[@]}"; do mkdir -p "$BOOTSTRAP_TMP_DIR/$(dirname "$_lib")" wget -qO "$BOOTSTRAP_TMP_DIR/$_lib" "$_BASE_URL/$_lib" 2>/dev/null & _pids+=($!) done for _pid in "${_pids[@]}"; do wait "$_pid" || true done fi if [ -f "$BOOTSTRAP_TMP_DIR/lib/common.sh" ]; then . "$BOOTSTRAP_TMP_DIR/lib/common.sh" . "$BOOTSTRAP_TMP_DIR/lib/platform.sh" . "$BOOTSTRAP_TMP_DIR/lib/shell_config.sh" else echo "Error: Failed to download bootstrap libraries." >&2 exit 1 fi fi # Install/update the bootstrap loader and download all necessary files install_bootstrap() { local target_files=() [ -f "$HOME/.bashrc" ] && target_files+=("$HOME/.bashrc") local routes_dir="$HOME/.config/bootstrap" mkdir -p "$routes_dir" # List of all files to download/copy local files=( "VERSION" "b.sh" "routes.sh" "lib/common.sh" "lib/platform.sh" "lib/shell_config.sh" "commands/help.sh" "commands/con.sh" "commands/uninstall.sh" "commands/up.sh" ) if [ -f "$_SCRIPT_DIR/b.sh" ] && [ -f "$_SCRIPT_DIR/routes.sh" ]; then log_info "Using local files from repository..." for file in "${files[@]}"; do mkdir -p "$(dirname "$routes_dir/$file")" if [ -f "$_SCRIPT_DIR/$file" ]; then cp "$_SCRIPT_DIR/$file" "$routes_dir/$file" fi done # Also copy installers if they exist locally if [ -d "$_SCRIPT_DIR/installers" ]; then mkdir -p "$routes_dir/installers" cp -r "$_SCRIPT_DIR/installers/"* "$routes_dir/installers/" fi else log_info "Downloading bootstrap scripts..." local base_url="https://git.adityagupta.dev/sortedcord/bootstrap/raw/branch/master" local has_curl=false local has_wget=false if command -v curl >/dev/null 2>&1; then has_curl=true elif command -v wget >/dev/null 2>&1; then has_wget=true else log_error "Neither curl nor wget is installed." exit 1 fi if [ "$has_curl" = true ]; then local curl_args=() for file in "${files[@]}"; do mkdir -p "$(dirname "$routes_dir/$file")" curl_args+=("-o" "$routes_dir/$file" "$base_url/$file") done if ! curl -fsSL "${curl_args[@]}"; then log_error "Failed to download bootstrap scripts." exit 1 fi elif [ "$has_wget" = true ]; then local pids=() for file in "${files[@]}"; do mkdir -p "$(dirname "$routes_dir/$file")" local file_url="$base_url/$file" wget -qO "$routes_dir/$file" "$file_url" & pids+=($!) done local err=0 for pid in "${pids[@]}"; do if ! wait "$pid"; then err=1 fi done if [ "$err" -ne 0 ]; then log_error "Failed to download some bootstrap scripts." exit 1 fi fi fi # Set up shell configuration files for config_file in "${target_files[@]}"; do # 1. Clean up old embedded function block if it exists (from previous setup) remove_block "$config_file" "bootstrap-cli b function" # 2. Clean up old loader block if it exists remove_block "$config_file" "bootstrap-cli setup" # 3. Append the new lightweight loader block log_info "Adding bootstrap loader to $config_file..." cat << 'EOF' >> "$config_file" # >>> bootstrap-cli setup >>> export BOOTSTRAP_DIR="$HOME/.config/bootstrap" [ -f "$BOOTSTRAP_DIR/b.sh" ] && . "$BOOTSTRAP_DIR/b.sh" # <<< bootstrap-cli setup <<< EOF done # Initialize the last update timestamp to prevent immediate update on first execution (Fix 2) local last_update_file="$routes_dir/.last_b_update" date +%s 2>/dev/null > "$last_update_file" || date +%s > "$last_update_file" # Set up pre-commit hook if in a git repository locally if [ -d "$_SCRIPT_DIR/.git" ]; then log_info "Setting up git pre-commit hook..." mkdir -p "$_SCRIPT_DIR/.git/hooks" if [ -f "$_SCRIPT_DIR/scripts/pre-commit" ]; then cp "$_SCRIPT_DIR/scripts/pre-commit" "$_SCRIPT_DIR/.git/hooks/pre-commit" chmod +x "$_SCRIPT_DIR/.git/hooks/pre-commit" log_success "Pre-commit hook installed to .git/hooks/pre-commit" fi fi } # Only execute installation if not sourced (Fix 3) if [ "$is_sourced" = false ]; then clear 2>/dev/null || true # Locate or download pixel_art.ansi and VERSION _art_file="$_SCRIPT_DIR/pixel_art.ansi" _version_file="$_SCRIPT_DIR/VERSION" if [ ! -f "$_art_file" ]; then if [ -n "${BOOTSTRAP_TMP_DIR:-}" ] && [ -d "$BOOTSTRAP_TMP_DIR" ]; then _art_file="$BOOTSTRAP_TMP_DIR/pixel_art.ansi" _version_file="$BOOTSTRAP_TMP_DIR/VERSION" _base_url="${_BASE_URL:-https://git.adityagupta.dev/sortedcord/bootstrap/raw/branch/master}" if [ ! -f "$_art_file" ]; then if command -v curl >/dev/null 2>&1; then curl -fsSL -o "$_art_file" "$_base_url/pixel_art.ansi" 2>/dev/null curl -fsSL -o "$_version_file" "$_base_url/VERSION" 2>/dev/null elif command -v wget >/dev/null 2>&1; then wget -qO "$_art_file" "$_base_url/pixel_art.ansi" 2>/dev/null wget -qO "$_version_file" "$_base_url/VERSION" 2>/dev/null fi fi fi fi if [ -f "$_art_file" ]; then # Calculate terminal width and center the logo _cols="" if command -v tput >/dev/null 2>&1; then _cols=$(tput cols 2>/dev/null) fi if [ -z "$_cols" ] || ! [ "$_cols" -gt 0 ] 2>/dev/null; then if command -v stty >/dev/null 2>&1; then _cols=$(stty size 2>/dev/null | cut -d' ' -f2) fi fi if [ -z "$_cols" ] || ! [ "$_cols" -gt 0 ] 2>/dev/null; then _cols="${COLUMNS:-0}" fi _logo_width=64 if [ -n "$_cols" ] && [ "$_cols" -ge "$_logo_width" ] 2>/dev/null; then _padding_width=$(( (_cols - _logo_width) / 2 )) _padding="" if [ "$_padding_width" -gt 0 ]; then _padding=$(printf "%${_padding_width}s" "") fi sed "s/^/$_padding/" "$_art_file" echo cat << 'EOF' | sed "s/^/$_padding/" ▀█████████▄ ▄██████▄ ▄██████▄ ███ ███ ███ ███ ███ ███ ███ ▀█████████▄ ███ ███ ███ ███ ███ ███ ▀███▀▀██ ▄███▄▄▄██▀ ███ ███ ███ ███ ███ ▀ ▀▀███▀▀▀██▄ ███ ███ ███ ███ ███ ███ ██▄ ███ ███ ███ ███ ███ ███ ███ ███ ███ ███ ███ ███ ▄█████████▀ ▀██████▀ ▀██████▀ ▄████▀ ▄████████ ███ ▄████████ ▄████████ ▄███████▄ ███ ███ ▀█████████▄ ███ ███ ███ ███ ███ ███ ███ █▀ ▀███▀▀██ ███ ███ ███ ███ ███ ███ ███ ███ ▀ ▄███▄▄▄▄██▀ ███ ███ ███ ███ ▀███████████ ███ ▀▀███▀▀▀▀▀ ▀███████████ ▀█████████▀ ███ ███ ▀███████████ ███ ███ ███ ▄█ ███ ███ ███ ███ ███ ███ ███ ▄████████▀ ▄████▀ ███ ███ ███ █▀ ▄████▀ ███ ███ EOF echo # Display centered version of bootstrap in bold _version="" if [ -f "$_version_file" ]; then _version=$(cat "$_version_file" | tr -d '\r\n') fi if [ -z "$_version" ]; then _version="1.0.7" # Fallback matching VERSION file fi _version_str="v$_version" _version_len=${#_version_str} _version_padding_width=$(( (_cols - _version_len) / 2 )) _version_padding="" if [ "$_version_padding_width" -gt 0 ]; then _version_padding=$(printf "%${_version_padding_width}s" "") fi printf "%s\033[1m%s\033[0m\n" "$_version_padding" "$_version_str" echo # Display centered aqua blue progress bar _bar_width=40 _bar_padding_width=$(( (_cols - (_bar_width + 6)) / 2 )) _bar_padding="" if [ "$_bar_padding_width" -gt 0 ]; then _bar_padding=$(printf "%${_bar_padding_width}s" "") fi _filled_all="████████████████████████████████████████" _empty_all="░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░" # Hide cursor printf "\033[?25l" for i in $(seq 1 40); do _filled_part="${_filled_all:0:i}" _empty_part="${_empty_all:i}" _pct=$(( i * 100 / 40 )) printf "\r%s\033[38;2;0;210;255m%s\033[90m%s\033[0m %3d%%" "$_bar_padding" "$_filled_part" "$_empty_part" "$_pct" sleep 0.05 done # Restore cursor and print newline printf "\033[?25h\n\n" fi fi install_bootstrap # Load the b function immediately in the current subshell if [ -f "$HOME/.config/bootstrap/b.sh" ]; then . "$HOME/.config/bootstrap/b.sh" fi # Handle sourcing the shell configuration file or printing instructions (Fix 1) echo log_success "Bootstrap CLI installed successfully!" log_info "To start using the 'b' command in this terminal session, run:" echo " source ~/.bashrc" else # Sourced mode (e.g., when sourced by installers or manually by user) # Load the b function in the current shell context if [ -f "$HOME/.config/bootstrap/b.sh" ]; then . "$HOME/.config/bootstrap/b.sh" fi fi