update for latest API
[webi-installers/.git] / webi / bootstrap.bash
1 #!/bin/bash
2
3 {
4
5 #WEBI_PKG=
6 #WEBI_HOST=https://webinstall.dev
7
8 mkdir -p "$HOME/.local/bin"
9
10 cat << EOF > "$HOME/.local/bin/webi"
11 #!/bin/bash
12
13 set -e
14 set -u
15
16 my_package="\${1:-}"
17 if [ -z "\$my_package" ]; then
18         echo "Usage: webi <package>@<version>"
19         echo "Example: webi node@latest"
20         exit 1
21 fi
22
23 ##
24 ## Detect acceptable package formats
25 ##
26
27 my_ext=""
28 set +e
29 if [ -n "\$(command -v git)" ]; then
30         my_ext="git,\$my_ext"
31 fi
32 if [ -n "\$(command -v tar)" ]; then
33         my_ext="tar,\$my_ext"
34 fi
35 if [ -n "\$(command -v unzip)" ]; then
36         my_ext="zip,\$my_ext"
37 fi
38 if [ -n "\$(command -v pkgutil)" ]; then
39         my_ext="pkg,\$my_ext"
40 fi
41 # disable this check for the sake of building the macOS installer on Linux
42 #if [ -n "\$(command -v diskutil)" ]; then
43         # note: could also detect via hdiutil
44         my_ext="dmg,\$my_ext"
45 #fi
46 my_ext="\$(echo "\$my_ext" | sed 's/,$//')" # nix trailing comma
47 set -e
48
49 ##
50 ## Detect http client
51 ##
52 set +e
53 export WEBI_CURL="\$(command -v curl)"
54 export WEBI_WGET="\$(command -v wget)"
55 set -e
56
57 export WEBI_BOOT="\$(mktemp -d -t "\$my_package-bootstrap.XXXXXXXX")"
58 export WEBI_HOST="\${WEBI_HOST:-https://webinstall.dev}"
59 export WEBI_UA="\$(uname -a)"
60
61 my_installer_url="\$WEBI_HOST/api/installers/\$my_package.bash?formats=\$my_ext"
62 if [ -n "\$WEBI_CURL" ]; then
63         curl -fsSL "\$my_installer_url" -H "User-Agent: curl \$WEBI_UA" \\
64                 -o "\$WEBI_BOOT/\$my_package-bootstrap.sh"
65 else
66         wget -q "\$my_installer_url" --user-agent="wget \$WEBI_UA" \\
67                 -O "\$WEBI_BOOT/\$my_package-bootstrap.sh"
68 fi
69
70 pushd "\$WEBI_BOOT" 2>&1 > /dev/null
71         bash "\$my_package-bootstrap.sh"
72 popd 2>&1 > /dev/null
73
74 rm -rf "\$WEBI_BOOT"
75 EOF
76
77 chmod a+x "$HOME/.local/bin/webi"
78
79 if [ -n "${WEBI_PKG:-}" ]; then
80     "$HOME/.local/bin/webi" "${WEBI_PKG}"
81 fi
82
83 }