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