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)
7 function __bootstrap_webi() {
15 # TODO should this be BASEURL instead?
26 # TODO not sure if BUILD is the best name for this
39 WEBI_PKG_PATH="${HOME}/Downloads/webi/${PKG_NAME:-error}/${WEBI_VERSION:-latest}"
43 ## Set up tmp, download, and install directories
46 WEBI_TMP=${WEBI_TMP:-"$(mktemp -d -t webinstall-"${WEBI_PKG:-}".XXXXXXXX)"}
47 export _webi_tmp="${_webi_tmp:-"$HOME/.local/opt/webi-tmp.d"}"
49 mkdir -p "${WEBI_PKG_PATH}"
50 mkdir -p "$HOME/.local/bin"
51 mkdir -p "$HOME/.local/opt"
57 WEBI_CURL="$(command -v curl)"
59 WEBI_WGET="$(command -v wget)"
63 # get the special formatted version (i.e. "go is go1.14" while node is "node v12.10.8")
65 _webi_canonical_name() {
66 if [ -n "$my_versioned_name" ]; then
67 echo "$my_versioned_name"
71 if [ -n "$(command -v pkg_format_cmd_version)" ]; then
72 my_versioned_name="'$(pkg_format_cmd_version "$WEBI_VERSION")'"
74 my_versioned_name="'$pkg_cmd_name v$WEBI_VERSION'"
77 echo "$my_versioned_name"
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
84 if [ -n "$(command -v pkg_link)" ]; then
89 if [ -n "$WEBI_SINGLE" ] || [ "single" == "${1:-}" ]; then
91 ln -s "$pkg_src_cmd" "$pkg_dst_cmd"
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>
96 ln -s "$pkg_src" "$pkg_dst"
100 # detect if this program is already installed or if an installed version may cause conflict
102 # Test for existing version
105 PATH="$(dirname "$pkg_dst_cmd"):$PATH"
107 my_current_cmd="$(command -v "$pkg_cmd_name")"
109 if [ -n "$my_current_cmd" ]; then
110 my_canonical_name="$(_webi_canonical_name)"
111 if [ "$my_current_cmd" != "$pkg_dst_cmd" ]; then
112 echo >&2 "WARN: possible PATH conflict between $my_canonical_name and currently installed version"
113 echo >&2 " ${pkg_dst_cmd} (new)"
114 echo >&2 " ${my_current_cmd} (existing)"
115 #my_current_version=false
117 # 'readlink' can't read links in paths on macOS 🤦
118 # but that's okay, 'cmp -s' is good enough for us
119 if cmp -s "${pkg_src_cmd}" "${my_current_cmd}"; then
120 echo "${my_canonical_name} already installed:"
121 echo -n " ${pkg_dst}"
122 if [[ ${pkg_src_cmd} != "${my_current_cmd}" ]]; then
123 echo -n " => ${pkg_src}"
128 if [ -x "$pkg_src_cmd" ]; then
129 # shellcheck disable=2119
130 # this function takes no args
132 echo "switched to $my_canonical_name:"
133 echo " ${pkg_dst} => ${pkg_src}"
137 export PATH="$my_path"
140 # detect if file is downloaded, and how to download it
142 if [ -n "${1:-}" ]; then
145 if [ "error" == "$WEBI_CHANNEL" ]; then
146 # TODO pass back requested OS / Arch / Version
147 echo >&2 "Error: no '$PKG_NAME' release for '${WEBI_OS:-}' on '$WEBI_ARCH' as one of '$WEBI_FORMATS' by the tag '${WEBI_TAG:-}'"
148 echo >&2 " '$PKG_NAME' is available for '$PKG_OSES' on '$PKG_ARCHES' as one of '$PKG_FORMATS'"
149 echo >&2 " (check that the package name and version are correct)"
151 echo >&2 " Double check at $(echo "$WEBI_RELEASES" | sed 's:\?.*::')"
155 my_url="$WEBI_PKG_URL"
157 if [ -n "${2:-}" ]; then
160 my_dl="${WEBI_PKG_PATH}/$WEBI_PKG_FILE"
163 WEBI_PKG_DOWNLOAD="${my_dl}"
164 export WEBI_PKG_DOWNLOAD
166 if [ -e "$my_dl" ]; then
171 echo "Downloading $PKG_NAME from"
174 # It's only 2020, we can't expect to have reliable CLI tools
175 # to tell us the size of a file as part of a base system...
176 if [ -n "$WEBI_WGET" ]; then
177 # wget has resumable downloads
178 # TODO wget -c --content-disposition "$my_url"
181 if [[ $- == *i* ]]; then
182 my_show_progress="--show-progress"
184 if ! wget -q $my_show_progress --user-agent="wget $WEBI_UA" -c "$my_url" -O "$my_dl.part"; then
185 echo >&2 "failed to download from $WEBI_PKG_URL"
190 # Neither GNU nor BSD curl have sane resume download options, hence we don't bother
191 # TODO curl -fsSL --remote-name --remote-header-name --write-out "$my_url"
192 my_show_progress="-#"
193 if [[ $- == *i* ]]; then
196 # shellcheck disable=SC2086
197 # we want the flags to be split
198 curl -fSL $my_show_progress -H "User-Agent: curl $WEBI_UA" "$my_url" -o "$my_dl.part"
200 mv "$my_dl.part" "$my_dl"
203 echo "Saved as $my_dl"
206 # detect which archives can be used
208 pushd "$WEBI_TMP" > /dev/null 2>&1
209 if [ "tar" == "$WEBI_EXT" ]; then
210 echo "Extracting ${WEBI_PKG_PATH}/$WEBI_PKG_FILE"
211 tar xf "${WEBI_PKG_PATH}/$WEBI_PKG_FILE"
212 elif [ "zip" == "$WEBI_EXT" ]; then
213 echo "Extracting ${WEBI_PKG_PATH}/$WEBI_PKG_FILE"
214 unzip "${WEBI_PKG_PATH}/$WEBI_PKG_FILE" > __unzip__.log
215 elif [ "exe" == "$WEBI_EXT" ]; then
216 echo "Moving ${WEBI_PKG_PATH}/$WEBI_PKG_FILE"
217 mv "${WEBI_PKG_PATH}/$WEBI_PKG_FILE" .
218 elif [ "xz" == "$WEBI_EXT" ]; then
219 echo "Inflating ${WEBI_PKG_PATH}/$WEBI_PKG_FILE"
220 unxz -c "${WEBI_PKG_PATH}/$WEBI_PKG_FILE" > "$(basename "$WEBI_PKG_FILE")"
223 echo "Failed to extract ${WEBI_PKG_PATH}/$WEBI_PKG_FILE"
226 popd > /dev/null 2>&1
229 # use 'pathman' to update $HOME/.config/envman/PATH.env
231 # make sure that we don't recursively install pathman with webi
233 export PATH="$HOME/.local/bin:$PATH"
235 # install pathman if not already installed
236 if [ -z "$(command -v pathman)" ]; then
237 "$HOME/.local/bin/webi" pathman > /dev/null
240 export PATH="$my_path"
242 # in case pathman was recently installed and the PATH not updated
243 mkdir -p "$_webi_tmp"
244 # prevent "too few arguments" output on bash when there are 0 lines of stdout
245 "$HOME/.local/bin/pathman" add "$1" | grep "export" 2> /dev/null >> "$_webi_tmp/.PATH.env" || true
248 # group common pre-install tasks as default
255 # move commands from the extracted archive directory to $HOME/.local/opt or $HOME/.local/bin
256 # shellcheck disable=2120
257 # webi_install may be sourced and used elsewhere
259 if [ -n "$WEBI_SINGLE" ] || [ "single" == "${1:-}" ]; then
260 mkdir -p "$(dirname "$pkg_src_cmd")"
261 mv ./"$pkg_cmd_name"* "$pkg_src_cmd"
264 mv ./"$pkg_cmd_name"* "$pkg_src"
268 # run post-install functions - just updating PATH by default
269 webi_post_install() {
270 webi_path_add "$(dirname "$pkg_dst_cmd")"
273 _webi_enable_exec() {
274 if [ -n "$(command -v spctl)" ] && [ -n "$(command -v xattr)" ]; then
275 # note: some packages contain files that cannot be affected by xattr
276 xattr -r -d com.apple.quarantine "$pkg_src" || true
279 # TODO need to test that the above actually worked
280 # (and proceed to this below if it did not)
281 if [ -n "$(command -v spctl)" ]; then
282 echo "Checking permission to execute '$pkg_cmd_name' on macOS 11+"
284 is_allowed="$(spctl -a "$pkg_src_cmd" 2>&1 | grep valid)"
286 if [ -z "$is_allowed" ]; then
288 echo "##########################################"
289 echo "# IMPORTANT: Permission Grant Required #"
290 echo "##########################################"
292 echo "Requesting permission to execute '$pkg_cmd_name' on macOS 10.14+"
295 spctl --add "$pkg_src_cmd"
300 # a friendly message when all is well, showing the final install path in $HOME/.local
301 _webi_done_message() {
302 echo "Installed $(_webi_canonical_name) as $pkg_dst_cmd"
307 ## BEGIN custom override functions from <package>/install.sh
313 if [[ -z ${WEBI_WELCOME:-} ]]; then
315 printf "Thanks for using webi to install '\e[32m${WEBI_PKG:-}\e[0m' on '\e[31m$(uname -s)/$(uname -m)\e[0m'.\n"
316 echo "Have a problem? Experience a bug? Please let us know:"
317 echo " https://github.com/webinstall/webi-installers/issues"
319 printf "\e[31mLovin'\e[0m it? Say thanks with a \e[34mStar on GitHub\e[0m:\n"
320 printf " \e[32mhttps://github.com/webinstall/webi-installers\e[0m\n"
324 function __init_installer() {
326 # do nothing - to satisfy parser prior to templating
337 ## END custom override functions
341 # run everything with defaults or overrides as needed
342 if command -v pkg_install > /dev/null ||
343 command -v pkg_link > /dev/null ||
344 command -v pkg_post_install > /dev/null ||
345 command -v pkg_done_message > /dev/null ||
346 command -v pkg_format_cmd_version > /dev/null ||
347 [[ -n ${WEBI_SINGLE:-} ]] ||
348 [[ -n ${pkg_cmd_name:-} ]] ||
349 [[ -n ${pkg_dst_cmd:-} ]] ||
350 [[ -n ${pkg_dst_dir:-} ]] ||
351 [[ -n ${pkg_dst:-} ]] ||
352 [[ -n ${pkg_src_cmd:-} ]] ||
353 [[ -n ${pkg_src_dir:-} ]] ||
354 [[ -n ${pkg_src:-} ]]; then
356 pkg_cmd_name="${pkg_cmd_name:-$PKG_NAME}"
358 if [ -n "$WEBI_SINGLE" ]; then
359 pkg_dst_cmd="${pkg_dst_cmd:-$HOME/.local/bin/$pkg_cmd_name}"
360 pkg_dst="$pkg_dst_cmd" # "$(dirname "$(dirname $pkg_dst_cmd)")"
362 #pkg_src_cmd="${pkg_src_cmd:-$HOME/.local/opt/$pkg_cmd_name-v$WEBI_VERSION/bin/$pkg_cmd_name-v$WEBI_VERSION}"
363 pkg_src_cmd="${pkg_src_cmd:-$HOME/.local/opt/$pkg_cmd_name-v$WEBI_VERSION/bin/$pkg_cmd_name}"
364 pkg_src="$pkg_src_cmd" # "$(dirname "$(dirname $pkg_src_cmd)")"
366 pkg_dst="${pkg_dst:-$HOME/.local/opt/$pkg_cmd_name}"
367 pkg_dst_cmd="${pkg_dst_cmd:-$pkg_dst/bin/$pkg_cmd_name}"
369 pkg_src="${pkg_src:-$HOME/.local/opt/$pkg_cmd_name-v$WEBI_VERSION}"
370 pkg_src_cmd="${pkg_src_cmd:-$pkg_src/bin/$pkg_cmd_name}"
372 # this script is templated and these are used elsewhere
373 # shellcheck disable=SC2034
374 pkg_src_bin="$(dirname "$pkg_src_cmd")"
375 # shellcheck disable=SC2034
376 pkg_dst_bin="$(dirname "$pkg_dst_cmd")"
378 if [[ -n "$(command -v pkg_pre_install)" ]]; then pkg_pre_install; else webi_pre_install; fi
380 pushd "$WEBI_TMP" > /dev/null 2>&1
381 echo "Installing to $pkg_src_cmd"
382 if [[ -n "$(command -v pkg_install)" ]]; then pkg_install; else webi_install; fi
384 chmod a+x "$pkg_src_cmd"
385 popd > /dev/null 2>&1
390 pushd "$WEBI_TMP" > /dev/null 2>&1
391 if [[ -n "$(command -v pkg_post_install)" ]]; then pkg_post_install; else webi_post_install; fi
392 popd > /dev/null 2>&1
394 pushd "$WEBI_TMP" > /dev/null 2>&1
395 if [[ -n "$(command -v pkg_done_message)" ]]; then pkg_done_message; else _webi_done_message; fi
396 popd > /dev/null 2>&1
401 webi_path_add "$HOME/.local/bin"
402 if [[ -z ${_WEBI_CHILD:-} ]] && [[ -f "$_webi_tmp/.PATH.env" ]]; then
403 if [[ -n $(cat "$_webi_tmp/.PATH.env") ]]; then
404 echo "You need to update your PATH to use $PKG_NAME:"
406 sort -u "$_webi_tmp/.PATH.env"
407 rm -f "$_webi_tmp/.PATH.env"
411 # cleanup the temp directory
414 # See? No magic. Just downloading and moving files.