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