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