-
Notifications
You must be signed in to change notification settings - Fork 38
/
install.sh
executable file
·279 lines (221 loc) · 8.76 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
#!/usr/bin/env bash
Init()
{
readonly SOURCE_SCRIPT_PATHFILE=/tmp/googliser.sh
readonly TARGET_SCRIPT_PATHFILE=/usr/local/bin/googliser
readonly SOURCE_COMPLETION_PATHFILE=/tmp/googliser-completion
BASH_COMPLETION_PACKAGE_PATHFILES=()
BASH_COMPLETION_PACKAGE_PATHFILES+=(/usr/share/bash-completion/bash_completion)
BASH_COMPLETION_PACKAGE_PATHFILES+=(/usr/local/etc/bash_completion)
readonly BASH_COMPLETION_PACKAGE_PATHFILES
TARGET_COMPLETION_PATHS=()
TARGET_COMPLETION_PATHS+=(/etc/bash_completion.d)
TARGET_COMPLETION_PATHS+=(/usr/local/etc/bash_completion.d)
TARGET_COMPLETION_PATHS+=(/usr/share/bash-completion/completions)
readonly TARGET_COMPLETION_PATHS
SUDO='sudo -k ' # '-k' disables cached authentication, so a password will be required every time
if [[ $EUID -eq 0 || $OSTYPE = "darwin"* ]]; then
SUDO=''
fi
readonly SUDO
echo " Installing googliser ..."
FindPackageManager || return 1
return 0
}
InstallBrew()
{
if [[ $OSTYPE = "darwin"* ]]; then
if ! (command -v brew >/dev/null); then
ruby -e "$(curl -fsSL git.io/get-brew)"
fi
brew install coreutils ghostscript gnu-sed gnu-getopt
fi
return 0
}
InstallImageMagick()
{
local cmd=''
local cmd_result=0
case $OSTYPE in
darwin*)
brew install imagemagick
;;
linux*)
if ! (command -v convert >/dev/null) || ! (command -v montage >/dev/null) || ! (command -v identify >/dev/null); then
if [[ -e /etc/fedora-release ]]; then
cmd+='ImageMagick '
else
cmd+='imagemagick '
fi
fi
if [[ -n $cmd ]]; then
if [[ $(basename "$PACKAGER_BIN") = pacman ]]; then # pacman has its own syntax
cmd="${SUDO}$PACKAGER_BIN -Syu; ${SUDO}$PACKAGER_BIN -S $cmd"
else
cmd="${SUDO}$PACKAGER_BIN install $cmd"
fi
[[ -n $SUDO ]] && echo " Executing: '$cmd'"
eval "$cmd"; cmd_result=$?
fi
if [[ $cmd_result -gt 0 ]]; then
echo " Unable to install additional packages"
return 1
fi
;;
esac
return 0
}
InstallMain()
{
local cmd=''
local cmd_result=0
if [[ ! -e $SOURCE_SCRIPT_PATHFILE && -w $(dirname "$SOURCE_SCRIPT_PATHFILE") ]]; then
if (command -v wget >/dev/null); then
wget -q git.io/googliser.sh -O "$SOURCE_SCRIPT_PATHFILE"
elif (command -v curl >/dev/null); then
curl -skLo "$SOURCE_SCRIPT_PATHFILE" git.io/googliser.sh
else
echo " Unable to find a downloader for $(basename "$SOURCE_SCRIPT_PATHFILE")"
return 1
fi
fi
[[ ! -x $SOURCE_SCRIPT_PATHFILE ]] && chmod +x "$SOURCE_SCRIPT_PATHFILE"
cmd="${SUDO}mv $SOURCE_SCRIPT_PATHFILE $TARGET_SCRIPT_PATHFILE"
[[ -n $SUDO ]] && echo " Executing: '$cmd'"
eval "$cmd"; cmd_result=$?
if [[ $cmd_result -gt 0 ]]; then
echo " Unable to move $SOURCE_SCRIPT_PATHFILE into target directory"
return 1
fi
return 0
}
InstallCompletion()
{
local cmd=''
local cmd_result=0
local target_completion_path=''
local bash_completion_package_pathfile=''
[[ $OSTYPE = "darwin"* ]] && brew install bash-completion
for target_completion_path in "${TARGET_COMPLETION_PATHS[@]}"; do
if [[ -d $target_completion_path ]]; then
for bash_completion_package_pathfile in "${BASH_COMPLETION_PACKAGE_PATHFILES[@]}"; do
if [[ -f $bash_completion_package_pathfile ]]; then
WriteCompletionScript
# move completion script into target path
cmd="${SUDO}mv $SOURCE_COMPLETION_PATHFILE $target_completion_path"
[[ -n $SUDO ]] && echo " Executing: '$cmd'"
eval "$cmd"; cmd_result=$?
if [[ $cmd_result -gt 0 ]]; then
echo " Unable to move $SOURCE_COMPLETION_PATHFILE into $target_completion_path"
return 1
fi
# now source completion script
case $OSTYPE in
darwin*)
SHELL=$(ps -p $$ -o ppid= | xargs ps -o comm= -p)
if [[ "$SHELL" == "zsh" ]]; then
echo "autoload -Uz compinit && compinit && autoload bashcompinit && bashcompinit" >> "$HOME/.zshrc"
LINE="source /usr/local/etc/bash_completion.d/googliser-completion"
CONFIG_FILE="$HOME/.zshrc"
grep -qF -- "$LINE" "$CONFIG_FILE" || echo "$LINE" >> "$CONFIG_FILE"
else
LINE="[ -f /usr/local/etc/bash_completion ] && . /usr/local/etc/bash_completion"
CONFIG_FILE="$HOME/.bash_profile"
grep -qF -- "$LINE" "$CONFIG_FILE" || echo "$LINE" >> "$CONFIG_FILE"
. $CONFIG_FILE
fi
;;
linux*)
# shellcheck disable=SC1090
. "$target_completion_path/googliser-completion"
;;
esac
break 2
fi
done
fi
done
return 0
}
WriteCompletionScript()
{
[[ ! -e $SOURCE_COMPLETION_PATHFILE && -w $(dirname "$SOURCE_COMPLETION_PATHFILE") ]] && cat > "$SOURCE_COMPLETION_PATHFILE" << 'EOF'
#!/usr/bin/env bash
_GoogliserCompletion()
{
# Pointer to current completion word.
# By convention, it's named "cur" but this isn't strictly necessary.
local cur
OPTS='-d -E -h -L -q -s -S -z -a -b -G -i -l -m -n -o -p -P -r -R -t -T -u --debug \
--exact-search --help --lightning --links-only --no-colour --no-color --safesearch-off \
--quiet --random --reindex-rename --save-links --skip-no-size --aspect-ratio \
--border-pixels --colour --color --exclude-links --exclude-words --format --gallery \
--input-links --input-phrases --lower-size --minimum-pixels --number --output --parallel \
--phrase --recent --retries --sites --thumbnails --timeout --title --type --upper-size --usage-rights'
COMPREPLY=() # Array variable storing the possible completions.
cur=${COMP_WORDS[COMP_CWORD]}
prev=${COMP_WORDS[COMP_CWORD-1]}
case "$cur" in
-*)
COMPREPLY=( $( compgen -W "${OPTS}" -- ${cur} ) );;
esac
# Display file completion for options that require files as arguments
case "$prev" in
--input-links|--exclude-links|-i|--input-phrases)
_filedir ;;
-a|--aspect-ratio)
COMPREPLY=( $( compgen -W "tall square wide panoramic" -- ${cur} ) );;
--colour|--color)
COMPREPLY=( $( compgen -W "any full black-white bw transparent clear red \
orange yellow green teal cyan blue purple magenta \
pink white grey gray black brown" -- ${cur} ) );;
--format)
COMPREPLY=( $( compgen -W "jpg png gif bmp svg webp ico craw" -- ${cur} ) );;
-m|--minimum-pixels)
COMPREPLY=( $( compgen -W "qsvga vga svga xga 2mp 4mp 6mp 8mp 10mp 12mp 15mp\
20mp 40mp 70mp large medium icon" -- ${cur} ) );;
-R|--recent)
COMPREPLY=( $( compgen -W "any hour day week month year" -- ${cur} ) );;
--type)
COMPREPLY=( $( compgen -W "face photo clipart lineart animated" -- ${cur} ) );;
--usage-rights)
COMPREPLY=( $( compgen -W "reuse reuse-with-mod noncomm-reuse noncomm-reuse-with-mod" -- ${cur} ) );;
esac
return 0
}
complete -F _GoogliserCompletion -o filenames googliser
EOF
}
FindPackageManager()
{
local managers=()
local manager=''
managers+=(brew)
managers+=(apt)
managers+=(yum)
managers+=(pacman)
managers+=(opkg)
managers+=(ipkg)
for manager in "${managers[@]}"; do
PACKAGER_BIN=$(command -v "$manager") && break
done
if [[ -z $PACKAGER_BIN ]]; then
echo " Unable to find local package manager"
return 1
fi
readonly PACKAGER_BIN
return 0
}
FailedInstall()
{
echo " Installation failed"
exit 1
}
Init || FailedInstall
InstallBrew || FailedInstall
InstallImageMagick || FailedInstall
InstallMain || FailedInstall
InstallCompletion || FailedInstall
echo " Installation complete"
echo
echo " Type 'googliser -h' for help"