mirror of
https://github.com/buildplan/du_setup.git
synced 2025-12-17 17:55:35 +00:00
improvements to bashrc function
This commit is contained in:
parent
2cb3cc6a0e
commit
c1e57a3858
59
du_setup.sh
59
du_setup.sh
@ -1050,10 +1050,15 @@ cleanup_provider_packages() {
|
||||
fi
|
||||
}
|
||||
|
||||
# --- (This function goes AFTER cleanup_provider_packages() and BEFORE setup_user()) ---
|
||||
|
||||
configure_custom_bashrc() {
|
||||
local USER_HOME="$1"
|
||||
local USERNAME="$2"
|
||||
local BASHRC_PATH="$USER_HOME/.bashrc"
|
||||
local temp_source_bashrc=""
|
||||
|
||||
trap 'rm -f "$temp_source_bashrc" 2>/dev/null' RETURN ERR INT TERM
|
||||
|
||||
if ! confirm "Replace default .bashrc for '$USERNAME' with a custom one?" "n"; then
|
||||
print_info "Skipping custom .bashrc configuration."
|
||||
@ -1061,11 +1066,17 @@ configure_custom_bashrc() {
|
||||
return 0
|
||||
fi
|
||||
|
||||
print_info "Creating custom .bashrc for '$USERNAME'..."
|
||||
log "Creating custom .bashrc at $BASHRC_PATH"
|
||||
print_info "Preparing custom .bashrc for '$USERNAME'..."
|
||||
|
||||
local bashrc_content
|
||||
bashrc_content=$(cat <<'EOF'
|
||||
temp_source_bashrc=$(mktemp "/tmp/custom_bashrc_source.XXXXXX")
|
||||
if [[ -z "$temp_source_bashrc" || ! -f "$temp_source_bashrc" ]]; then
|
||||
print_error "Failed to create temporary file for .bashrc content."
|
||||
log "Error: mktemp failed for bashrc source."
|
||||
return 0
|
||||
fi
|
||||
chmod 600 "$temp_source_bashrc"
|
||||
|
||||
if ! cat > "$temp_source_bashrc" <<'EOF'; then
|
||||
# shellcheck shell=bash
|
||||
# ===================================================================
|
||||
# Universal Portable .bashrc for Modern Terminals
|
||||
@ -2228,49 +2239,51 @@ alias commands='compgen -A function -A alias | grep -v "^_" | sort | column'
|
||||
# - Consider moving rarely-used functions to separate files
|
||||
# - Use 'time bash -i -c exit' to measure startup time
|
||||
EOF
|
||||
) # End of bashrc_content assignment
|
||||
|
||||
# Back up existing .bashrc if it's not the default
|
||||
if [[ -f "$BASHRC_PATH" ]] && ! grep -q "generated by /usr/sbin/adduser" "$BASHRC_PATH" 2>/dev/null; then
|
||||
local BASHRC_BACKUP="$BASHRC_PATH.backup_$(date +%Y%m%d_%H%M%S)"
|
||||
print_info "Backing up existing non-default .bashrc to $BASHRC_BACKUP"
|
||||
cp "$BASHRC_PATH" "$BASHRC_BACKUP"
|
||||
log "Backed up existing .bashrc to $BASHRC_BACKUP"
|
||||
print_error "Failed to write .bashrc content to temporary file $temp_source_bashrc."
|
||||
log "Critical error: Failed to write bashrc content to $temp_source_bashrc."
|
||||
rm -f "$temp_source_bashrc" 2>/dev/null
|
||||
return 0
|
||||
fi
|
||||
|
||||
local temp_bashrc_path
|
||||
temp_bashrc_path="/tmp/custom_bashrc_for_${USERNAME}.txt"
|
||||
if ! printf '%s\n' "$bashrc_content" | tee "$BASHRC_PATH" > /dev/null; then
|
||||
log "Successfully created temporary .bashrc source at $temp_source_bashrc"
|
||||
|
||||
local temp_fallback_path="/tmp/custom_bashrc_for_${USERNAME}.txt"
|
||||
|
||||
if ! tee "$BASHRC_PATH" < "$temp_source_bashrc" > /dev/null; then
|
||||
print_error "Failed to automatically write custom .bashrc to $BASHRC_PATH."
|
||||
log "Error writing custom .bashrc for $USERNAME to $BASHRC_PATH (likely permissions issue)."
|
||||
|
||||
if printf '%s\n' "$bashrc_content" | tee "$temp_bashrc_path" > /dev/null; then
|
||||
chmod 644 "$temp_bashrc_path"
|
||||
if cp "$temp_source_bashrc" "$temp_fallback_path"; then
|
||||
chmod 644 "$temp_fallback_path"
|
||||
print_warning "ACTION REQUIRED: The custom .bashrc content has been saved to:"
|
||||
print_warning " ${temp_bashrc_path}"
|
||||
print_warning " ${temp_fallback_path}"
|
||||
print_info "After setup, please manually copy it:"
|
||||
print_info " sudo cp ${temp_bashrc_path} ${BASHRC_PATH}"
|
||||
print_info " sudo cp ${temp_fallback_path} ${BASHRC_PATH}"
|
||||
print_info " sudo chown ${USERNAME}:${USERNAME} ${BASHRC_PATH}"
|
||||
print_info " sudo chmod 644 ${BASHRC_PATH}"
|
||||
log "Saved custom .bashrc content to $temp_bashrc_path for manual installation."
|
||||
log "Saved custom .bashrc content to $temp_fallback_path for manual installation."
|
||||
else
|
||||
print_error "Also failed to save custom .bashrc content to $temp_bashrc_path."
|
||||
log "Critical error: Failed both writing to $BASHRC_PATH and saving to $temp_bashrc_path."
|
||||
print_error "Also failed to save custom .bashrc content to $temp_fallback_path."
|
||||
log "Critical error: Failed both writing to $BASHRC_PATH and copying $temp_source_bashrc to $temp_fallback_path."
|
||||
fi
|
||||
else
|
||||
if ! chown "$USERNAME:$USERNAME" "$BASHRC_PATH" || ! chmod 644 "$BASHRC_PATH"; then
|
||||
print_warning "Failed to set correct ownership/permissions on $BASHRC_PATH."
|
||||
log "Failed to chown/chmod $BASHRC_PATH"
|
||||
print_warning "ACTION REQUIRED: Please manually set ownership/permissions:"
|
||||
print_info " sudo cp ${temp_source_bashrc} ${BASHRC_PATH} # You might need this if the file is corrupted"
|
||||
print_info " sudo chown ${USERNAME}:${USERNAME} ${BASHRC_PATH}"
|
||||
print_info " sudo chmod 644 ${BASHRC_PATH}"
|
||||
temp_source_bashrc=""
|
||||
else
|
||||
print_success "Custom .bashrc created for '$USERNAME'."
|
||||
log "Custom .bashrc configuration completed for $USERNAME."
|
||||
rm -f "$temp_fallback_path" 2>/dev/null
|
||||
fi
|
||||
rm -f "$temp_bashrc_path" 2>/dev/null
|
||||
fi
|
||||
|
||||
rm -f "$temp_source_bashrc" 2>/dev/null
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user