cleanup: shfmt, shellcheck, and whitespace
[webi-installers/.git] / _webi / bootstrap.sh
1 #!/bin/bash
2
3 {
4
5     set -e
6     set -u
7
8     #WEBI_PKG=
9     #WEBI_HOST=https://webinstall.dev
10     export WEBI_HOST
11
12     mkdir -p "$HOME/.local/bin"
13
14     cat << EOF > "$HOME/.local/bin/webi"
15 #!/bin/bash
16
17 set -e
18 set -u
19
20 {
21
22 export WEBI_TIMESTAMP=\$(date +%F_%H-%M-%S)
23 export _webi_tmp="\${_webi_tmp:-\$(mktemp -d -t webi-\$WEBI_TIMESTAMP.XXXXXXXX)}"
24
25 if [ -n "\${_WEBI_PARENT:-}" ]; then
26   export _WEBI_CHILD=true
27 else
28   export _WEBI_CHILD=
29 fi
30 export _WEBI_PARENT=true
31
32 ##
33 ## Detect acceptable package formats
34 ##
35
36 my_ext=""
37 set +e
38 # NOTE: the order here is least favorable to most favorable
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 if [ -n "\$(command -v git)" ]; then
48     my_ext="git,\$my_ext"
49 fi
50 if [ -n "\$(command -v unxz)" ]; then
51     my_ext="xz,\$my_ext"
52 fi
53 if [ -n "\$(command -v unzip)" ]; then
54     my_ext="zip,\$my_ext"
55 fi
56 # for mac/linux 'exe' refers to the uncompressed binary without extension
57 my_ext="exe,\$my_ext"
58 if [ -n "\$(command -v tar)" ]; then
59     my_ext="tar,\$my_ext"
60 fi
61 my_ext="\$(echo "\$my_ext" | sed 's/,$//')" # nix trailing comma
62 set -e
63
64
65 ##
66 ## Detect http client
67 ##
68
69 set +e
70 export WEBI_CURL="\$(command -v curl)"
71 export WEBI_WGET="\$(command -v wget)"
72 set -e
73
74 export WEBI_HOST="\${WEBI_HOST:-https://webinstall.dev}"
75 export WEBI_UA="\$(uname -a)"
76
77
78 webinstall() {
79
80     my_package="\${1:-}"
81     if [ -z "\$my_package" ]; then
82         >&2 echo "Usage: webi <package>@<version> ..."
83         >&2 echo "Example: webi node@lts rg"
84         exit 1
85     fi
86
87     export WEBI_BOOT="\$(mktemp -d -t "\$my_package-bootstrap.\$WEBI_TIMESTAMP.XXXXXXXX")"
88
89     my_installer_url="\$WEBI_HOST/api/installers/\$my_package.sh?formats=\$my_ext"
90     set +e
91     if [ -n "\$WEBI_CURL" ]; then
92         curl -fsSL "\$my_installer_url" -H "User-Agent: curl \$WEBI_UA" \\
93             -o "\$WEBI_BOOT/\$my_package-bootstrap.sh"
94     else
95         wget -q "\$my_installer_url" --user-agent="wget \$WEBI_UA" \\
96             -O "\$WEBI_BOOT/\$my_package-bootstrap.sh"
97     fi
98     if ! [ \$? -eq 0 ]; then
99       >&2 echo "error fetching '\$my_installer_url'"
100       exit 1
101     fi
102     set -e
103
104     pushd "\$WEBI_BOOT" 2>&1 > /dev/null
105         bash "\$my_package-bootstrap.sh"
106     popd 2>&1 > /dev/null
107
108     rm -rf "\$WEBI_BOOT"
109
110 }
111
112 show_path_updates() {
113
114     if ! [ -n "\${_WEBI_CHILD}" ]; then
115         if [ -f "\$_webi_tmp/.PATH.env" ]; then
116             my_paths=\$(cat "\$_webi_tmp/.PATH.env" | sort -u)
117             if [ -n "\$my_paths" ]; then
118                 echo "IMPORTANT: You must update you PATH to use the installed program(s)"
119                 echo ""
120                 echo "You can CLOSE and REOPEN Terminal, or RUN these exports:"
121                 echo ""
122                 echo "\$my_paths"
123                 echo ""
124             fi
125             rm -f "\$_webi_tmp/.PATH.env"
126         fi
127     fi
128
129 }
130
131 for pkgname in "\$@"
132 do
133     webinstall "\$pkgname"
134 done
135
136 show_path_updates
137
138 }
139
140 EOF
141
142     chmod a+x "$HOME/.local/bin/webi"
143
144     if [ -n "${WEBI_PKG:-}" ]; then
145         "$HOME/.local/bin/webi" "${WEBI_PKG}"
146     fi
147
148 }