11 # TODO should this be BASEURL instead?
20 # TODO not sure if BUILD is the best name for this
31 ## Set up tmp, download, and install directories
34 WEBI_TMP=${WEBI_TMP:-"$(mktemp -d -t webinstall-${WEBI_PKG:-}.XXXXXXXX)"}
36 mkdir -p "$HOME/Downloads"
37 mkdir -p "$HOME/.local/bin"
38 mkdir -p "$HOME/.local/opt"
44 export WEBI_CURL="$(command -v curl)"
45 export WEBI_WGET="$(command -v wget)"
49 # get the special formatted version (i.e. "go is go1.14" while node is "node v12.10.8")
51 _webi_canonical_name() {
52 if [ -n "$my_versioned_name" ]; then
53 echo "$my_versioned_name"
57 if [ -n "$(command -v pkg_format_cmd_version)" ]; then
58 my_versioned_name="$(pkg_format_cmd_version "$WEBI_VERSION")"
60 my_versioned_name="'$pkg_cmd_name' v$WEBI_VERSION"
63 echo "$my_versioned_name"
66 # update symlinks according to $HOME/.local/opt and $HOME/.local/bin install paths.
68 if [ -n "$(command -v pkg_link)" ]; then
73 if [ -n "$WEBI_SINGLE" ] || [ "single" == "${1:-}" ]; then
74 if [ -L "$pkg_dst_cmd" ]; then
76 elif [ -e "$pkg_dst_cmd" ]; then
77 echo "remove $pkg_dst_cmd?"
78 rm -rf -i "$pkg_dst_cmd"
80 ln -s "$pkg_src_cmd" "$pkg_dst_cmd"
82 # 'pkg_dst' will default to $HOME/.local/opt/node
83 # 'pkg_src' will be the installed version, such as to $HOME/.local/opt/node-v12.8.0
84 if [ -L "$pkg_dst" ]; then
86 elif [ -e "$pkg_dst" ]; then
87 echo "remove $pkg_dst?"
90 ln -s "$pkg_src" "$pkg_dst"
94 # detect if this program is already installed or if an installed version may cause conflict
96 # Test for existing version
98 my_current_cmd="$(command -v "$pkg_cmd_name")"
100 if [ -n "$my_current_cmd" ]; then
101 pkg_current_version="$(pkg_get_current_version 2>/dev/null | head -n 1)"
102 # remove trailing '.0's for golang's sake
103 my_current_version="$(echo $pkg_current_version | sed 's:\.0::g')"
104 my_src_version="$(echo $WEBI_VERSION | sed 's:\.0::g')"
105 my_canonical_name="$(_webi_canonical_name)"
106 if [ "$my_src_version" == "$my_current_version" ]; then
107 echo "$my_canonical_name already installed at $my_current_cmd"
110 if [ "$my_current_cmd" != "$pkg_dst_cmd" ]; then
111 echo "WARN: possible conflict between $my_canonical_name and $pkg_current_version at $my_current_cmd"
113 if [ -x "$pkg_src_cmd" ]; then
115 echo "switched to $my_canonical_name at $pkg_src"
122 # detect if file is downloaded, and how to download it
124 if [ -n "${1:-}" ]; then
127 if [ "error" == "$WEBI_CHANNEL" ]; then
128 # TODO pass back requested OS / Arch / Version
129 echo "Error: no '$WEBI_NAME' release found for the given OS and architecture by that tag or version"
130 echo " (check that the package name and version are correct)"
133 my_url="$WEBI_PKG_URL"
135 if [ -n "${2:-}" ]; then
138 my_dl="$HOME/Downloads/$WEBI_PKG_FILE"
141 if [ -e "$my_dl" ]; then
146 echo "Downloading $WEBI_NAME to $my_dl"
148 # It's only 2020, we can't expect to have reliable CLI tools
149 # to tell us the size of a file as part of a base system...
150 if [ -n "$WEBI_WGET" ]; then
151 # wget has resumable downloads
152 # TODO wget -c --content-disposition "$my_url"
154 wget -q --show-progress --user-agent="wget $WEBI_UA" -c "$my_url" -O "$my_dl.part"
155 if ! [ $? -eq 0 ]; then
156 echo "failed to download from $WEBI_PKG_URL"
161 # Neither GNU nor BSD curl have sane resume download options, hence we don't bother
162 # TODO curl -fsSL --remote-name --remote-header-name --write-out "$my_url"
163 curl -fSL -H "User-Agent: curl $WEBI_UA" "$my_url" -o "$my_dl.part"
166 mv "$my_dl.part" "$my_dl"
169 # detect which archives can be used
171 pushd "$WEBI_TMP" 2>&1 >/dev/null
172 if [ "tar" == "$WEBI_EXT" ]; then
173 echo "Extracting $HOME/Downloads/$WEBI_PKG_FILE"
174 tar xf "$HOME/Downloads/$WEBI_PKG_FILE"
175 elif [ "zip" == "$WEBI_EXT" ]; then
176 echo "Extracting $HOME/Downloads/$WEBI_PKG_FILE"
177 unzip "$HOME/Downloads/$WEBI_PKG_FILE"
178 elif [ "exe" == "$WEBI_EXT" ]; then
179 # do nothing (but don't leave an empty if block either)
181 elif [ "xz" == "$WEBI_EXT" ]; then
182 echo "Inflating $HOME/Downloads/$WEBI_PKG_FILE"
183 unxz -c "$HOME/Downloads/$WEBI_PKG_FILE" > $(basename "$WEBI_PKG_FILE")
186 echo "Failed to extract $HOME/Downloads/$WEBI_PKG_FILE"
192 # use 'pathman' to update $HOME/.config/envman/PATH.env
194 # make sure that we don't recursively install pathman with webi
196 export PATH="$HOME/.local/bin:$PATH"
198 my_pathman=$(command -v pathman)
200 export PATH="$my_path"
202 # install pathman if not already installed
203 if [ -z "$my_pathman" ]; then
204 "$HOME/.local/bin/webi" pathman
205 export PATH="$HOME/.local/bin:$PATH"
208 # in case pathman was recently installed and the PATH not updated
209 "$HOME/.local/bin/pathman" add "$1"
212 # group common pre-install tasks as default
219 # move commands from the extracted archive directory to $HOME/.local/opt or $HOME/.local/bin
221 if [ -n "$WEBI_SINGLE" ] || [ "single" == "${1:-}" ]; then
222 mkdir -p "$(dirname $pkg_src_cmd)"
223 mv ./"$pkg_cmd_name"* "$pkg_src_cmd"
224 chmod a+x "$pkg_src_cmd"
226 mkdir -p "$(dirname $pkg_src)"
227 if [ -L "$pkg_src" ]; then
229 elif [ -e "$pkg_src" ]; then
230 echo "remove $pkg_src?"
233 mv ./"$pkg_cmd_name"* "$pkg_src"
237 # run post-install functions - just updating PATH by default
238 webi_post_install() {
239 webi_path_add "$pkg_dst_bin"
242 # a friendly message when all is well, showing the final install path in $HOME/.local
243 _webi_done_message() {
244 echo "Installed $(_webi_canonical_name) as $pkg_dst_cmd"
249 ## BEGIN custom override functions from <package>/install.sh
263 ## END custom override functions
267 # run everything with defaults or overrides as needed
268 if [ -n "$(command -v pkg_get_current_version)" ]; then
269 pkg_cmd_name="${pkg_cmd_name:-$WEBI_NAME}"
271 if [ -n "$WEBI_SINGLE" ]; then
272 pkg_dst_cmd="${pkg_dst_cmd:-$HOME/.local/bin/$pkg_cmd_name}"
273 pkg_dst_bin="$(dirname $pkg_dst_cmd)"
274 pkg_dst="$(dirname $pkg_dst_bin)"
276 pkg_src_cmd="${pkg_src_cmd:-$HOME/.local/xbin/$pkg_cmd_name-$WEBI_VERSION}"
277 pkg_src_bin="$(dirname $pkg_src_cmd)"
278 pkg_src="$(dirname $pkg_src_bin)"
280 pkg_dst="${pkg_dst:-$HOME/.local/opt/$pkg_cmd_name}"
281 pkg_dst_bin="${pkg_dst_bin:-$pkg_dst/bin}"
282 pkg_dst_cmd="${pkg_dst_cmd:-$pkg_dst_bin/$pkg_cmd_name}"
284 pkg_src="${pkg_src:-$HOME/.local/opt/$pkg_cmd_name-v$WEBI_VERSION}"
285 pkg_src_bin="${pkg_src_bin:-$pkg_src/bin}"
286 pkg_src_cmd="${pkg_src_cmd:-$pkg_src_bin/$pkg_cmd_name}"
289 [ -n "$(command -v pkg_pre_install)" ] && pkg_pre_install || webi_pre_install
291 pushd "$WEBI_TMP" 2>&1 >/dev/null
292 echo "Installing to $pkg_src_cmd"
293 [ -n "$(command -v pkg_install)" ] && pkg_install || webi_install
298 pushd "$WEBI_TMP" 2>&1 >/dev/null
299 [ -n "$(command -v pkg_post_install)" ] && pkg_post_install || webi_post_install
302 pushd "$WEBI_TMP" 2>&1 >/dev/null
303 [ -n "$(command -v pkg_done_message)" ] && pkg_done_message || _webi_done_message
309 # cleanup the temp directory
312 # See? No magic. Just downloading and moving files.