-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·230 lines (203 loc) · 7.12 KB
/
install.sh
File metadata and controls
executable file
·230 lines (203 loc) · 7.12 KB
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
#!/bin/bash
################################################################################
# Doily install script.
# https://github.com/relsqui/doily
#
# (c) 2017 Finn Ellis.
# You are free to use, copy, modify, etc. this by the terms of the MIT license.
# See included LICENSE.txt for details.
#
# Install a doily release from the Github repository, either systemwide
# or for the current user; or, remove doily for the system or user.
# Does not destroy user-specific configuration or data in any case.
################################################################################
set -e
trap 'error_out "$LINENO"' ERR
# Constants specify where to get the build from.
VERSION="0.1.2"
BRANCH="master"
# Defaults, may be modified by options.
target="system"
action="install"
binary_dir="${DOILY_TEST_BIN:-/usr/local/bin}"
config_dir="${DOILY_TEST_ETC:-/usr/local/etc}/doily"
error_out() {
############################################################################
# Exit cleanly on error, offering suggestions to fix the problem if known.
#
# Globals:
# - target
# Environment:
# - EUID
# Args:
# - Line number of the error (provided by trap).
# Returns:
# - None.
############################################################################
echo
echo "The doily install script exited with an error on line $1."
echo
if [[ "${target}" == "system" && "${EUID}" != 0 ]]; then
cat <<EOF
*** You seem to be attempting a systemwide operation without root. ***
*** Did you mean to use sudo, or --user? ***
EOF
else
cat <<EOF
Check for error messages above. You can also use the --help option to get
more information about using the doily install script.
EOF
fi
cat <<EOF
If that doesn't help, you can open an issue by going to:
https://github.com/relsqui/doily/issues
Use the search box to see if someone has already reported the same problem.
If not, click on the "new issue" button and explain what happened.
Please include ALL of the install script output in your description. Thanks!
EOF
exit 2
}
install() {
############################################################################
# Installs Doily for either the local user or the entire system.
#
# Globals:
# - BRANCH
# - VERSION
# - binary_dir
# - config_dir
# - target
# Args:
# - None.
# Returns:
# - None.
############################################################################
echo "Setting up temp directory."
tempdir="$(mktemp --tmpdir -dt doily-XXXXX)"
trap 'rm -r "${tempdir}"; echo "Removing temp directory."' EXIT
echo "Fetching doily files and unpacking them."
release_url="https://raw.githubusercontent.com/relsqui/doily/${BRANCH}/releases/doily-${VERSION}.tar.gz"
curl -s "${release_url}" | tar -xzC "${tempdir}"
echo "Creating directories."
mkdir -p "${binary_dir}" "${config_dir}"
# Clobbering old binary with updated binary is OK.
echo "Moving binary into place."
mv "${tempdir}/doily" "${binary_dir}"
if [[ "${target}" == "user" ]]; then
# Don't clobber existing user configuration with the default.
echo "Creating a config file if it didn't already exist."
mv -n "${tempdir}/default.conf" "${config_dir}/doily.conf"
if [[ ":$PATH:" != *":$HOME/bin:"* ]]; then
cat <<EOF
Installed doily as $HOME/bin/doily. It looks like that directory
isn't in your \$PATH. If you want to be able to run doily without typing the
full path, you'll need to do something like:
echo 'export PATH="\$PATH:\$HOME/bin"' >> .bashrc && source .bashrc
EOF
fi
else
# Check before clobbering the systemwide default.
mv -i "${tempdir}/default.conf" "${config_dir}/default.conf"
fi
cat <<EOF
*** Success! Doily installed. ***
EOF
}
uninstall() {
############################################################################
# Removes the Doily binary, either from the local userspace or from the
# entire system. Does not remove personal daily files in either case, but
# when uninstalling for a user, instructs them in how to do so.
#
# Globals:
# - binary_dir
# - config_dir
# - target
# Args:
# - None.
# Returns:
# - None.
############################################################################
echo "Removing doily binary."
rm "${binary_dir}/doily"
if [[ "${target}" == "system" ]]; then
# Remove the systemwide default, but not user config.
echo "Removing default configuration. Leaving user data and config."
rm -rf "${config_dir}"
else
cat <<EOF
Your personal settings and writings have been left alone.
If you really want to remove those, you can paste the following:
# Get rid of configuration, plugins, and so on:
rm -rf ${config_dir}
EOF
# Try to find dailies directory, but don't error out if we fail.
source "${config_dir}/doily.conf" 2>/dev/null || true
if [[ -z "${doily_dir}" ]]; then
cat <<EOF
Couldn't determine what your dailies directory is.
(Did you already delete your configuration file?)
EOF
else
cat <<EOF
# Get rid of your dailies. This can't be reversed!
rm -rf ${doily_dir}
EOF
fi
fi
}
main() {
############################################################################
# Parses arguments and initiates install/remove action or displays help.
#
# Globals:
# - binary_dir (sets)
# - config_dir (sets)
# - target (sets)
# Args:
# - Command-line arguments should be passed in with "$@".
# Returns:
# - None.
############################################################################
args=$(getopt -o urh -l user,remove,help -- "$@") || exit 1
eval set -- "$args"
for arg; do
case "$arg" in
-u|--user)
target="user"
binary_dir="$HOME/bin"
# This complies with the XDG Base Directory Specification:
# https://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
config_dir="${XDG_CONFIG_HOME:-$HOME/.config}/doily"
shift
;;
-r|--remove)
action="remove";
shift
;;
-h|--help)
cat <<EOF
usage: bash install.sh [-h|--help] [-u|--user] [-r|--remove]
Install doily (http://github.com/relsqui/doily), a daily writing script.
Defaults to systemwide installation, which requires superuser privileges.
Options:
-h --help Display this help message.
-u --user Install for the current user only.
-r --remove Remove doily rather than installing it.
This can be combined with -u to remove a user install.
EOF
exit 0
;;
--) shift ;;
*)
echo "Unexpected argument: ${arg}. Use --help for help."
exit 1
esac
done
if [[ "${action}" == "remove" ]]; then
uninstall
else
install
fi
}
main "$@"