begin support for batch templating
[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 else
50     echo "WARN: 'unzip' not found"
51 fi
52 if [ -n "\$(command -v tar)" ]; then
53         my_ext="tar,\$my_ext"
54 fi
55 my_ext="\$(echo "\$my_ext" | sed 's/,$//')" # nix trailing comma
56 set -e
57
58 ##
59 ## Detect http client
60 ##
61 set +e
62 export WEBI_CURL="\$(command -v curl)"
63 export WEBI_WGET="\$(command -v wget)"
64 set -e
65
66 export WEBI_BOOT="\$(mktemp -d -t "\$my_package-bootstrap.XXXXXXXX")"
67 export WEBI_HOST="\${WEBI_HOST:-https://webinstall.dev}"
68 export WEBI_UA="\$(uname -a)"
69
70 my_installer_url="\$WEBI_HOST/api/installers/\$my_package.bash?formats=\$my_ext"
71 set +e
72 if [ -n "\$WEBI_CURL" ]; then
73         curl -fsSL "\$my_installer_url" -H "User-Agent: curl \$WEBI_UA" \\
74                 -o "\$WEBI_BOOT/\$my_package-bootstrap.sh"
75 else
76         wget -q "\$my_installer_url" --user-agent="wget \$WEBI_UA" \\
77                 -O "\$WEBI_BOOT/\$my_package-bootstrap.sh"
78 fi
79 if ! [ \$? -eq 0 ]; then
80   echo "error fetching '\$my_installer_url'"
81   exit 1
82 fi
83 set -e
84
85 pushd "\$WEBI_BOOT" 2>&1 > /dev/null
86         bash "\$my_package-bootstrap.sh"
87 popd 2>&1 > /dev/null
88
89 rm -rf "\$WEBI_BOOT"
90
91 }
92 EOF
93
94 chmod a+x "$HOME/.local/bin/webi"
95
96 if [ -n "${WEBI_PKG:-}" ]; then
97     "$HOME/.local/bin/webi" "${WEBI_PKG}"
98 fi
99
100 }