#!/bin/bash -e tag=$(basename $(/usr/bin/curl -Ls -o /dev/null -w %{url_effective} https://github.com/MRtrix3/mrtrix3/releases/latest)) if [ -z "${tag}" ]; then echo "ERROR: could not find tag name for latest release ..." exit fi if [ "$1" != "-f" ]; then echo "This installer will download MRtrix ${tag} and install it to /usr/local/mrtrix3." echo "In addition it will:" echo "* create symbolic links in /usr/local/bin to the binaries in /usr/local/mrtrix3/bin" echo "* create symbolic links in /Applications to the app bundles in /usr/local/mrtrix3/bin" fi if [ $EUID != 0 ]; then echo "ERROR: This script requires root privileges, please run as: sudo "$0" "$@"" exit fi if [ -d "/usr/local/mrtrix3" ] || [ -L "/usr/local/mrtrix3" ] ; then echo "WARNING: /usr/local/mrtrix3 already exists and will be replaced during installation." fi if [ "$1" != "-f" ]; then while true; do read -p "Are you sure you want to continue? [y/n] " yn case $yn in [Yy]* ) break;; [Nn]* ) exit;; esac done fi if [ ! -d "/usr/local/bin" ]; then if [ -e "/usr/local/bin" ]; then echo "WARNING: /usr/local/bin is not a directory, cannot create symlinks" else echo "WARNING: /usr/local/bin does not exist, creating it for you." mkdir -p -m 755 /usr/local/bin fi fi url=https://github.com/MRtrix3/mrtrix3/releases/download/${tag}/mrtrix3-macos-${tag}.tar.gz if [ -z "${url}" ]; then echo "ERROR: Could not find tarball of latest release ..." exit fi echo "Downloading "${url}" ..." /usr/bin/curl -sL "${url}" -O tarball=$(basename "${url}") if [ ! -f "${tarball}" ]; then echo "ERROR: Download not sucessful ..." exit fi if [ -f /usr/local/mrtrix3/symlinks ]; then echo "Removing symbolic links of previous installation ..." for l in $(cat /usr/local/mrtrix3/symlinks); do if [ -L "${l}" ]; then unlink "${l}" fi done fi if [ -d "/usr/local/mrtrix3" ] || [ -L "/usr/local/mrtrix3" ] ; then echo "Removing previous installation in /usr/local/mrtrix3 ..." rm -rf "/usr/local/mrtrix3" fi for l in /usr/local/bin/*; do if [ -L "${l}" ]; then t="$(readlink "${l}")" if [[ "${t}" == *"mrtrix3"* ]]; then echo "WARNING: Removing symbolic link "${l}" to "${t}" (conflicting homebrew installation?) ..." unlink "${l}" fi fi done echo "Unpacking "${tarball}" to /usr/local/mrtrix3 ..." tar oxf "${tarball}" -C /usr/local rm "${tarball}" echo "Fixing python shebang ..." cd /usr/local/mrtrix3/bin for f in $(grep -lr '^#!/usr/bin/env python' .); do sed -i '' 's|^#!/usr/bin/env python$|#!/usr/bin/python3|' ${f} done echo "Applying patch to show correct version numbers in scripts ..." cd /usr/local/mrtrix3 curl -s https://github.com/MRtrix3/mrtrix3/commit/4a293d30e1c0686037ae637f67932d497eb71ee6.patch | patch -s -p1 echo "Creating symlinks in /usr/local/bin ..." cd /usr/local/bin touch /usr/local/mrtrix3/symlinks for target in $(find ../mrtrix3/bin -maxdepth 1 -type f ! -name "*.*"); do ln -sf "${target}" echo /usr/local/bin/"$(basename "${target}")" >> /usr/local/mrtrix3/symlinks done echo "Creating symlinks in /Applications ..." cd /Applications for target in /usr/local/mrtrix3/bin/*.app; do ln -sf "${target}" echo /Applications/"$(basename "${target}")" >> /usr/local/mrtrix3/symlinks done if [[ $SHELL = "/bin/zsh" ]]; then profile=~/.zprofile; else profile=~/.bash_profile; fi echo "$PATH" | grep -q '/usr/local/bin:' || printf "WARNING: /usr/local/bin is not in PATH. You can add it to the PATH with:\necho 'export PATH=/usr/local/bin:\$PATH' >> ${profile}\n" echo "Installation complete!"