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