-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.bash
More file actions
299 lines (253 loc) · 9.04 KB
/
install.bash
File metadata and controls
299 lines (253 loc) · 9.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#!/bin/bash
# Determine the OS and architecture
OS=$(uname -s)
ARCH=$(uname -m)
# Function to check if a command is available
command_exists() {
command -v "$1" >/dev/null 2>&1
}
# Flag to track whether Homebrew installation was used
BREW_INSTALLED=false
# Check for required commands
if ! command_exists curl; then
echo "Error: curl is not installed."
exit 1
fi
# On macOS, prefer Homebrew installation if brew is available
if [[ "$OS" == "Darwin" ]] && command_exists brew; then
echo "Homebrew detected on macOS. Attempting to install via brew..."
if brew install shelltime/tap/shelltime; then
BREW_INSTALLED=true
echo "Successfully installed shelltime via Homebrew."
# Rename old manual-install binaries so the system uses the Homebrew version
if [ -f "$HOME/.shelltime/bin/shelltime" ]; then
mv "$HOME/.shelltime/bin/shelltime" "$HOME/.shelltime/bin/shelltime.bak"
echo "Renamed ~/.shelltime/bin/shelltime to shelltime.bak (now using Homebrew version)"
fi
if [ -f "$HOME/.shelltime/bin/shelltime-daemon" ]; then
mv "$HOME/.shelltime/bin/shelltime-daemon" "$HOME/.shelltime/bin/shelltime-daemon.bak"
echo "Renamed ~/.shelltime/bin/shelltime-daemon to shelltime-daemon.bak (now using Homebrew version)"
fi
else
echo "Homebrew installation failed. Falling back to manual installation..."
fi
fi
if [ "$BREW_INSTALLED" = false ]; then
CLI_FILE_NAME="https://github.com/shelltime/cli/releases/latest/download/cli_"
DAEMON_FILE_NAME="${CLI_FILE_NAME}daemon_"
cd /tmp
curr_time_dir="shelltime_install_$(date +"%Y%m%d_%H%M%S")"
mkdir -p "$curr_time_dir"
cd "$curr_time_dir"
get_download_url() {
local baseUrl="$1"
local downloadUrl=""
if [[ "$OS" == "Darwin" ]]; then
baseUrl="${baseUrl}${OS}"
if [[ "$ARCH" == "x86_64" ]]; then
downloadUrl="${baseUrl}_x86_64.zip"
elif [[ "$ARCH" == "arm64" ]]; then
downloadUrl="${baseUrl}_arm64.zip"
else
echo "Unsupported architecture: $ARCH on macOS"
exit 1
fi
if ! command_exists unzip; then
echo "Error: unzip is not installed."
exit 1
fi
elif [[ "$OS" == "Linux" ]]; then
baseUrl="${baseUrl}${OS}"
if [[ "$ARCH" == "x86_64" ]]; then
downloadUrl="${baseUrl}_x86_64.tar.gz"
elif [[ "$ARCH" == "aarch64" ]]; then
downloadUrl="${baseUrl}_arm64.tar.gz"
else
echo "Unsupported architecture: $ARCH on Linux"
exit 1
fi
if ! command_exists tar; then
echo "Error: tar is not installed."
exit 1
fi
elif [[ "$OS" == "MINGW64_NT" ]] || [[ "$OS" == "MSYS_NT" ]] || [[ "$OS" == "CYGWIN_NT" ]]; then
baseUrl="${baseUrl}Windows"
if [[ "$ARCH" == "x86_64" ]]; then
downloadUrl="${baseUrl}_x86_64.zip"
elif [[ "$ARCH" == "aarch64" ]]; then
downloadUrl="${baseUrl}_arm64.zip"
else
echo "Unsupported architecture: $ARCH on Windows"
exit 1
fi
if ! command_exists unzip; then
echo "Error: unzip is not installed."
exit 1
fi
else
echo "Unsupported OS: $OS"
exit 1
fi
echo "$downloadUrl"
}
URL=$(get_download_url "$CLI_FILE_NAME")
# Download the file
FILENAME=$(basename "$URL")
curl -sSLO "$URL"
# Check if the download was successful
if [ ! -f "$FILENAME" ]; then
echo "Error: Failed to download $FILENAME"
exit 1
fi
# Extract the file
if [[ "$FILENAME" == *.zip ]]; then
unzip "$FILENAME" > /dev/null
elif [[ "$FILENAME" == *.tar.gz ]]; then
tar zxvf "$FILENAME" > /dev/null
else
echo "Unsupported file type: $FILENAME"
exit 1
fi
# Check if the shelltime file exists
if [ ! -f "shelltime" ]; then
echo "Error: shelltime binary not found after extraction"
exit 1
fi
# Check if $HOME/.shelltime/bin exists, create if not
if [ ! -d "$HOME/.shelltime/bin" ]; then
mkdir -p "$HOME/.shelltime/bin"
if [ $? -ne 0 ]; then
echo "Error: Failed to create $HOME/.shelltime/bin directory."
exit 1
fi
fi
# Move the binary to the appropriate location
if [[ "$OS" == "Darwin" ]] || [[ "$OS" == "Linux" ]]; then
mv shelltime "$HOME/.shelltime/bin/"
if [ -f "shelltime-daemon" ]; then
mv shelltime-daemon "$HOME/.shelltime/bin/"
fi
# elif [[ "$OS" == "MINGW64_NT" ]] || [[ "$OS" == "MSYS_NT" ]] || [[ "$OS" == "CYGWIN_NT" ]]; then
# mv shelltime /c/Windows/System32/
fi
# Add $HOME/.shelltime/bin to user path
if [[ "$OS" == "Darwin" ]] || [[ "$OS" == "Linux" ]]; then
# For Zsh
if [ -f "$HOME/.zshrc" ]; then
if ! grep -q '$HOME/.shelltime/bin' "$HOME/.zshrc"; then
echo '# Added by shelltime' >> "$HOME/.zshrc"
echo 'export PATH="$HOME/.shelltime/bin:$PATH"' >> "$HOME/.zshrc"
fi
fi
# For Fish
if command_exists fish; then
if [ ! -d "$HOME/.config/fish" ]; then
mkdir -p "$HOME/.config/fish"
fi
if [ ! -f "$HOME/.config/fish/config.fish" ]; then
touch "$HOME/.config/fish/config.fish"
fi
if ! grep -q '$HOME/.shelltime/bin' "$HOME/.config/fish/config.fish"; then
echo '# Added by shelltime' >> "$HOME/.config/fish/config.fish"
echo 'fish_add_path $HOME/.shelltime/bin' >> "$HOME/.config/fish/config.fish"
fi
fi
# For Bash
if [ -f "$HOME/.bashrc" ]; then
if ! grep -q '$HOME/.shelltime/bin' "$HOME/.bashrc"; then
echo '# Added by shelltime' >> "$HOME/.bashrc"
echo 'export PATH="$HOME/.shelltime/bin:$PATH"' >> "$HOME/.bashrc"
fi
fi
fi
# Clean up
cd /tmp
if [ -d "/tmp/$curr_time_dir" ]; then
rm -rf "/tmp/$curr_time_dir"
fi
# HELP WANTED
# I don't know where the `/bin` folder in windows. so i don't know where should the binaries be installed.
# if you know, please let me know.
if [[ "$OS" == "MINGW64_NT" ]] || [[ "$OS" == "MSYS_NT" ]] || [[ "$OS" == "CYGWIN_NT" ]]; then
echo "Note: Please move /tmp/shelltime to your bin folder manually."
echo "If you know where binaries should be installed on Windows, please open an issue: https://github.com/shelltime/cli"
fi
fi # end of manual installation block
# Check if $HOME/.shelltime/daemon exists, create if not
if [ ! -d "$HOME/.shelltime/daemon" ]; then
mkdir -p "$HOME/.shelltime/daemon"
if [ $? -ne 0 ]; then
echo "Warning: Failed to create $HOME/.shelltime/daemon directory. Daemon functionality may be unavailable."
fi
fi
# STEP 2
# insert a preexec and postexec script to user configuration, including `zsh` and `fish`
# Define the path
hooks_path="$HOME/.shelltime/hooks"
# Check if the directory exists
if [ ! -d "$hooks_path" ]; then
mkdir -p "$hooks_path"
if [ $? -ne 0 ]; then
echo "Warning: Failed to create $hooks_path directory. Shell hooks may be unavailable."
fi
fi
# Function to check and delete .bak files
check_and_delete_bak() {
local file="$1"
if [ -f "${hooks_path}/${file}.bak" ]; then
rm "${hooks_path}/${file}.bak"
fi
}
# Function to check, rename, and download files
process_file() {
local file="$1"
local url="$2"
# Check if the file exists and rename it
if [ -f "${hooks_path}/${file}" ]; then
mv "${hooks_path}/${file}" "${hooks_path}/${file}.bak"
fi
# Download the new file
curl -sSL "${url}" -o "${hooks_path}/${file}"
}
# Function to add source line to config file if not already present
add_source_to_config() {
local config_file="$1"
local source_file="$2"
local source_line="source ${source_file}"
if ! grep -qF "${source_line}" "${config_file}"; then
echo "${source_line}" >> "${config_file}"
fi
}
# Ensure hooks_path exists
mkdir -p "$hooks_path"
# Check and delete .bak files
check_and_delete_bak "zsh.zsh"
check_and_delete_bak "fish.fish"
# Process zsh.zsh
process_file "zsh.zsh" "https://raw.githubusercontent.com/shelltime/installation/master/hooks/zsh.zsh"
# Process fish.fish
process_file "fish.fish" "https://raw.githubusercontent.com/shelltime/installation/master/hooks/fish.fish"
# Process bash.bash
process_file "bash-preexec.sh" "https://raw.githubusercontent.com/rcaloras/bash-preexec/master/bash-preexec.sh"
process_file "bash.bash" "https://raw.githubusercontent.com/shelltime/installation/master/hooks/bash.bash"
# Add source lines to config files
if [ -f "$HOME/.zshrc" ]; then
add_source_to_config "$HOME/.zshrc" "${hooks_path}/zsh.zsh"
fi
if [ -f "$HOME/.config/fish/config.fish" ]; then
add_source_to_config "$HOME/.config/fish/config.fish" "${hooks_path}/fish.fish"
fi
if [ -f "$HOME/.bashrc" ]; then
add_source_to_config "$HOME/.bashrc" "${hooks_path}/bash.bash"
fi
# Reinstall daemon if shelltime is available
if command_exists shelltime; then
shelltime daemon reinstall > /dev/null 2>&1
fi
echo ""
echo "Installation complete!"
echo ""
echo "Next steps:"
echo " 1. Reload your shell: source ~/.zshrc (or ~/.bashrc / ~/.config/fish/config.fish)"
echo " 2. Run: shelltime init"
echo ""