feature: spam for stars plzkthx
[webi-installers/.git] / _webi / bootstrap.sh
1 #!/bin/bash
2
3 #set -x
4
5 function __install_webi() {
6
7     #WEBI_PKG=
8     #WEBI_HOST=https://webinstall.dev
9     export WEBI_HOST
10
11     echo ""
12     echo -e "Thanks for using webi to install '\e[32m${WEBI_PKG:-}\e[0m' on '\e[31m$(uname -s)/$(uname -m)\e[0m'."
13     echo "Have a problem? Experience a bug? Please let us know:"
14     echo "        https://github.com/webinstall/webi-installers/issues"
15     echo ""
16     echo -e "\e[31mLovin'\e[0m it? Say thanks with a \e[34mStar on GitHub\e[0m:"
17     echo -e "        \e[32mhttps://github.com/webinstall/webi-installers\e[0m"
18     echo ""
19     WEBI_WELCOME=true
20     export WEBI_WELCOME
21
22     set -e
23     set -u
24
25     mkdir -p "$HOME/.local/bin"
26
27     cat << EOF > "$HOME/.local/bin/webi"
28 #!/bin/bash
29
30 set -e
31 set -u
32 #set -x
33
34 function __webi_main () {
35
36     export WEBI_TIMESTAMP=\$(date +%F_%H-%M-%S)
37     export _webi_tmp="\${_webi_tmp:-\$(mktemp -d -t webi-\$WEBI_TIMESTAMP.XXXXXXXX)}"
38
39     if [ -n "\${_WEBI_PARENT:-}" ]; then
40         export _WEBI_CHILD=true
41     else
42         export _WEBI_CHILD=
43     fi
44     export _WEBI_PARENT=true
45
46     ##
47     ## Detect acceptable package formats
48     ##
49
50     my_ext=""
51     set +e
52     # NOTE: the order here is least favorable to most favorable
53     if [ -n "\$(command -v pkgutil)" ]; then
54         my_ext="pkg,\$my_ext"
55     fi
56     # disable this check for the sake of building the macOS installer on Linux
57     #if [ -n "\$(command -v diskutil)" ]; then
58         # note: could also detect via hdiutil
59         my_ext="dmg,\$my_ext"
60     #fi
61     if [ -n "\$(command -v git)" ]; then
62         my_ext="git,\$my_ext"
63     fi
64     if [ -n "\$(command -v unxz)" ]; then
65         my_ext="xz,\$my_ext"
66     fi
67     if [ -n "\$(command -v unzip)" ]; then
68         my_ext="zip,\$my_ext"
69     fi
70     # for mac/linux 'exe' refers to the uncompressed binary without extension
71     my_ext="exe,\$my_ext"
72     if [ -n "\$(command -v tar)" ]; then
73         my_ext="tar,\$my_ext"
74     fi
75     my_ext="\$(echo "\$my_ext" | sed 's/,$//')" # nix trailing comma
76     set -e
77
78
79     ##
80     ## Detect http client
81     ##
82
83     set +e
84     export WEBI_CURL="\$(command -v curl)"
85     export WEBI_WGET="\$(command -v wget)"
86     set -e
87
88     export WEBI_HOST="\${WEBI_HOST:-https://webinstall.dev}"
89     export WEBI_UA="\$(uname -a)"
90
91
92     function webinstall() {
93
94         my_package="\${1:-}"
95         if [ -z "\$my_package" ]; then
96             >&2 echo "Usage: webi <package>@<version> ..."
97             >&2 echo "Example: webi node@lts rg"
98             exit 1
99         fi
100
101         export WEBI_BOOT="\$(mktemp -d -t "\$my_package-bootstrap.\$WEBI_TIMESTAMP.XXXXXXXX")"
102
103         my_installer_url="\$WEBI_HOST/api/installers/\$my_package.sh?formats=\$my_ext"
104         set +e
105         if [ -n "\$WEBI_CURL" ]; then
106             curl -fsSL "\$my_installer_url" -H "User-Agent: curl \$WEBI_UA" \\
107                 -o "\$WEBI_BOOT/\$my_package-bootstrap.sh"
108         else
109             wget -q "\$my_installer_url" --user-agent="wget \$WEBI_UA" \\
110                 -O "\$WEBI_BOOT/\$my_package-bootstrap.sh"
111         fi
112         if ! [ \$? -eq 0 ]; then
113           >&2 echo "error fetching '\$my_installer_url'"
114           exit 1
115         fi
116         set -e
117
118         pushd "\$WEBI_BOOT" 2>&1 > /dev/null
119             bash "\$my_package-bootstrap.sh"
120         popd 2>&1 > /dev/null
121
122         rm -rf "\$WEBI_BOOT"
123
124     }
125
126     show_path_updates() {
127
128         if ! [ -n "\${_WEBI_CHILD}" ]; then
129             if [ -f "\$_webi_tmp/.PATH.env" ]; then
130                 my_paths=\$(cat "\$_webi_tmp/.PATH.env" | sort -u)
131                 if [ -n "\$my_paths" ]; then
132                     echo "IMPORTANT: You must update you PATH to use the installed program(s)"
133                     echo ""
134                     echo "You can either"
135                     echo "A) can CLOSE and REOPEN Terminal or"
136                     echo "B) RUN these exports:"
137                     echo ""
138                     echo "\$my_paths"
139                     echo ""
140                 fi
141                 rm -f "\$_webi_tmp/.PATH.env"
142             fi
143         fi
144
145     }
146
147     for pkgname in "\$@"
148     do
149         webinstall "\$pkgname"
150     done
151
152     show_path_updates
153
154 }
155
156 __webi_main "\$@"
157
158 EOF
159
160     chmod a+x "$HOME/.local/bin/webi"
161
162     if [ -n "${WEBI_PKG:-}" ]; then
163         "$HOME/.local/bin/webi" "${WEBI_PKG}"
164     else
165         echo ""
166         echo "Hmm... no WEBI_PKG was specified. This is probably an error in the script."
167         echo ""
168         echo "Please open an issue with this information: Package '${WEBI_PKG:-}' on '$(uname -s)/$(uname -m)'"
169         echo "    https://github.com/webinstall/packages/issues"
170         echo ""
171     fi
172
173 }
174
175 __install_webi