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