Ticket #21807: arb_macsetup

File arb_macsetup, 7.5 KB (added by matt.cottrell@…, 15 years ago)

Script to help users set environment variables needed to run ARB

Line 
1#!/bin/bash
2#
3# Copyright (c) 2002-2007 Juan Manuel Palacios <jmpp@macports.org>, The MacPorts Project.
4# All rights reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
9# 1. Redistributions of source code must retain the above copyright
10#    notice, this list of conditions and the following disclaimer.
11# 2. Redistributions in binary form must reproduce the above copyright
12#    notice, this list of conditions and the following disclaimer in the
13#    documentation and/or other materials provided with the distribution.
14# 3. Neither the name of Apple, Inc., The MacPorts Project nor the
15#    names of its contributors may be used to endorse or promote products
16#    derived from this software without specific prior written permission.
17#
18# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
22# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28# SUCH DAMAGE.
29#
30
31# This is the arb_macsetup script
32# Use it to set the ARBHOME and PATH variables after installing ARB using MacPorts.
33
34# Derived from the MacPorts postflight script
35# Provided by: Matthew Cottrell
36# October 1, 2009
37
38# Abstraction variables:
39# The MacPorts PREFIX is typicaly /opt/local
40PREFIX=@@PREFIX@@
41BINPATH=${PREFIX}/bin
42SBINPATH=${PREFIX}/sbin
43MANPAGES=${PREFIX}/share/man
44TIMESTAMP=$(date +"%Y-%m-%d_at_%H:%M:%S")
45BACKUP_SUFFIX=macports-saved_${TIMESTAMP}
46OUR_STRING="ARB Installer addition on ${TIMESTAMP}"
47
48# Determine the user's shell, in order to choose an appropriate configuration file we'll be tweaking.
49# Exit nicely if the shell is any other than bash or tcsh, as that's considered non-standard.
50USHELL=$(dscl . -read /users/${USER} shell | awk -F'/' '{print $NF}') || {
51    echo "An attempt to determine your shell name failed! Please set your ARB compatible environment manually."
52    exit 1
53}
54case "${USHELL}" in
55    tcsh)
56        echo "Detected the tcsh shell."
57        LOGIN_FLAG=""
58        ENV_COMMAND="setenv"
59        ASSIGN=" "
60        if [ -f ${HOME}/.tcshrc ]; then
61            CONF_FILE=tcshrc
62        elif [ -f ${HOME}/.cshrc ]; then
63            CONF_FILE=cshrc
64        else
65            CONF_FILE=tcshrc
66        fi
67        ;;
68    bash)
69        echo "Detected the bash shell."
70        LOGIN_FLAG="-l"
71        ENV_COMMAND="export"
72        ASSIGN="="
73        if [ -f ${HOME}/.bash_profile ]; then
74            CONF_FILE=bash_profile
75        elif [ -f ${HOME}/.bash_login ]; then
76            CONF_FILE=bash_login
77        else
78            CONF_FILE=profile
79        fi
80        ;;
81    *)
82        echo "Unknown shell ($USHELL)! Please set your ARB compatible environment manually."
83       
84        exit 0
85        ;;
86esac
87
88# Through this command we write an environment variable to an appropriate shell configuration file,
89# backing up the original only if it exists and if it doesn't contain the ${OUR_STRING} identification string,
90# which hints that we've already tweaked it and therefore already backed it up.
91function write_setting () {
92    if [ -f ${HOME}/.${CONF_FILE} ] && ! grep "${OUR_STRING}" ${HOME}/.${CONF_FILE} > /dev/null; then
93        echo "Backing up your ${HOME}/.${CONF_FILE} shell confguration file as ${HOME}/.${CONF_FILE}.${BACKUP_SUFFIX} before adapting it for ARB."
94        /bin/cp -fp ${HOME}/.${CONF_FILE} "${HOME}/.${CONF_FILE}.${BACKUP_SUFFIX}" || {
95            echo "An attempt to backup your original configuration file failed! Please set your ARB compatible environment manually."
96           
97            exit 1
98        }
99        echo -e "\n##\n# Your previous ${HOME}/.${CONF_FILE} file was backed up as ${HOME}/.${CONF_FILE}.${BACKUP_SUFFIX}\n##" >> ${HOME}/.${CONF_FILE}
100    fi
101    echo -e "\n# ${OUR_STRING}: adding an appropriate ${1} variable for use with ${3}." >> ${HOME}/.${CONF_FILE}
102    echo "${ENV_COMMAND} ${1}${ASSIGN}${2}" >> ${HOME}/.${CONF_FILE}
103    echo -e "# Finished adapting your ${1} environment variable for use with ARB.\n" >> ${HOME}/.${CONF_FILE}
104    chown ${USER} ${HOME}/.${CONF_FILE} || echo "Warning: unable to adapt permissions on your ${HOME}/.${CONF_FILE} shell configuration file!"
105    echo "An appropriate ${1} variable has been added to your shell environment by the ${3} installer."
106}
107
108# Confirm that MacPorts has been configured for this user:
109if ${SHELL} ${LOGIN_FLAG} -c "/usr/bin/printenv PATH" | grep ${PREFIX} > /dev/null; then
110    echo "Your shell already has the right PATH environment variable for use with MacPorts!"
111else
112    echo "Uh oh! MacPorts is not configured for this user"
113    echo "Setting up MacPorts and ARB"
114    # Adding our setting to the PATH variable if not already there:
115    write_setting PATH "${BINPATH}:${SBINPATH}:\$PATH" MacPorts
116
117        # We gather the path into a variable of our own for faster operation:
118        ORIGINAL_MANPATH="$(${SHELL} ${LOGIN_FLAG} -c "/usr/bin/printenv MANPATH")"
119        # Adding our setting to the MANPATH variable only if it exists:
120        if ! ${SHELL} ${LOGIN_FLAG} -c "/usr/bin/env | grep MANPATH" > /dev/null || \
121        # and following that, if it's not empty:
122          [ -z "${ORIGINAL_MANPATH}" ] || \
123        # or if it doesn't already contain our path:
124          echo "${ORIGINAL_MANPATH}" | grep ${MANPAGES} > /dev/null || \
125        # or if there's no empty component somewhere in the middle of it:
126          echo "${ORIGINAL_MANPATH}" | grep :: > /dev/null || \
127        # or at the start of it:
128          [ -z "$(echo "${ORIGINAL_MANPATH}" | awk -F : '{print $1}')" ] || \
129        # or at the end of it:
130          [ -z "$(echo "${ORIGINAL_MANPATH}" | awk -F : '{print $NF}')" ]; then
131                echo "Your shell already has the right MANPATH environment variable for use with MacPorts!"
132        else
133                write_setting MANPATH "${MANPAGES}:\$MANPATH"
134        fi
135        echo "Done setting up MacPorts"
136fi
137
138# Adding ARB path to the PATH variable if it is not already there:
139if ${SHELL} ${LOGIN_FLAG} -c "/usr/bin/printenv PATH" | grep ${PREFIX}/share/arb/bin > /dev/null; then
140    echo "Your shell already has the right PATH environment variable for use with ARB!"
141else
142    write_setting PATH "${PREFIX}/share/arb/bin:\$PATH" ARB
143fi
144
145# Adding the ARBHOME variable if not already there:
146if ${SHELL} ${LOGIN_FLAG} -c "/usr/bin/printenv ARBHOME" | grep ${PREFIX}/share/arb > /dev/null; then
147    echo "Your shell already has the right ARBHOME environment variable for use with ARB!"
148else
149        write_setting ARBHOME "${PREFIX}/share/arb" ARB
150fi
151
152# arb_setenv script is done with its job - exit gracefully!
153echo ""
154echo "You have succesfully installed ARB"
155echo ""
156echo  "Open a new terminal window and type arb to launch ARB"
157echo ""
158echo "A demo data base is located at ${prefix}/share/arb.demo.arb"
159echo ""
160echo "************************************************************************************************"
161echo "Note: To set up ARB for another user log into their account and type ${prefix}/bin/arb_macsetup"
162echo "************************************************************************************************"
163echo ""
164echo "Please cite: Wolfgang Ludwig, et al. (2004) ARB: a software environment for sequence data. Nucleic Acids Research. 32:1363-1371"
165exit 0