use unquote backslash paths for windows
[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 if [ -n "\$(command -v tar)" ]; then
57     my_ext="tar,\$my_ext"
58 fi
59 my_ext="\$(echo "\$my_ext" | sed 's/,$//')" # nix trailing comma
60 set -e
61
62
63 ##
64 ## Detect http client
65 ##
66
67 set +e
68 export WEBI_CURL="\$(command -v curl)"
69 export WEBI_WGET="\$(command -v wget)"
70 set -e
71
72 export WEBI_HOST="\${WEBI_HOST:-https://webinstall.dev}"
73 export WEBI_UA="\$(uname -a)"
74
75
76 webinstall() {
77
78     my_package="\${1:-}"
79     if [ -z "\$my_package" ]; then
80         >&2 echo "Usage: webi <package>@<version> ..."
81         >&2 echo "Example: webi node@lts rg"
82         exit 1
83     fi
84
85     export WEBI_BOOT="\$(mktemp -d -t "\$my_package-bootstrap.\$WEBI_TIMESTAMP.XXXXXXXX")"
86
87     my_installer_url="\$WEBI_HOST/api/installers/\$my_package.sh?formats=\$my_ext"
88     set +e
89     if [ -n "\$WEBI_CURL" ]; then
90         curl -fsSL "\$my_installer_url" -H "User-Agent: curl \$WEBI_UA" \\
91             -o "\$WEBI_BOOT/\$my_package-bootstrap.sh"
92     else
93         wget -q "\$my_installer_url" --user-agent="wget \$WEBI_UA" \\
94             -O "\$WEBI_BOOT/\$my_package-bootstrap.sh"
95     fi
96     if ! [ \$? -eq 0 ]; then
97       >&2 echo "error fetching '\$my_installer_url'"
98       exit 1
99     fi
100     set -e
101
102     pushd "\$WEBI_BOOT" 2>&1 > /dev/null
103         bash "\$my_package-bootstrap.sh"
104     popd 2>&1 > /dev/null
105
106     rm -rf "\$WEBI_BOOT"
107
108 }
109
110 show_path_updates() {
111
112     if ! [ -n "\${_WEBI_CHILD}" ]; then
113         if [ -f "\$_webi_tmp/.PATH.env" ]; then
114             my_paths=\$(cat "\$_webi_tmp/.PATH.env" | sort -u)
115             if [ -n "\$my_paths" ]; then
116                 echo "IMPORTANT: You must update you PATH to use the installed program(s)"
117                 echo ""
118                 echo "You can CLOSE and REOPEN Terminal, or RUN these exports:"
119                 echo ""
120                 echo "\$my_paths"
121                 echo ""
122             fi
123             rm -f "\$_webi_tmp/.PATH.env"
124         fi
125     fi
126
127 }
128
129 for pkgname in "\$@"
130 do
131     webinstall "\$pkgname"
132 done
133
134 show_path_updates
135
136 }
137
138 EOF
139
140 chmod a+x "$HOME/.local/bin/webi"
141
142 if [ -n "${WEBI_PKG:-}" ]; then
143     "$HOME/.local/bin/webi" "${WEBI_PKG}"
144 fi
145
146 }