--- unpack/opt/microsoft/rclient/3.4.3/bin/R/R.orig	2018-01-24 21:23:15.612866647 +0000
+++ unpack/opt/microsoft/rclient/3.4.3/bin/R/R	2018-01-24 21:23:41.049170514 +0000
@@ -29,17 +29,16 @@
 
 function setup_runtime()
 {
-    local expected_runtime=$(cat ${SCRIPT_DIR}/DEFAULT_RUNTIME)
-    local r_path="/opt/microsoft/ropen/${expected_runtime}/lib64/R"
+    local r_path="${CONDA_PREFIX}/lib/R"
     
     if [[ -d ${ML_SERVER_ROOT}/runtime/R ]]; then
         return 0
     fi
 
-    if [[ $(id -u) -ne 0 ]]; then
-      echo "FATAL: Sudo or Root permissions are needed to setup the runtime"
-      return 1
-    fi
+    # if [[ $(id -u) -ne 0 ]]; then
+    #   echo "FATAL: Sudo or Root permissions are needed to setup the runtime"
+    #   return 1
+    # fi
 
     if [[ ! -d ${ML_SERVER_ROOT}/runtime ]]; then
       mkdir ${ML_SERVER_ROOT}/runtime
@@ -56,7 +55,7 @@
       cp -r ${r_path} ${ML_SERVER_ROOT}/runtime/R
       return 0
     else
-      echo "FATAL: Could not find a sutable default R runtime. Please ensure Microsoft R Open ${EXPECTED_RUNTIME} is installed"
+      echo "FATAL: Could not find a sutable default R runtime. Please ensure Microsoft R Open is installed"
       return 1
     fi
 }
@@ -84,41 +83,40 @@
     return 1
   fi
 
-  local expected_runtime=$(cat ${SCRIPT_DIR}/DEFAULT_RUNTIME)
-  local r_path="/opt/microsoft/ropen/${expected_runtime}/lib64/R/bin/R"
-  local rscript_path="/opt/microsoft/ropen/${expected_runtime}/lib64/R/bin/Rscript"
-
-  if [[ -f /usr/bin/R ]]; then
-    local r_link=$(readlink -f /usr/bin/R)
-    echo "Found /usr/bin/R => ${r_link}"
+  local r_path="${CONDA_PREFIX}/lib/R/bin/R"
+  local rscript_path="${CONDA_PREFIX}/lib/R/bin/Rscript"
+
+  if [[ -f ${CONDA_PREFIX}/bin/R ]]; then
+    local r_link=$(readlink -f ${CONDA_PREFIX}/bin/R)
+    echo "Found ${CONDA_PREFIX}/bin/R => ${r_link}"
     if [[ ${r_link} == ${r_path} ]]; then
-      echo "Replacing /usr/bin/R with ${SCRIPT_DIR}/R"
-      rm /usr/bin/R
-      ln -s ${SCRIPT_DIR}/R /usr/bin/R
+      echo "Replacing ${CONDA_PREFIX}/bin/R with ${SCRIPT_DIR}/R"
+      rm ${CONDA_PREFIX}/bin/R
+      ln -s ${SCRIPT_DIR}/R ${CONDA_PREFIX}/bin/R
     fi
   fi
   
-  if [[ -f /usr/bin/Rscript ]]; then
-    local rscript_link=$(readlink -f /usr/bin/Rscript)
-    echo "Found /usr/bin/Rscript => ${rscript_link}"
+  if [[ -f ${CONDA_PREFIX}/bin/Rscript ]]; then
+    local rscript_link=$(readlink -f ${CONDA_PREFIX}/bin/Rscript)
+    echo "Found ${CONDA_PREFIX}/bin/Rscript => ${rscript_link}"
     if [[ ${rscript_link} == ${rscript_path} ]]; then
-      echo "Replacing /usr/bin/Rscript with ${SCRIPT_DIR}/Rscript"
-      rm /usr/bin/Rscript
-      ln -s ${SCRIPT_DIR}/Rscript /usr/bin/Rscript
+      echo "Replacing ${CONDA_PREFIX}/bin/Rscript with ${SCRIPT_DIR}/Rscript"
+      rm ${CONDA_PREFIX}/bin/Rscript
+      ln -s ${SCRIPT_DIR}/Rscript ${CONDA_PREFIX}/bin/Rscript
     fi
   fi
 
-  if [[ -f /usr/bin/mlserver-R-${ML_SERVER_VERSION} ]]; then
-    rm /usr/bin/mlserver-R-${ML_SERVER_VERSION}
+  if [[ -f ${CONDA_PREFIX}/bin/mlserver-R-${ML_SERVER_VERSION} ]]; then
+    rm ${CONDA_PREFIX}/bin/mlserver-R-${ML_SERVER_VERSION}
   fi
 
-  ln -s "${SCRIPT_DIR}/R" "/usr/bin/mlserver-R-${ML_SERVER_VERSION}"
+  ln -s "${SCRIPT_DIR}/R" "${CONDA_PREFIX}/bin/mlserver-R-${ML_SERVER_VERSION}"
 
-  if [[ -f /usr/bin/mlserver-Rscript-${ML_SERVER_VERSION} ]]; then
-    rm /usr/bin/mlserver-Rscript-${ML_SERVER_VERSION}
+  if [[ -f ${CONDA_PREFIX}/bin/mlserver-Rscript-${ML_SERVER_VERSION} ]]; then
+    rm ${CONDA_PREFIX}/bin/mlserver-Rscript-${ML_SERVER_VERSION}
   fi
 
-  ln -s "${SCRIPT_DIR}/Rscript" "/usr/bin/mlserver-Rscript-${ML_SERVER_VERSION}"
+  ln -s "${SCRIPT_DIR}/Rscript" "${CONDA_PREFIX}/bin/mlserver-Rscript-${ML_SERVER_VERSION}"
 }
 
 function unsymlink_mlserver()
@@ -128,37 +126,36 @@
     return 1
   fi
 
-  local expected_runtime=$(cat ${SCRIPT_DIR}/DEFAULT_RUNTIME)
-  local r_path="/opt/microsoft/ropen/${expected_runtime}/lib64/R/bin/R"
-  local r_script_path="/opt/microsoft/ropen/${expected_runtime}/lib64/R/bin/Rscript"
-
-  if [[ -f /usr/bin/R ]]; then
-    local r_link=$(readlink -f /usr/bin/R)
-    echo "Found /usr/bin/R => ${r_link}"
+  local r_path="${CONDA_PREFIX}/lib/R/bin/R"
+  local r_script_path="${CONDA_PREFIX}/lib/R/bin/Rscript"
+
+  if [[ -f ${CONDA_PREFIX}/bin/R ]]; then
+    local r_link=$(readlink -f ${CONDA_PREFIX}/bin/R)
+    echo "Found ${CONDA_PREFIX}/bin/R => ${r_link}"
     if [[ ${r_link} == ${SCRIPT_DIR}/R ]]; then
-      echo "Replacing /usr/bin/R with ${r_path}"
-      rm /usr/bin/R
-      ln -s ${r_path} /usr/bin/R
+      echo "Replacing ${CONDA_PREFIX}/bin/R with ${r_path}"
+      rm ${CONDA_PREFIX}/bin/R
+      ln -s ${r_path} ${CONDA_PREFIX}/bin/R
     fi
   fi
 
-  if [[ -f /usr/bin/Rscript ]]; then
-    local r_script_link=$(readlink -f /usr/bin/Rscript)
-    echo "Found /usr/bin/Rscript => ${r_script_link}"
+  if [[ -f ${CONDA_PREFIX}/bin/Rscript ]]; then
+    local r_script_link=$(readlink -f ${CONDA_PREFIX}/bin/Rscript)
+    echo "Found ${CONDA_PREFIX}/bin/Rscript => ${r_script_link}"
     if [[ ${r_script_link} == ${SCRIPT_DIR}/Rscript ]]; then
-      echo "Replacing /usr/bin/Rscript with ${r_script_path}"
-      rm /usr/bin/Rscript
-      ln -s ${r_script_path} /usr/bin/Rscript
+      echo "Replacing ${CONDA_PREFIX}/bin/Rscript with ${r_script_path}"
+      rm ${CONDA_PREFIX}/bin/Rscript
+      ln -s ${r_script_path} ${CONDA_PREFIX}/bin/Rscript
     fi
   fi
 
-  if [[ -f /usr/bin/mlserver-R-${ML_SERVER_VERSION} ]]; then
-    rm /usr/bin/mlserver-R-${ML_SERVER_VERSION}
+  if [[ -f ${CONDA_PREFIX}/bin/mlserver-R-${ML_SERVER_VERSION} ]]; then
+    rm ${CONDA_PREFIX}/bin/mlserver-R-${ML_SERVER_VERSION}
   fi
 
 
-  if [[ -f /usr/bin/mlserver-Rscript-${ML_SERVER_VERSION} ]]; then
-    rm /usr/bin/mlserver-Rscript-${ML_SERVER_VERSION}
+  if [[ -f ${CONDA_PREFIX}/bin/mlserver-Rscript-${ML_SERVER_VERSION} ]]; then
+    rm ${CONDA_PREFIX}/bin/mlserver-Rscript-${ML_SERVER_VERSION}
   fi
 }
 
