|
1 |
| -{ # Prevent execution if this script was only partially downloaded |
2 |
| - |
3 |
| -oops() { |
4 |
| - echo "$0:" "$@" >&2 |
5 |
| - exit 1 |
6 |
| -} |
7 |
| - |
8 |
| -readonly ERR_UNSUPPORTED_OS=1 |
9 |
| -readonly ERR_INVALID_USAGE=2 |
10 |
| -readonly tmpDir="$(mktemp -d -t pi-tarball-unpack.XXXXXXXXXX || \ |
11 |
| - oops "Can't create temporary directory for downloading the Pi tarball")" |
12 |
| - |
13 |
| -require_util() { |
14 |
| - command -v "$1" > /dev/null 2>&1 || |
15 |
| - oops "you do not have '$1' installed, which I need to $2" |
16 |
| -} |
17 |
| - |
18 |
| -die() { |
19 |
| - local message="$1" |
20 |
| - local -i exit_code="${2:-1}" |
21 |
| - |
22 |
| - printf "%sError $exit_code: $message%s\n" "${RED}" "${NORMAL}" >&2 |
23 |
| - exit $exit_code |
24 |
| -} |
25 |
| - |
26 |
| -# Return the latest version URL in the form: https://github.com/hernanmd/pi/releases/tag/0.4.8 |
27 |
| -get_latest_version() { |
28 |
| - basename "$(curl -s -o /dev/null -I -w "%{redirect_url}" https://github.com/hernanmd/pi/releases/latest)" |
29 |
| -} |
30 |
| - |
31 |
| -cleanup() { |
32 |
| - rm -rf "$tmpDir" |
33 |
| -} |
34 |
| - |
35 |
| -trap cleanup EXIT INT QUIT TERM |
36 |
| - |
37 |
| -do_install() { |
38 |
| - require_util curl "download the binary tarball" |
39 |
| - require_util tar "unpack the binary tarball" |
40 |
| - local latest="$(get_latest_version)" |
41 |
| - local url="https://github.com/hernanmd/pi/archive/$latest.tar.gz" |
42 |
| - local tarball="$tmpDir/pi.tar.gz" |
43 |
| - curl -sSL "$url" -o "$tarball" || oops "failed to download '$url'" |
44 |
| - local unpack="$HOME/.pi" |
45 |
| - # Create a user local directory for pi |
46 |
| - mkdir -pv "$unpack" |
47 |
| - # Check previous installation and remove it to prevent Directory not empty on rename " |
48 |
| - [ -e "$unpack/pi-$latest" ] && rm -rf "$unpack/pi-$latest" |
49 |
| - # Uncompress, untar and move to a non-versioned persistent user directory |
50 |
| - ( cd "$unpack" && tar zxvf "$tarball" && mv -fv "pi-$latest" pi ) || oops "failed to unpack '$url'" |
51 |
| - # Check if main script was uncompressed succesfully |
52 |
| - script=$(echo "$unpack"/pi/bin/pi) |
53 |
| - [ -e "$script" ] || oops "main script is missing from the tarball!" |
54 |
| -} |
55 |
| - |
56 |
| -check_install() { |
57 |
| - [[ "$(type -t pi)" == "file" ]] |
58 |
| -} |
59 |
| - |
60 |
| -check_noroot() { |
61 |
| - [[ "$(whoami)" != "root" ]] || die "Don't install with sudo or as root" |
62 |
| -} |
63 |
| - |
64 |
| -install_pi() { |
65 |
| - # Use colors, but only if connected to a terminal, and that terminal supports them. |
66 |
| - if command -v tput >/dev/null 2>&1; then |
67 |
| - ncolors=$(tput colors) |
68 |
| - fi |
69 |
| - if [ -t 1 ] && [ -n "$ncolors" ] && [ "$ncolors" -ge 8 ]; then |
70 |
| - RED="$(tput setaf 1)" |
71 |
| - GREEN="$(tput setaf 2)" |
72 |
| - YELLOW="$(tput setaf 3)" |
73 |
| - BLUE="$(tput setaf 4)" |
74 |
| - BOLD="$(tput bold)" |
75 |
| - NORMAL="$(tput sgr0)" |
| 1 | + { # Prevent execution if this script was only partially downloaded |
| 2 | + |
| 3 | + oops() { |
| 4 | + echo "$0:" "$@" >&2 |
| 5 | + exit 1 |
| 6 | + } |
| 7 | + |
| 8 | + readonly ERR_INVALID_USAGE=2 |
| 9 | + readonly tmpDir="$(mktemp -d -t pi-tarball-unpack.XXXXXXXXXX || \ |
| 10 | + oops "Can't create temporary directory for downloading the Pi tarball")" |
| 11 | + |
| 12 | + require_util() { |
| 13 | + command -v "$1" > /dev/null 2>&1 || |
| 14 | + oops "you do not have '$1' installed, which I need to $2" |
| 15 | + } |
| 16 | + |
| 17 | + die() { |
| 18 | + local message="$1" |
| 19 | + local -i exit_code="${2:-1}" |
| 20 | + |
| 21 | + printf "%sError $exit_code: $message%s\n" "${RED}" "${NORMAL}" >&2 |
| 22 | + exit $exit_code |
| 23 | + } |
| 24 | + |
| 25 | + # Return the latest version URL in the form: https://github.com/hernanmd/pi/releases/tag/0.4.8 |
| 26 | + get_latest_version() { |
| 27 | + basename "$(curl -s -o /dev/null -I -w "%{redirect_url}" https://github.com/hernanmd/pi/releases/latest)" |
| 28 | + } |
| 29 | + |
| 30 | + cleanup() { |
| 31 | + rm -rf "$tmpDir" |
| 32 | + } |
| 33 | + |
| 34 | + trap cleanup EXIT INT QUIT TERM |
| 35 | + |
| 36 | + do_install() { |
| 37 | + require_util curl "download the binary tarball" |
| 38 | + require_util tar "unpack the binary tarball" |
| 39 | + local latest |
| 40 | + local url |
| 41 | + local tarball |
| 42 | + local unpack |
| 43 | + |
| 44 | + latest="$(get_latest_version)" |
| 45 | + url="https://github.com/hernanmd/pi/archive/$latest.tar.gz" |
| 46 | + tarball="$tmpDir/pi.tar.gz" |
| 47 | + curl -sSL "$url" -o "$tarball" || oops "failed to download '$url'" |
| 48 | + unpack="$HOME/.pi" |
| 49 | + # Create a user local directory for pi |
| 50 | + mkdir -pv "$unpack" |
| 51 | + # Check previous installation and remove it to prevent Directory not empty on rename " |
| 52 | + printf "Checking previous installation on %s ..." "$unpack" |
| 53 | + [ -e "$unpack/pi-$latest" ] && { printf "\nRemoving previous installation..."; rm -rf "$unpack/pi-$latest"; } |
| 54 | + printf "done\n" |
| 55 | + # Uncompress, untar and move to a non-versioned persistent user directory |
| 56 | + ( cd "$unpack" && tar zxvf "$tarball" && mv -fv "pi-$latest" pi ) || oops "failed to unpack '$url'" |
| 57 | + # Check if main script was uncompressed succesfully |
| 58 | + script=$(echo "$unpack"/pi/bin/pi) |
| 59 | + [ -e "$script" ] || oops "main script is missing from the tarball!" |
| 60 | + } |
| 61 | + |
| 62 | + check_install() { |
| 63 | + [[ "$(type -t pi)" == "file" ]] |
| 64 | + } |
| 65 | + |
| 66 | + check_noroot() { |
| 67 | + [[ "$(whoami)" != "root" ]] || die "Don't install with sudo or as root" |
| 68 | + } |
| 69 | + |
| 70 | + install_pi() { |
| 71 | + local os |
| 72 | + |
| 73 | + # Use colors, but only if connected to a terminal, and that terminal supports them. |
| 74 | + if command -v tput >/dev/null 2>&1; then |
| 75 | + ncolors=$(tput colors) |
| 76 | + fi |
| 77 | + if [ -t 1 ] && [ -n "$ncolors" ] && [ "$ncolors" -ge 8 ]; then |
| 78 | + RED="$(tput setaf 1)" |
| 79 | + GREEN="$(tput setaf 2)" |
| 80 | + YELLOW="$(tput setaf 3)" |
| 81 | + BLUE="$(tput setaf 4)" |
| 82 | + BOLD="$(tput bold)" |
| 83 | + NORMAL="$(tput sgr0)" |
| 84 | + else |
| 85 | + RED="" |
| 86 | + GREEN="" |
| 87 | + YELLOW="" |
| 88 | + BLUE="" |
| 89 | + BOLD="" |
| 90 | + NORMAL="" |
| 91 | + fi |
| 92 | + |
| 93 | + # Only enable exit-on-error after the non-critical colorization stuff, |
| 94 | + # which may fail on systems lacking tput or terminfo |
| 95 | + set -e |
| 96 | + |
| 97 | + printf "%sInstalling pi...%s\n" "${YELLOW}" "${NORMAL}" |
| 98 | + os="$(uname)" |
| 99 | + case "$os" in |
| 100 | + (Linux | Darwin | MINGW*) |
| 101 | + check_noroot |
| 102 | + |
| 103 | + local -i upgrade=0 |
| 104 | + check_install && upgrade=1 |
| 105 | + |
| 106 | + do_install |
| 107 | + |
| 108 | + if ! check_install; then |
| 109 | + cat <<-EOF |
| 110 | + ${YELLOW} |
| 111 | + In order to make pi work, you need to add the following |
| 112 | + to your .bash_profile/.profile file: |
| 113 | +
|
| 114 | + export PATH="\$HOME/.pi/pi/bin:\$PATH |
| 115 | + ${NORMAL}" |
| 116 | + EOF |
| 117 | + fi |
| 118 | + |
| 119 | + local version="$(get_latest_version)" |
| 120 | + if [[ $upgrade -eq 1 ]]; then |
| 121 | + show_banner "$version" "upgraded" |
| 122 | + else |
| 123 | + show_banner "$version" "installed" |
| 124 | + fi |
| 125 | + |
| 126 | + ;; |
| 127 | + (*) |
| 128 | + die "Unsupported OS: $os" |
| 129 | + ;; |
| 130 | + esac |
| 131 | + } |
| 132 | + |
| 133 | + show_banner () { |
| 134 | + printf "${GREEN}" |
| 135 | + echo ' # ' |
| 136 | + echo ' ### ' |
| 137 | + echo ' # ' |
| 138 | + echo ' ' |
| 139 | + echo ' /### ### ' |
| 140 | + echo ' / ### / ### ' |
| 141 | + echo ' / ###/ ## ' |
| 142 | + echo '## ## ## ' |
| 143 | + echo '## ## ## ' |
| 144 | + echo '## ## ## ' |
| 145 | + echo '## ## ## ' |
| 146 | + echo '## ## ## ' |
| 147 | + echo '####### ### / ' |
| 148 | + echo '###### ##/ ' |
| 149 | + echo '## ' |
| 150 | + echo '## ' |
| 151 | + echo '## ' |
| 152 | + echo '## ' |
| 153 | + echo ' ....is now installed!' |
| 154 | + echo 'Please look over pi help to access options.' |
| 155 | + echo '' |
| 156 | + echo 'p.s. If you like this work star it at https://github.com/hernanmd/pi' |
| 157 | + echo '' |
| 158 | + printf "${NORMAL}" |
| 159 | + } |
| 160 | + |
| 161 | + show_usage() { |
| 162 | + cat <<-EOF |
| 163 | + Usage: $(basename $0) COMMAND |
| 164 | +
|
| 165 | + Commands: |
| 166 | + -h|--help Shows usage |
| 167 | + -u|--upgrade|--update Upgrades pi to the latest version |
| 168 | + -v|--version Shows version |
| 169 | + EOF |
| 170 | + } |
| 171 | + |
| 172 | + usage() { |
| 173 | + show_usage |
| 174 | + exit $ERR_INVALID_USAGE |
| 175 | + } |
| 176 | + |
| 177 | + main() { |
| 178 | + [[ $# -gt 0 ]] || usage |
| 179 | + |
| 180 | + local command="${1:-}" |
| 181 | + case "$command" in |
| 182 | + ("-h" | "--help") |
| 183 | + shift |
| 184 | + show_usage |
| 185 | + ;; |
| 186 | + ("-u" | "--upgrade" | "--update") |
| 187 | + shift |
| 188 | + install_pi |
| 189 | + ;; |
| 190 | + (*) |
| 191 | + shift |
| 192 | + show_usage |
| 193 | + ;; |
| 194 | + esac |
| 195 | + } |
| 196 | + |
| 197 | + if [[ -z "${BASH_SOURCE[0]:-}" ]]; then |
| 198 | + install_pi |
76 | 199 | else
|
77 |
| - RED="" |
78 |
| - GREEN="" |
79 |
| - YELLOW="" |
80 |
| - BLUE="" |
81 |
| - BOLD="" |
82 |
| - NORMAL="" |
| 200 | + main "$@" |
83 | 201 | fi
|
84 | 202 |
|
85 |
| - # Only enable exit-on-error after the non-critical colorization stuff, |
86 |
| - # which may fail on systems lacking tput or terminfo |
87 |
| - set -e |
88 |
| - |
89 |
| - printf "%sInstalling pi...%s\n" "${YELLOW}" "${NORMAL}" |
90 |
| - local os="$(uname)" |
91 |
| - case "$os" in |
92 |
| - (Linux | Darwin | MINGW*) |
93 |
| - check_noroot |
94 |
| - |
95 |
| - local -i upgrade=0 |
96 |
| - check_install && upgrade=1 |
97 |
| - |
98 |
| - do_install |
99 |
| - |
100 |
| - if ! check_install; then |
101 |
| - cat <<-EOF |
102 |
| - ${YELLOW} |
103 |
| - In order to make pi work, you need to add the following |
104 |
| - to your .bash_profile/.profile file: |
105 |
| -
|
106 |
| - export PATH="\$HOME/.pi/pi/bin:\$PATH |
107 |
| - ${NORMAL}" |
108 |
| - EOF |
109 |
| - fi |
110 |
| - |
111 |
| - local version="$(get_latest_version)" |
112 |
| - if [[ $upgrade -eq 1 ]]; then |
113 |
| - show_banner "$version" "upgraded" |
114 |
| - else |
115 |
| - show_banner "$version" "installed" |
116 |
| - fi |
117 |
| - |
118 |
| - ;; |
119 |
| - (*) |
120 |
| - die "Unsupported OS: $os" |
121 |
| - ;; |
122 |
| - esac |
123 |
| -} |
124 |
| - |
125 |
| -show_banner () { |
126 |
| - printf "${GREEN}" |
127 |
| - echo ' # ' |
128 |
| - echo ' ### ' |
129 |
| - echo ' # ' |
130 |
| - echo ' ' |
131 |
| - echo ' /### ### ' |
132 |
| - echo ' / ### / ### ' |
133 |
| - echo ' / ###/ ## ' |
134 |
| - echo '## ## ## ' |
135 |
| - echo '## ## ## ' |
136 |
| - echo '## ## ## ' |
137 |
| - echo '## ## ## ' |
138 |
| - echo '## ## ## ' |
139 |
| - echo '####### ### / ' |
140 |
| - echo '###### ##/ ' |
141 |
| - echo '## ' |
142 |
| - echo '## ' |
143 |
| - echo '## ' |
144 |
| - echo '## ' |
145 |
| - echo ' ....is now installed!' |
146 |
| - echo 'Please look over pi help to access options.' |
147 |
| - echo '' |
148 |
| - echo 'p.s. If you like this work star it at https://github.com/hernanmd/pi' |
149 |
| - echo '' |
150 |
| - printf "${NORMAL}" |
151 |
| -} |
152 |
| - |
153 |
| -show_usage() { |
154 |
| - cat <<-EOF |
155 |
| - Usage: $(basename $0) COMMAND |
156 |
| -
|
157 |
| - Commands: |
158 |
| - -h|--help Shows usage |
159 |
| - -u|--upgrade|--update Upgrades pi to the latest version |
160 |
| - -v|--version Shows version |
161 |
| - EOF |
162 |
| -} |
163 |
| - |
164 |
| -usage() { |
165 |
| - show_usage |
166 |
| - exit $ERR_INVALID_USAGE |
167 |
| -} |
168 |
| - |
169 |
| -main() { |
170 |
| - [[ $# -gt 0 ]] || usage |
171 |
| - |
172 |
| - local command="${1:-}" |
173 |
| - case "$command" in |
174 |
| - ("-h" | "--help") |
175 |
| - shift |
176 |
| - show_usage |
177 |
| - ;; |
178 |
| - ("-u" | "--upgrade" | "--update") |
179 |
| - shift |
180 |
| - install_pi |
181 |
| - ;; |
182 |
| - (*) |
183 |
| - shift |
184 |
| - show_usage |
185 |
| - ;; |
186 |
| - esac |
187 |
| -} |
188 |
| - |
189 |
| -if [[ -z "${BASH_SOURCE[0]:-}" ]]; then |
190 |
| - install_pi |
191 |
| -else |
192 |
| - main "$@" |
193 |
| -fi |
194 |
| - |
195 |
| -} # End of wrapping |
| 203 | + } # End of wrapping |
0 commit comments