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