bugfix: invert download check
[webi-installers/.git] / _webi / template.sh
1 #!/bin/bash
2
3 # shellcheck disable=2001
4 # because I prefer to use sed rather than bash replace
5 # (there's too little space in my head to learn both syntaxes)
6
7 function __bootstrap_webi() {
8
9     set -e
10     set -u
11     #set -x
12
13     #WEBI_PKG=
14     #PKG_NAME=
15     # TODO should this be BASEURL instead?
16     #WEBI_OS=
17     #WEBI_ARCH=
18     #WEBI_HOST=
19     #WEBI_RELEASES=
20     #WEBI_CSV=
21     #WEBI_TAG=
22     #WEBI_VERSION=
23     #WEBI_MAJOR=
24     #WEBI_MINOR=
25     #WEBI_PATCH=
26     # TODO not sure if BUILD is the best name for this
27     #WEBI_BUILD=
28     #WEBI_LTS=
29     #WEBI_CHANNEL=
30     #WEBI_EXT=
31     #WEBI_FORMATS=
32     #WEBI_PKG_URL=
33     #WEBI_PKG_FILE=
34     #PKG_OSES=
35     #PKG_ARCHES=
36     #PKG_FORMATS=
37     WEBI_UA="$(uname -a)"
38     export WEBI_HOST
39
40     ##
41     ## Set up tmp, download, and install directories
42     ##
43
44     WEBI_TMP=${WEBI_TMP:-"$(mktemp -d -t webinstall-"${WEBI_PKG:-}".XXXXXXXX)"}
45     export _webi_tmp="${_webi_tmp:-"$HOME/.local/opt/webi-tmp.d"}"
46
47     mkdir -p "$HOME/Downloads"
48     mkdir -p "$HOME/.local/bin"
49     mkdir -p "$HOME/.local/opt"
50
51     ##
52     ## Detect http client
53     ##
54     set +e
55     WEBI_CURL="$(command -v curl)"
56     export WEBI_CURL
57     WEBI_WGET="$(command -v wget)"
58     export WEBI_WGET
59     set -e
60
61     # get the special formatted version (i.e. "go is go1.14" while node is "node v12.10.8")
62     my_versioned_name=""
63     _webi_canonical_name() {
64         if [ -n "$my_versioned_name" ]; then
65             echo "$my_versioned_name"
66             return 0
67         fi
68
69         if [ -n "$(command -v pkg_format_cmd_version)" ]; then
70             my_versioned_name="$(pkg_format_cmd_version "$WEBI_VERSION")"
71         else
72             my_versioned_name="'$pkg_cmd_name' v$WEBI_VERSION"
73         fi
74
75         echo "$my_versioned_name"
76     }
77
78     # update symlinks according to $HOME/.local/opt and $HOME/.local/bin install paths.
79     # shellcheck disable=2120
80     # webi_link may be used in the templated install script
81     webi_link() {
82         if [ -n "$(command -v pkg_link)" ]; then
83             pkg_link
84             return 0
85         fi
86
87         if [ -n "$WEBI_SINGLE" ] || [ "single" == "${1:-}" ]; then
88             rm -rf "$pkg_dst_cmd"
89             ln -s "$pkg_src_cmd" "$pkg_dst_cmd"
90         else
91             # 'pkg_dst' will default to $HOME/.local/opt/<pkg>
92             # 'pkg_src' will be the installed version, such as to $HOME/.local/opt/<pkg>-<version>
93             rm -rf "$pkg_dst"
94             ln -s "$pkg_src" "$pkg_dst"
95         fi
96     }
97
98     # detect if this program is already installed or if an installed version may cause conflict
99     webi_check() {
100         # Test for existing version
101         set +e
102         my_path="$PATH"
103         PATH="$(dirname "$pkg_dst_cmd"):$PATH"
104         export PATH
105         my_current_cmd="$(command -v "$pkg_cmd_name")"
106         set -e
107         if [ -n "$my_current_cmd" ]; then
108             pkg_current_version="$(pkg_get_current_version 2> /dev/null | head -n 1)"
109             # remove trailing '.0's for golang's sake
110             my_current_version="$(echo "$pkg_current_version" | sed 's:\.0::g')"
111             my_src_version="$(echo "$WEBI_VERSION" | sed 's:\.0::g')"
112             my_canonical_name="$(_webi_canonical_name)"
113             if [ "$my_src_version" == "$my_current_version" ]; then
114                 echo "$my_canonical_name already installed at $my_current_cmd"
115                 exit 0
116             else
117                 if [ "$my_current_cmd" != "$pkg_dst_cmd" ]; then
118                     echo >&2 "WARN: possible conflict between $my_canonical_name and $pkg_current_version at $my_current_cmd"
119                 fi
120                 if [ -x "$pkg_src_cmd" ]; then
121                     # shellcheck disable=2119
122                     # this function takes no args
123                     webi_link
124                     echo "switched to $my_canonical_name at $pkg_src"
125                     exit 0
126                 fi
127             fi
128         fi
129         export PATH="$my_path"
130     }
131
132     # detect if file is downloaded, and how to download it
133     webi_download() {
134         if [ -n "${1:-}" ]; then
135             my_url="$1"
136         else
137             if [ "error" == "$WEBI_CHANNEL" ]; then
138                 # TODO pass back requested OS / Arch / Version
139                 echo >&2 "Error: no '$PKG_NAME' release for '${WEBI_OS:-}' on '$WEBI_ARCH' as one of '$WEBI_FORMATS' by the tag '${WEBI_TAG:-}'"
140                 echo >&2 "       '$PKG_NAME' is available for '$PKG_OSES' on '$PKG_ARCHES' as one of '$PKG_FORMATS'"
141                 echo >&2 "       (check that the package name and version are correct)"
142                 echo >&2 ""
143                 echo >&2 "       Double check at $(echo "$WEBI_RELEASES" | sed 's:\?.*::')"
144                 echo >&2 ""
145                 exit 1
146             fi
147             my_url="$WEBI_PKG_URL"
148         fi
149         if [ -n "${2:-}" ]; then
150             my_dl="$2"
151         else
152             my_dl="$HOME/Downloads/$WEBI_PKG_FILE"
153         fi
154
155         if [ -e "$my_dl" ]; then
156             echo "Found $my_dl"
157             return 0
158         fi
159
160         echo "Downloading $PKG_NAME from"
161         echo "$my_url"
162
163         # It's only 2020, we can't expect to have reliable CLI tools
164         # to tell us the size of a file as part of a base system...
165         if [ -n "$WEBI_WGET" ]; then
166             # wget has resumable downloads
167             # TODO wget -c --content-disposition "$my_url"
168             set +e
169             my_show_progress=""
170             if [[ $- == *i* ]]; then
171                 my_show_progress="--show-progress"
172             fi
173             if ! wget -q $my_show_progress --user-agent="wget $WEBI_UA" -c "$my_url" -O "$my_dl.part"; then
174                 echo >&2 "failed to download from $WEBI_PKG_URL"
175                 exit 1
176             fi
177             set -e
178         else
179             # Neither GNU nor BSD curl have sane resume download options, hence we don't bother
180             # TODO curl -fsSL --remote-name --remote-header-name --write-out "$my_url"
181             my_show_progress="-#"
182             if [[ $- == *i* ]]; then
183                 my_show_progress=""
184             fi
185             # shellcheck disable=SC2086
186             # we want the flags to be split
187             curl -fSL $my_show_progress -H "User-Agent: curl $WEBI_UA" "$my_url" -o "$my_dl.part"
188         fi
189         mv "$my_dl.part" "$my_dl"
190
191         echo ""
192         echo "Saved as $my_dl"
193     }
194
195     # detect which archives can be used
196     webi_extract() {
197         pushd "$WEBI_TMP" > /dev/null 2>&1
198         if [ "tar" == "$WEBI_EXT" ]; then
199             echo "Extracting $HOME/Downloads/$WEBI_PKG_FILE"
200             tar xf "$HOME/Downloads/$WEBI_PKG_FILE"
201         elif [ "zip" == "$WEBI_EXT" ]; then
202             echo "Extracting $HOME/Downloads/$WEBI_PKG_FILE"
203             unzip "$HOME/Downloads/$WEBI_PKG_FILE" > __unzip__.log
204         elif [ "exe" == "$WEBI_EXT" ]; then
205             echo "Moving $HOME/Downloads/$WEBI_PKG_FILE"
206             mv "$HOME/Downloads/$WEBI_PKG_FILE" .
207         elif [ "xz" == "$WEBI_EXT" ]; then
208             echo "Inflating $HOME/Downloads/$WEBI_PKG_FILE"
209             unxz -c "$HOME/Downloads/$WEBI_PKG_FILE" > "$(basename "$WEBI_PKG_FILE")"
210         else
211             # do nothing
212             echo "Failed to extract $HOME/Downloads/$WEBI_PKG_FILE"
213             exit 1
214         fi
215         popd > /dev/null 2>&1
216     }
217
218     # use 'pathman' to update $HOME/.config/envman/PATH.env
219     webi_path_add() {
220         # make sure that we don't recursively install pathman with webi
221         my_path="$PATH"
222         export PATH="$HOME/.local/bin:$PATH"
223
224         # install pathman if not already installed
225         if [ -z "$(command -v pathman)" ]; then
226             "$HOME/.local/bin/webi" pathman > /dev/null
227         fi
228
229         export PATH="$my_path"
230
231         # in case pathman was recently installed and the PATH not updated
232         mkdir -p "$_webi_tmp"
233         # prevent "too few arguments" output on bash when there are 0 lines of stdout
234         "$HOME/.local/bin/pathman" add "$1" | grep "export" 2> /dev/null >> "$_webi_tmp/.PATH.env" || true
235     }
236
237     # group common pre-install tasks as default
238     webi_pre_install() {
239         webi_check
240         webi_download
241         webi_extract
242     }
243
244     # move commands from the extracted archive directory to $HOME/.local/opt or $HOME/.local/bin
245     # shellcheck disable=2120
246     # webi_install may be sourced and used elsewhere
247     webi_install() {
248         if [ -n "$WEBI_SINGLE" ] || [ "single" == "${1:-}" ]; then
249             mkdir -p "$(dirname "$pkg_src_cmd")"
250             mv ./"$pkg_cmd_name"* "$pkg_src_cmd"
251         else
252             rm -rf "$pkg_src"
253             mv ./"$pkg_cmd_name"* "$pkg_src"
254         fi
255     }
256
257     # run post-install functions - just updating PATH by default
258     webi_post_install() {
259         webi_path_add "$(dirname "$pkg_dst_cmd")"
260     }
261
262     _webi_enable_exec() {
263         if [ -n "$(command -v spctl)" ] && [ -n "$(command -v xattr)" ]; then
264             # note: some packages contain files that cannot be affected by xattr
265             xattr -r -d com.apple.quarantine "$pkg_src" || true
266             return 0
267         fi
268         # TODO need to test that the above actually worked
269         # (and proceed to this below if it did not)
270         if [ -n "$(command -v spctl)" ]; then
271             echo "Checking permission to execute '$pkg_cmd_name' on macOS 11+"
272             set +e
273             is_allowed="$(spctl -a "$pkg_src_cmd" 2>&1 | grep valid)"
274             set -e
275             if [ -z "$is_allowed" ]; then
276                 echo ""
277                 echo "##########################################"
278                 echo "#  IMPORTANT: Permission Grant Required  #"
279                 echo "##########################################"
280                 echo ""
281                 echo "Requesting permission to execute '$pkg_cmd_name' on macOS 10.14+"
282                 echo ""
283                 sleep 3
284                 spctl --add "$pkg_src_cmd"
285             fi
286         fi
287     }
288
289     # a friendly message when all is well, showing the final install path in $HOME/.local
290     _webi_done_message() {
291         echo "Installed $(_webi_canonical_name) as $pkg_dst_cmd"
292     }
293
294     ##
295     ##
296     ## BEGIN custom override functions from <package>/install.sh
297     ##
298     ##
299
300     WEBI_SINGLE=
301
302     if [[ -z ${WEBI_WELCOME:-} ]]; then
303         echo ""
304         echo "Thanks for using webi to install '$PKG_NAME' on '$WEBI_OS/$WEBI_ARCH'."
305         echo "Have a problem? Experience a bug? Please let us know:"
306         echo "        https://github.com/webinstall/packages/issues"
307         echo ""
308     fi
309
310     function __init_installer() {
311
312         # do nothing - to satisfy parser prior to templating
313         echo -n ""
314
315         # {{ installer }}
316
317     }
318
319     __init_installer
320
321     ##
322     ##
323     ## END custom override functions
324     ##
325     ##
326
327     # run everything with defaults or overrides as needed
328     if [ -n "$(command -v pkg_get_current_version)" ]; then
329         pkg_cmd_name="${pkg_cmd_name:-$PKG_NAME}"
330
331         if [ -n "$WEBI_SINGLE" ]; then
332             pkg_dst_cmd="${pkg_dst_cmd:-$HOME/.local/bin/$pkg_cmd_name}"
333             pkg_dst="$pkg_dst_cmd" # "$(dirname "$(dirname $pkg_dst_cmd)")"
334
335             #pkg_src_cmd="${pkg_src_cmd:-$HOME/.local/opt/$pkg_cmd_name-v$WEBI_VERSION/bin/$pkg_cmd_name-v$WEBI_VERSION}"
336             pkg_src_cmd="${pkg_src_cmd:-$HOME/.local/opt/$pkg_cmd_name-v$WEBI_VERSION/bin/$pkg_cmd_name}"
337             pkg_src="$pkg_src_cmd" # "$(dirname "$(dirname $pkg_src_cmd)")"
338         else
339             pkg_dst="${pkg_dst:-$HOME/.local/opt/$pkg_cmd_name}"
340             pkg_dst_cmd="${pkg_dst_cmd:-$pkg_dst/bin/$pkg_cmd_name}"
341
342             pkg_src="${pkg_src:-$HOME/.local/opt/$pkg_cmd_name-v$WEBI_VERSION}"
343             pkg_src_cmd="${pkg_src_cmd:-$pkg_src/bin/$pkg_cmd_name}"
344         fi
345         # this script is templated and these are used elsewhere
346         # shellcheck disable=SC2034
347         pkg_src_bin="$(dirname "$pkg_src_cmd")"
348         # shellcheck disable=SC2034
349         pkg_dst_bin="$(dirname "$pkg_dst_cmd")"
350
351         if [[ -n "$(command -v pkg_pre_install)" ]]; then pkg_pre_install; else webi_pre_install; fi
352
353         pushd "$WEBI_TMP" > /dev/null 2>&1
354         echo "Installing to $pkg_src_cmd"
355         if [[ -n "$(command -v pkg_install)" ]]; then pkg_install; else webi_install; fi
356         chmod a+x "$pkg_src"
357         chmod a+x "$pkg_src_cmd"
358         popd > /dev/null 2>&1
359
360         webi_link
361
362         _webi_enable_exec
363         pushd "$WEBI_TMP" > /dev/null 2>&1
364         if [[ -n "$(command -v pkg_post_install)" ]]; then pkg_post_install; else webi_post_install; fi
365         popd > /dev/null 2>&1
366
367         pushd "$WEBI_TMP" > /dev/null 2>&1
368         if [[ -n "$(command -v pkg_done_message)" ]]; then pkg_done_message; else _webi_done_message; fi
369         popd > /dev/null 2>&1
370
371         echo ""
372     fi
373
374     webi_path_add "$HOME/.local/bin"
375     if [[ -z ${_WEBI_CHILD:-} ]] && [[ -f "$_webi_tmp/.PATH.env" ]]; then
376         if [[ -n $(cat "$_webi_tmp/.PATH.env") ]]; then
377             echo "You need to update your PATH to use $PKG_NAME:"
378             echo ""
379             sort -u "$_webi_tmp/.PATH.env"
380             rm -f "$_webi_tmp/.PATH.env"
381         fi
382     fi
383
384     # cleanup the temp directory
385     rm -rf "$WEBI_TMP"
386
387     # See? No magic. Just downloading and moving files.
388
389 }
390
391 __bootstrap_webi