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