-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathinstall.sh
executable file
·295 lines (264 loc) · 12.1 KB
/
install.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#!/bin/bash
## Arduino tools installation script.
## Part of the pearl Computer Architecture of module Pearls of Computer Science
## Written by Remco de Man for the University of Twente
##
## This script installs the needed Arduino tools for macOS and Linux.
## In essence, it ensures the following things:
## (1) A working version of hex2hex is available, that means one of the
## following:
## (1.1) There is an existing binary of hex2hex/hex2hex
## (1.2) A working version is compiled using hex2hex/hex2hex.c and
## a locally installed C compiler.
## (1.3) The python version can be used using a Python interpeter
## (2) Under macOS only, ensure we have access to the XCode command line
## tools, install them automatically when we don't have them
## (3) Under macOS only, ensure that we have a working Homebrew
## environment, as per https://brew.sh/. Maybe installed if missing.
## (4) Ensure that we have a working version of avrdude. On Debian based
## systems this may be installed using APT. On macOS this may be
## installed using Homebrew.
## (5) Ensure that we have a working version of avra. On Debian based
## systems this may be installed using APT. On macOS this may be
## installed using Homebrew.
## (6) Ensure that we have a working version of putty under Linux, or
## screen under macOS. Putty is more convenient to use but does not
## work quite well under macOS. Putty may be installed using APT
## on Debian based systems. GNU screen may be installed using
## Homebrew on macOS based systems.
## If any of the above `requirements` cannot be resolved, the script will
## stop and inform the user.
## This script does itself not install any files outside of the directory
## the script lives in, however:
## (-) On macOS, it does install XCode command line tools and Homebrew,
## furthermore, it installs the following Homebrew packages:
## (1) avra
## (2) avrdude
## (3) screen
## (-) On Debian based systems, it might install the following APT packages
## (1) avra
## (2) avrdude
## (3) putty
## You can remove them later if you want. Use the `--autoremove` flag
## to also uninstall the dependencies automcatically installed. See
## `man apt` for more details.
## Usage:
## install
# Setup some environment variables that contain control characters to change
# the text color and background color of the console output.
TEXT_BOLD=$(tput bold)
TEXT_NORMAL=$(tput sgr0)
TEXT_RED=$(tput setaf 1)
TEXT_GREEN=$(tput setaf 2)
TEXT_YELLOW=$(tput setaf 3)
TEXT_BLUE=$(tput setaf 4)
# Bash oneliner to get the directory the scripts lives in.
DIR="$(dirname "${BASH_SOURCE[0]}")"
function check_error {
# Simple function which checks if there was an error based on
# an error code provided as the first argument. Returns the error
# code if there was in fact an error.
if [[ "$1" -ne 0 ]]; then
echo -e "${TEXT_RED}error, exit code $1${TEXT_NORMAL}"
return $1
fi
}
function detect_required_program {
# Function which detects if the program given in the first argument
# actually is on the PATH of the running system. Using `which` ensures
# that we also detect aliases/symlinks may someone use a different, but
# hopefully compatible, tool. Returns correctly if the required program
# is detected.
echo -e -n "> $1: "
local PROGRAM="$(which $2)"
if [[ -n "$PROGRAM" ]]; then
echo -e "${TEXT_GREEN}found${TEXT_NORMAL} at ${TEXT_BOLD}${PROGRAM}${TEXT_NORMAL}"
return 0
fi
echo -e "${TEXT_RED}not found${TEXT_NORMAL}"
echo -e "${TEXT_BLUE}${TEXT_BOLD}Hint:${TEXT_NORMAL} $1 is missing. Please install it first. In most cases, the package will be called '$2'."
return 1
}
function detect_optional_program {
# Function which detects if the program given in the first argument
# actually is on the PATH of the running system. Using `which` ensures
# that we also detect aliases/symlinks may someone use a different, but
# hopefully compatible, tool. Returns correctly if the program is
# detected.
echo -e -n "> $1: "
local PATH="$(which $2)"
if [[ -n "$PATH" ]]; then
echo -e "${TEXT_GREEN}found${TEXT_NORMAL} at ${TEXT_BOLD}${PATH}${TEXT_NORMAL}"
return 0
fi
echo -e "${TEXT_YELLOW}optional${TEXT_NORMAL}"
return 1
}
function detect_hex2hex {
# Function which detects if the hex2hex dependency is somehow
# executable.
local CC=0
local PYTHON=0
detect_optional_program "C compiler" "cc" && CC=1
detect_optional_program "Python" "python" && PYTHON=1
echo -e -n "> hex2hex: "
if [[ -x "hex2hex/hex2hex" ]]; then
# If there is already a binary hex2hex executable found within
# the tools folder, we will use this to resolve the dependency.
echo -e "${TEXT_GREEN}found${TEXT_NORMAL} at ${TEXT_BOLD}${DIR}/hex2hex/hex2hex${TEXT_NORMAL}"
elif [[ -f "hex2hex/hex2hex.c" ]] && [[ "${CC}" == "1" ]]; then
# If there does exist a hex2hex binary yet, but we have the
# source code and a working compiler, then compile the source
# code into a binary and use this to resolve the dependency.
echo -e -n "${TEXT_NORMAL}compiling ${TEXT_NORMAL}"
cc hex2hex/hex2hex.c -o hex2hex/hex2hex
check_error "$?" || exit 1
echo -e "${TEXT_GREEN}done${TEXT_NORMAL}"
elif [[ -f "hex2hex/hex2hex.py" ]] && [[ "${PYTHON}" == "1" ]]; then
# If there is no way to get a working hex2hex binary, but we
# do have a Python interpreter and a Python version of hex2hex
# then, use this to resolve the hex2hex dependency.
echo -e "${TEXT_GREEN}found at ${TEXT_BOLD}${DIR}/hex2hex/hex2hex.py${TEXT_NORMAL}"
else
# If we after all these options failed to get the hex2hex
# dependency resolved, we give up.
echo -e "${TEXT_RED}not found${TEXT_NORMAL}"
return 1
fi
}
function detect_xcode_tools {
# Function which detects if the XCode command line tools are installed.
# These tools are needed under macOS to provide certain functionality
# on the command line, such as a working C compiler and Git. It has no
# useful effect under Linux. We need this to install homebrew under
# macOS, which we need to install all needed tools.
echo -e "${TEXT_BLUE}${TEXT_BOLD}Hint:${TEXT_NORMAL} The script will now try to install the XCode Developer Command Line tools."
echo -e "${TEXT_BLUE}${TEXT_BOLD}Hint:${TEXT_NORMAL} This might show an confirmation dialog. Please press install when asked."
xcode-select --install
echo -e -n "\n"
}
function detect_brew {
# Function which tries to resolve the Homebrew dependency under macOS.
# It first checks if there is already a working `brew` command. If
# there is not, install the Homebrew according to the instructions
# on the Homebrew webpage, https://brew.sh/index_nl.
# This should not be run under Linux. Linux does not need it as we
# already have access to all needed GNU tools (under normal
# circumstances).
echo -e -n "> Homebrew: "
local PROGRAM="$(which brew)"
if [[ -n "$PROGRAM" ]]; then
echo -e "${TEXT_GREEN}found${TEXT_NORMAL} at ${TEXT_BOLD}${PROGRAM}${TEXT_NORMAL}"
return 0
fi
echo -e "${TEXT_YELLOW}launching installation...${TEXT_NORMAL}"
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
echo -e -n "\n> Homebrew: "
check_error "$?" || exit 1
echo -e "${TEXT_GREEN}done${TEXT_NORMAL}"
}
function detect_or_install_brew {
# Function which tries to resolve a certain brew package under macOS.
# The description of the brew package is given as first argument to
# this function, the name of the package as second argument. If the
# package is not yet installed, the function will try to install it.
echo -e -n "> $1: "
local PROGRAM="$(which $2)"
if [[ -n "$PROGRAM" ]]; then
echo -e "${TEXT_GREEN}found${TEXT_NORMAL} at ${TEXT_BOLD}${PROGRAM}${TEXT_NORMAL}"
return 0
fi
echo -e -n "${TEXT_NORMAL}installing ${TEXT_NORMAL}"
brew install $2 &> /dev/null
if check_error "$?"; then
echo -e "${TEXT_GREEN}done${TEXT_NORMAL}"
else
echo -e "${TEXT_BLUE}${TEXT_BOLD}Hint:${TEXT_NORMAL} Error while installing package '$2'. Try manually installing the package using 'brew install $2' and try again."
return 1
fi
}
function detect_or_install_apt {
# Function which tries to resolve an installed package under Debian
# based Linux distributions (the ones that use APT as their package
# manager).
# The description of the brew package is given as first argument to
# this function, the name of the package as second argument. If the
# package is not yet installed, the function will try to install it.
echo -e -n "> $1: "
local PROGRAM="$(which $2)"
if [[ -n "$PROGRAM" ]]; then
echo -e "${TEXT_GREEN}found${TEXT_NORMAL} at ${TEXT_BOLD}${PROGRAM}${TEXT_NORMAL}"
return 0
fi
echo -e -n "${TEXT_NORMAL}installing ${TEXT_NORMAL}"
apt-get install -q -y $2 &> /dev/null
if check_error "$?"; then
echo -e "${TEXT_GREEN}done${TEXT_NORMAL}"
else
echo -e "${TEXT_BLUE}${TEXT_BOLD}Hint:${TEXT_NORMAL} Error while installing package '$2'. Maybe you should run the script as root? Try manually installing the package using 'apt install $2' and try again."
return 1
fi
}
echo -e "${TEXT_NORMAL}Arduino Tools configure script for Linux/macOS${TEXT_NORMAL}"
pushd "$DIR" > /dev/null
# Detect our OS type first.
if [[ $OSTYPE == darwin* ]]; then
# Seems like we are on a macOS based system.
echo -e "Detected ${TEXT_BLUE}macOS${TEXT_NORMAL}\n"
# First check if we have access to the XCode command line tools.
# Install them when neccessary.
detect_xcode_tools
# Resolve a working version of hex2hex.
detect_hex2hex || (echo -e "${TEXT_BLUE}${TEXT_BOLD}Hint:${TEXT_NORMAL} Please install a C compiler or the Python runtime first." && exit 1)
# Resolve homebrew for access to more command line tools.
detect_brew
# Ensure we have `avrdude` installed via Homebrew such that we
# can upload programs to the Arduino.
detect_or_install_brew "avrdude uploader" "avrdude" || exit 1
# Ensure we have `avra` installed via Homebrew such that we
# can assemble programs for the Arduino.
detect_or_install_brew "avra assembler" "avra" || exit 1
# Ensure we have GNU `screen` installed for reading serial
# connections.
detect_or_install_brew "screen" "screen" || exit 1
elif [[ $OSTYPE == linux* ]]; then
# Seems like we are on a Linux based system, let's check which kind...
if [[ -n "$(which apt-get)" ]]; then
# We are on a Debian based system. We can use APT to
# install tools that are missing.
echo -e "Detected ${TEXT_BLUE}Debian-based Linux${TEXT_NORMAL}\n"
# Resolve a working version of hex2hex.
detect_hex2hex || (echo -e "${TEXT_BLUE}${TEXT_BOLD}Hint:${TEXT_NORMAL} Please install a C compiler or the Python runtime first." && exit 1)
# Ensure we have `avrdude` installed such that we
# can upload programs to the Arduino.
detect_or_install_apt "avrdude uploader" "avrdude" || exit 1
# Ensure we have `avra` installed via Homebrew such that we
# can assemble programs for the Arduino.
detect_or_install_apt "avra assembler" "avra" || exit 1
# Ensure that we have `putty` installed for reading the serial
# output of the Arduino.
detect_or_install_apt "putty" "putty" || exit 1
else
# We are on a non-Debian based system (Fedora, Arch, etc.).
# The script can only give advice on missing tools, but not
# install them automatically.
echo -e "Detected ${TEXT_BLUE}Linux${TEXT_NORMAL}\n"
# Resolve a working version of hex2hex.
detect_hex2hex || (echo -e "${TEXT_BLUE}${TEXT_BOLD}Hint:${TEXT_NORMAL} Please install a C compiler or the Python runtime first." && exit 1)
# Ensure we have `avrdude` installed such that we
# can upload programs to the Arduino.
detect_required_program "avrdude uploader" "avrdude" || exit 1
# Ensure we have `avra` installed such that we
# can assemble programs for the Arduino.
detect_required_program "avra assembler" "avra" || exit 1
# Ensure that we have `putty` installed for reading the serial
# output of the Arduino.
detect_required_program "putty" "putty" || exit 1
fi
else
# We did not successfully detect macOS of Linux. Let's... just stop.
echo -e "${TEXT_RED}Unknown OS${TEXT_NORMAL}"
echo -e "${TEXT_BLUE}${TEXT_BOLD}Hint:${TEXT_NORMAL} Run this script on a compatible operating system to continue."
exit 1
fi
echo -e "\n${TEXT_GREEN}${TEXT_BOLD}Successful:${TEXT_NORMAL} All tools are installed and successfully configured."