#!/usr/bin/env bash

# ---------------------------------------------------------
# Configure R Alternatives
#
# Script to automatically configure R installation(s)
# to be managed by update-alternatives.
#
# Currently supports native R and Architect R:
# https://www.getarchitect.io/
#
# Use at your own risk. If something breaks let me know.
#
# Author: Daan Seynaeve <daan.seynaeve at openanalytics.eu>
# Date: Wed 02 May 2018
# Last updated: Fri 28 Jun 2024
# ---------------------------------------------------------

if [ $EUID != 0 ]; then
	echo "running as non-root, trying to elevate via sudo..."
    	sudo "$0" "$@"
    	exit $?
fi

ARCHITECT=/opt/architect
UNSTABLE=$ARCHITECT/architect-unstable
STABLE=$ARCHITECT/architect-stable

ARCHITECT_R=$ARCHITECT/plugins/eu.openanalytics.architect.r.server.gtk.linux/R
UNSTABLE_R=$UNSTABLE/plugins/eu.openanalytics.architect.r.server.gtk.linux_unstable/R
STABLE_R=$STABLE/plugins/eu.openanalytics.architect.r.server.gtk.linux_stable/R

R_LIBS_USER=/home/$SUDO_USER/.architect/workspace/.metadata/.r/eplugin-local-eu.openanalytics.r.server/user-library

INFO='\033[0;32m'
ROUT='\e[34m'
NC='\033[0m' # No Color


if [[ $* == *--help* ]]; then
	
	echo "Description:"
	echo "  Script to automatically configure R installation(s)"
	echo "  to be managed by update-alternatives."
	echo "Options:"
	echo -e "  --clean\t\tremove any existing alternatives and scripts."
	echo -e "  --help\t\tshow this info."
	exit 0
fi

if [[ $* == *--clean* ]]; then
	echo "Removing existing alternatives"
	update-alternatives --remove-all R
	update-alternatives --remove-all Rscript

	rm -fv /usr/local/bin/R-architect.sh
	rm -fv /usr/local/bin/Rscript-architect.sh
	rm -fv /usr/local/bin/R-architect-stable.sh
	rm -fv /usr/local/bin/Rscript-architect-unstable.sh
	rm -fv /usr/local/bin/R-architect-stable.sh
	rm -fv /usr/local/bin/Rscript-architect-unstable.sh

	exit 0
fi

if [ ! -d /opt/architect ]; then
	echo "Architect is not installed (in $ARCHITECT)"
	exit 1
fi

# ---------------------------------------------------------
# R executable
# ---------------------------------------------------------

if [ -f /usr/bin/R ] && [ ! -L /usr/bin/R ]; then
	echo "/usr/bin/R is not a link and needs to be removed"
	read -p "Continue? (Yn) " -n 1 -r
	echo
	if [[ $REPLY =~ ^[Nn]$ ]]; then
		echo "please move or remove /usr/bin/R and run again"
		exit 1
	else
		rm /usr/bin/R
	fi
fi

# Newer Architect
if [ -d $ARCHITECT/plugins ]; then

	ARCHITECT_VERSION=$(ls $ARCHITECT/plugins | grep architect | sed -Ens s/.*architect_\(.*\).jar/\\1/p)

	echo "Architect ($ARCHITECT_VERSION) found at $ARCHITECT"
	echo -e "${INFO}> Install Architect R alternative${NC}"

	# /opt/architect/plugins/eu.openanalytics.architect.r.server.gtk.linux/R
	echo "Replacing existing version of /usr/local/bin/R-architect.sh"
	echo "#!/usr/bin/env bash" > /usr/local/bin/R-architect.sh
	echo "R_LIBS_SITE=$ARCHITECT_R/../../eu.openanalytics.architect.r.libs.rj.gtk.linux/libs R_LIBS_USER=$R_LIBS_USER $ARCHITECT_R/bin/R \"\$@\"" >> /usr/local/bin/R-architect.sh

	chmod +x /usr/local/bin/R-architect.sh
	
	update-alternatives --install /usr/bin/R R /usr/local/bin/R-architect.sh 100

fi

# Stable Architect
if [ -L $STABLE ]; then

	echo "Architect stable found at $STABLE"
	echo -e "${INFO}> Installing Architect (stable) R alternative${NC}"

	echo "Replacing existing version of /usr/local/bin/R-architect-stable.sh"
	echo "#!/usr/bin/env bash" > /usr/local/bin/R-architect-stable.sh
	echo "R_LIBS_SITE=$UNSTABLE_R/libs R_LIBS_USER=$R_LIBS_USER $UNSTABLE_R/bin/Rs \"\$@\"" >> /usr/local/bin/R-architect-stable.sh

	chmod +x /usr/local/bin/R-architect-unstable.sh
	
	update-alternatives --install /usr/bin/R R /usr/local/bin/R-architect-stable.sh 90
fi

# Unstable Architect
if [ -L $UNSTABLE ]; then

	echo "Architect unstable found at $UNSTABLE"
	echo -e "${INFO}> Installing Architect (unstable) R alternative${NC}"

	echo "Replacing existing version of /usr/local/bin/R-architect-unstable.sh"
	echo "#!/usr/bin/env bash" > /usr/local/bin/R-architect-unstable.sh
	echo "R_LIBS_SITE=$UNSTABLE_R/libs R_LIBS_USER=$R_LIBS_USER $UNSTABLE_R/bin/R \"\$@\"" >> /usr/local/bin/R-architect-unstable.sh

	chmod +x /usr/local/bin/R-architect-unstable.sh

	update-alternatives --install /usr/bin/R R /usr/local/bin/R-architect-unstable.sh 91

fi

if [ -d /usr/lib/R/bin ]; then
	echo -e "${INFO}> Installing native R alternative${NC}"
	update-alternatives --install /usr/bin/R R /usr/lib/R/bin/R 80
fi

update-alternatives --display R
echo "To select a different R alternative, run 'update-alternatives --config R'"

# echo "R .libPaths() output:"
# echo -e "${ROUT}"
# R -q -e ".libPaths()"
# echo -e "${NC}"

# ---------------------------------------------------------
# Rscript executable
# ---------------------------------------------------------

if [ -f /usr/bin/Rscript ] && [ ! -L /usr/bin/Rscript ]; then
	echo "/usr/bin/Rscript is not a link and needs to be removed"
	read -p "Continue? (Yn) " -n 1 -r
	echo
	if [[ $REPLY =~ ^[Nn]$ ]]; then
		echo "please move or remove /usr/bin/Rscript and run again"
		exit 1
	else
		rm /usr/bin/Rscript
	fi
fi

# Newer Architect
if [ -d $ARCHITECT/plugins ]; then

	ARCHITECT_VERSION=$(ls $ARCHITECT/plugins | grep architect | sed -Ens s/.*architect_\(.*\).jar/\\1/p)

	echo "Architect ($ARCHITECT_VERSION) found at $ARCHITECT"
	echo -e "${INFO}> Install Architect R alternative${NC}"

	echo "Replacing existing version of /usr/local/bin/R-architect.sh"
	echo "#!/usr/bin/env bash" > /usr/local/bin/Rscript-architect.sh
	echo "R_LIBS_SITE=$ARCHITECT_R/../../eu.openanalytics.architect.r.libs.rj.gtk.linux/libs R_LIBS_USER=$R_LIBS_USER $ARCHITECT_R/bin/Rscript \"\$@\"" >> /usr/local/bin/Rscript-architect.sh

	chmod +x /usr/local/bin/Rscript-architect.sh
	
	update-alternatives --install /usr/bin/Rscript Rscript /usr/local/bin/Rscript-architect.sh 100

fi

# Stable Architect
if [ -L $STABLE ]; then

	echo "Architect stable found at $STABLE"
	echo -e "${INFO}> Installing Architect (stable) Rscript alternative${NC}"

	echo "Replacing existing version of /usr/local/bin/Rscript-architect-stable.sh"
	echo "#!/usr/bin/env bash" > /usr/local/bin/Rscript-architect-stable.sh
	echo "R_LIBS_SITE=$STABLE_R/libs R_LIBS_USER=$R_LIBS_USER $STABLE_R/bin/Rscript \"\$@\"" >> /usr/local/bin/Rscript-architect-stable.sh

	chmod +x /usr/local/bin/Rscript-architect-stable.sh

	update-alternatives --install /usr/bin/Rscript Rscript /usr/local/bin/Rscript-architect-stable.sh 90
fi

# Unstable Architect
if [ -L $UNSTABLE ]; then

	echo -e "${INFO}> Installing Architect (unstable) Rscript alternative${NC}"
	echo "Architect unstable found at $UNSTABLE"

	echo "Replacing existing version of /usr/local/bin/Rscript-architect-unstable.sh"
	echo "#!/usr/bin/env bash" > /usr/local/bin/Rscript-architect-unstable.sh
	echo "R_LIBS_SITE=$UNSTABLE_R/libs R_LIBS_USER=$R_LIBS_USER $UNSTABLE_R/bin/Rscript \"\$@\"" >> /usr/local/bin/Rscript-architect-unstable.sh

	chmod +x /usr/local/bin/Rscript-architect-unstable.sh

	update-alternatives --install /usr/bin/Rscript Rscript /usr/local/bin/Rscript-architect-unstable.sh 91

fi

if [ -d /usr/lib/R/bin ]; then
	echo -e "${INFO}> Installing default Rscript alternative${NC}"
	update-alternatives --install /usr/bin/Rscript Rscript /usr/lib/R/bin/Rscript 80
fi

update-alternatives --display Rscript

# echo "Rscript .libPaths() output:"
# echo -e "${ROUT}"
# Rscript -e ".libPaths()"
# echo -e "${NC}"

echo "To selected a different Rscript alternative, run 'update-alternatives --config Rscript'"

