Diff of /bin/setup [000000] .. [030aeb]

Switch to unified view

a b/bin/setup
1
#!/bin/bash
2
3
# Initialize DOSMA
4
#
5
# @usage (from terminal/command line):
6
# ./setup
7
#
8
# @initialization protocol:
9
#   1. Navigate to this folder in terminal/command line
10
#   2. Run "chmod +x setup" from command-line (Linux) or Terminal (MacOS)
11
#
12
# @author: Arjun Desai, Stanford University
13
#          (c) Stanford University, 2018
14
15
openURL() {
16
    if [[ "$OSTYPE" == "linux-gnu" ]]; then
17
            xdg-open $1
18
    elif [[ "$OSTYPE" == "darwin"* ]]; then
19
            # Mac OSX
20
            open $1
21
    else
22
        echo "Only Linux and MacOS are supported"
23
        exit 125
24
    fi
25
}
26
27
setBashFile() {
28
    if [[ "$OSTYPE" == "linux-gnu" ]]; then
29
            bash_file="$HOME/.bashrc"
30
    elif [[ "$OSTYPE" == "darwin"* ]]; then
31
            bash_file="$HOME/.bash_profile"
32
    else
33
        echo "Only Linux and MacOS are supported"
34
        exit 125
35
    fi
36
}
37
38
BIN_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
39
DOSMA_DIR="$( cd "$( dirname "${BIN_DIR}" )" >/dev/null 2>&1 && pwd )"
40
41
ANACONDA_KEYWORD="anaconda"
42
ANACONDA_DOWNLOAD_URL="https://www.anaconda.com/distribution/"
43
MINICONDA_KEYWORD="miniconda"
44
45
DOSMA_ENV_NAME="dosma_env"
46
ENV_FILE=$DOSMA_DIR/environment.yml
47
48
GOOGLE_FORM_URL="https://forms.gle/sprthTC2swyt8dDb6"
49
50
DOSMA_SETUP_PATTERN="dosma setup|export PATH.*dosma\/bin"
51
52
hasAnaconda=0
53
updateEnv=1
54
updatePath=1
55
setBashFile
56
57
while getopts ":h" opt; do
58
    case ${opt} in
59
        h )
60
            echo "DOSMA setup"
61
            echo "     Create Anaconda virtual environment and add dosma executable to PATH variable"
62
            echo "Usage:"
63
            echo "    -h                Display this help message"
64
            exit 0
65
            ;;
66
        \? )
67
            echo "Usage: cmd [-h] [-u] [-e]"
68
            exit 0
69
            ;;
70
    esac
71
done
72
73
# Check if conda exists
74
if echo $PATH | grep -q $ANACONDA_KEYWORD; then
75
    hasAnaconda=1
76
    echo "Conda found in path"
77
fi
78
79
if echo $PATH | grep -q $MINICONDA_KEYWORD
80
then
81
    hasAnaconda=1
82
    echo "Miniconda found in path"
83
fi
84
85
if [[ $hasAnaconda -eq 0 ]]; then
86
    echo "Anaconda/Miniconda not installed - install from $ANACONDA_DOWNLOAD_URL"
87
    openURL $ANACONDA_DOWNLOAD_URL
88
    exit 125
89
fi
90
91
# Check if OS is supported
92
if [[ "$OSTYPE" != "linux-gnu" && "$OSTYPE" != "darwin"* ]]; then
93
    echo "Only Linux and MacOS are supported"
94
    exit 125
95
fi
96
97
# Create Anaconda environment (dosma_env)
98
if [[ `conda env list | grep $DOSMA_ENV_NAME` ]]; then
99
    if [[ ${updateEnv} -eq 0 ]]; then
100
        echo "Environment 'dosma_env' found. Run 'conda activate dosma_env' to get started."
101
    else
102
        conda env remove -n $DOSMA_ENV_NAME
103
        conda env create -f $ENV_FILE
104
    fi
105
else
106
    conda env create -f $ENV_FILE
107
fi
108
109
# If DOSMA executable already exists as a shortcut and updating DOSMA, then remove the path lines
110
if [[ ! -z `cat ${bash_file} | egrep -i "${DOSMA_SETUP_PATTERN}"` && ${updatePath} -ne 0 ]]; then
111
    echo ""
112
    echo "Overwriting DOSMA executable path"
113
    echo ""
114
115
    cp $bash_file $bash_file"-dosma.bak"
116
    tempFile="$HOME/.bash_profile_temp"
117
    egrep -iv "${DOSMA_SETUP_PATTERN}" $bash_file >> $tempFile
118
    cp $tempFile $bash_file
119
fi
120
121
# Add dosma to path
122
if [[ -z `cat ${bash_file} | egrep -i "${DOSMA_SETUP_PATTERN}"` ]]; then
123
    cp $bash_file $bash_file"-dosma.bak"
124
125
    echo "Adding dosma to path - changes made in $bash_file"
126
    echo "" >> $bash_file
127
    echo "# Added by DOSMA setup" >> $bash_file
128
    echo 'export PATH='$BIN_DIR':$PATH' >> $bash_file
129
fi
130
131
echo ""
132
echo ""
133
echo "DOSMA Usage"
134
echo "------------"
135
echo "For command line help menu: 'dosma -h'"
136
echo "For GUI (user interface): 'dosma --app'"
137
echo ""
138
echo "For help with DOSMA, see documetation:"
139
echo "    https://ad12.github.io/DOSMA"
140
echo ""
141
echo "Please also complete the DOSMA questionnaire if not previously completed"
142
echo "    ${GOOGLE_FORM_URL}"
143
echo ""
144
echo ""
145
# Launch google form
146
openURL $GOOGLE_FORM_URL