diff --git a/Scripts/r.sh b/Scripts/r.sh index bc5edbe..ad87cb4 100644 --- a/Scripts/r.sh +++ b/Scripts/r.sh @@ -33,6 +33,36 @@ EOF chmod +x "$R_BIN_DIR/$target_name" } +resolve_non_wrapper_command() { + local target_name="$1" + local original_path="$PATH" + local filtered_parts=() + local filtered_path="" + local resolved_path="" + local part + + IFS=':' read -r -a filtered_parts <<< "$PATH" + + local kept_parts=() + for part in "${filtered_parts[@]}"; do + if [ -n "$part" ] && [ "$part" != "$R_BIN_DIR" ]; then + kept_parts+=("$part") + fi + done + + filtered_path=$(IFS=:; printf '%s' "${kept_parts[*]}") + resolved_path=$(PATH="$filtered_path" command -v "$target_name" 2>/dev/null || true) + + PATH="$original_path" + + if [ -z "$resolved_path" ]; then + echo -e "${RED} ERROR:${NC} Could not resolve system command for $target_name outside $R_BIN_DIR" + exit 1 + fi + + printf '%s\n' "$resolved_path" +} + echo -e "${BLUE} LOG:${YELLOW} Detecting R installation strategy for $OS...${NC}" if is_arch_family; then @@ -51,8 +81,8 @@ if is_arch_family; then echo -e "${GREEN} SUCCESS:${NC} R installed via Pacman." R_BIN="R" - SYSTEM_R_BIN="$(command -v R)" - SYSTEM_RSCRIPT_BIN="$(command -v Rscript)" + SYSTEM_R_BIN="$(resolve_non_wrapper_command R)" + SYSTEM_RSCRIPT_BIN="$(resolve_non_wrapper_command Rscript)" write_r_wrapper "R" "exec \"$SYSTEM_R_BIN\"" write_r_wrapper "Rscript" "exec \"$SYSTEM_RSCRIPT_BIN\"" @@ -117,8 +147,8 @@ elif is_fedora_family; then fi R_BIN="R" - SYSTEM_R_BIN="$(command -v R)" - SYSTEM_RSCRIPT_BIN="$(command -v Rscript)" + SYSTEM_R_BIN="$(resolve_non_wrapper_command R)" + SYSTEM_RSCRIPT_BIN="$(resolve_non_wrapper_command Rscript)" write_r_wrapper "R" "exec \"$SYSTEM_R_BIN\"" write_r_wrapper "Rscript" "exec \"$SYSTEM_RSCRIPT_BIN\"" @@ -132,8 +162,8 @@ elif is_macos; then fi R_BIN="R" - SYSTEM_R_BIN="$(command -v R)" - SYSTEM_RSCRIPT_BIN="$(command -v Rscript)" + SYSTEM_R_BIN="$(resolve_non_wrapper_command R)" + SYSTEM_RSCRIPT_BIN="$(resolve_non_wrapper_command Rscript)" write_r_wrapper "R" "exec \"$SYSTEM_R_BIN\"" write_r_wrapper "Rscript" "exec \"$SYSTEM_RSCRIPT_BIN\""