diff --git a/du_setup.sh b/du_setup.sh index d2a00f3..47f8f84 100644 --- a/du_setup.sh +++ b/du_setup.sh @@ -1064,15 +1064,8 @@ configure_custom_bashrc() { print_info "Creating custom .bashrc for '$USERNAME'..." log "Creating custom .bashrc at $BASHRC_PATH" - # Back up existing .bashrc if it's not the default skeleton - 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" - fi - - if ! tee "$BASHRC_PATH" > /dev/null <<'EOF' + local bashrc_content + bashrc_content=$(cat <<'EOF' # shellcheck shell=bash # =================================================================== # Universal Portable .bashrc for Modern Terminals @@ -2235,20 +2228,50 @@ 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 - then - print_error "Failed to write custom .bashrc to $BASHRC_PATH." - log "Failed to write custom .bashrc." - return 1 +) # 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" fi - # Set correct ownership and permissions - 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" + 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 + 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" + print_warning "ACTION REQUIRED: The custom .bashrc content has been saved to:" + print_warning " ${temp_bashrc_path}" + print_info "After setup, please manually copy it:" + print_info " sudo cp ${temp_bashrc_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." + 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." + 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 chown ${USERNAME}:${USERNAME} ${BASHRC_PATH}" + print_info " sudo chmod 644 ${BASHRC_PATH}" + else + print_success "Custom .bashrc created for '$USERNAME'." + log "Custom .bashrc configuration completed for $USERNAME." + fi + rm -f "$temp_bashrc_path" 2>/dev/null fi - print_success "Custom .bashrc created for '$USERNAME'." - log "Custom .bashrc configuration completed for $USERNAME." + return 0 } # --- USER INTERACTION ---