forked from curusarn/resh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.sh
executable file
·196 lines (162 loc) · 6.39 KB
/
install.sh
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
#!/usr/bin/env bash
set -euo pipefail
# TODO: There is a lot of hardcoded stuff here (paths mostly)
# TODO: Split this into installation and setup because we want to suport package manager installation eventually
# TODO: "installation" should stay here and be simple, "setup" should be moved behind "reshctl setup"
echo
echo "Checking your system ..."
printf '\e[31;1m' # red color on
cleanup() {
printf '\e[0m' # reset
exit
}
trap cleanup EXIT INT TERM
# /usr/bin/zsh -> zsh
login_shell=$(echo "$SHELL" | rev | cut -d'/' -f1 | rev)
if [ "$login_shell" != bash ] && [ "$login_shell" != zsh ]; then
echo "* UNSUPPORTED login shell: $login_shell"
echo " -> RESH supports zsh and bash"
echo
if [ -z "${RESH_INSTALL_IGNORE_LOGIN_SHELL-}" ]; then
echo 'EXITING!'
echo ' -> You can skip this check with `export RESH_INSTALL_IGNORE_LOGIN_SHELL=1`'
exit 1
fi
fi
# TODO: Explicitly ask users if they want to enable RESH in shells
# Only offer shells with supported versions
# E.g. Enable RESH in: Zsh (your login shell), Bash, Both shells
bash_version=$(bash -c 'echo ${BASH_VERSION}')
bash_version_major=$(bash -c 'echo ${BASH_VERSINFO[0]}')
bash_version_minor=$(bash -c 'echo ${BASH_VERSINFO[1]}')
bash_ok=1
if [ "$bash_version_major" -le 3 ]; then
bash_ok=0
elif [ "$bash_version_major" -eq 4 ] && [ "$bash_version_minor" -lt 3 ]; then
bash_ok=0
fi
if [ "$bash_ok" != 1 ]; then
echo "* UNSUPPORTED bash version: $bash_version"
echo " -> Update to bash 4.3+ if you want to use RESH in bash"
echo
fi
zsh_ok=1
if ! zsh --version >/dev/null 2>&1; then
echo "* Zsh not installed"
zsh_ok=0
else
zsh_version=$(zsh -c 'echo ${ZSH_VERSION}')
zsh_version_major=$(echo "$zsh_version" | cut -d'.' -f1)
if [ "$zsh_version_major" -lt 5 ]; then
echo "* UNSUPPORTED zsh version: $zsh_version"
echo " -> Updatie to zsh 5.0+ if you want to use RESH in zsh"
echo
zsh_ok=0
fi
fi
if [ "$bash_ok" != 1 ] && [ "$zsh_ok" != 1 ]; then
echo "* You have no shell that is supported by RESH!"
echo " -> Please install/update zsh or bash and run this installation again"
echo
if [ -z "${RESH_INSTALL_IGNORE_NO_SHELL-}" ]; then
echo 'EXITING!'
echo ' -> You can prevent this check by setting `export RESH_INSTALL_IGNORE_NO_SHELL=1`'
echo
exit 1
fi
fi
printf '\e[0m' # reset
# echo "Continue with installation? (Any key to CONTINUE / Ctrl+C to ABORT)"
# # shellcheck disable=2034
# read -r x
if [ -z "${__RESH_VERSION-}" ]; then
# First installation
# Stop the daemon anyway just to be sure
# But don't output anything
./scripts/resh-daemon-stop.sh -q ||:
else
./scripts/resh-daemon-stop.sh ||:
fi
echo "Installing ..."
# Crete dirs first to get rid of edge-cases
# If we fail we don't roll back - directories are harmless
mkdir_if_not_exists() {
if [ ! -d "$1" ]; then
mkdir "$1"
fi
}
mkdir_if_not_exists ~/.resh
mkdir_if_not_exists ~/.resh/bin
mkdir_if_not_exists ~/.config
# Run setup and update tasks
./bin/resh-install-utils setup-device
# migrate-all updates format of config and history
# migrate-all restores original config and history on error
# There is no need to roll back anything else because we haven't replaced
# anything in the previous installation.
./bin/resh-install-utils migrate-all
# Copy files
# echo "Copying files ..."
cp -f submodules/bash-preexec/bash-preexec.sh ~/.bash-preexec.sh
cp -f submodules/bash-zsh-compat-widgets/bindfunc.sh ~/.resh/bindfunc.sh
cp -f scripts/shellrc.sh ~/.resh/shellrc
cp -f scripts/resh-daemon-start.sh ~/.resh/bin/resh-daemon-start
cp -f scripts/resh-daemon-stop.sh ~/.resh/bin/resh-daemon-stop
cp -f scripts/resh-daemon-restart.sh ~/.resh/bin/resh-daemon-restart
cp -f scripts/hooks.sh ~/.resh/
cp -f scripts/rawinstall.sh ~/.resh/
# echo "Copying more files ..."
# Copy all go executables. We don't really need to omit install-utils from the bin directory
cp -f bin/resh-* ~/.resh/bin/
# Rename reshctl
mv ~/.resh/bin/resh-control ~/.resh/bin/reshctl
echo "Handling shell files ..."
# Only add shell directives into bash if it passed version checks
if [ "$bash_ok" = 1 ]; then
if [ ! -f ~/.bashrc ]; then
touch ~/.bashrc
fi
# Adding resh shellrc to .bashrc ...
grep -q '[[ -f ~/.resh/shellrc ]] && source ~/.resh/shellrc' ~/.bashrc ||\
echo -e '\n[[ -f ~/.resh/shellrc ]] && source ~/.resh/shellrc # this line was added by RESH' >> ~/.bashrc
# Adding bash-preexec to .bashrc ...
grep -q '[[ -f ~/.bash-preexec.sh ]] && source ~/.bash-preexec.sh' ~/.bashrc ||\
echo -e '\n[[ -f ~/.bash-preexec.sh ]] && source ~/.bash-preexec.sh # this line was added by RESH' >> ~/.bashrc
fi
# Only add shell directives into zsh if it passed version checks
if [ "$zsh_ok" = 1 ]; then
# Adding resh shellrc to .zshrc ...
if [ -f ~/.zshrc ]; then
grep -q '[ -f ~/.resh/shellrc ] && source ~/.resh/shellrc' ~/.zshrc ||\
echo -e '\n[ -f ~/.resh/shellrc ] && source ~/.resh/shellrc # this line was added by RESH' >> ~/.zshrc
fi
fi
~/.resh/bin/resh-daemon-start
# bright green
high='\e[1m'
reset='\e[0m'
printf '
Installation finished successfully.
QUICK START
\e[32;1m Press CTRL+R to launch RESH SEARCH \e[0m
'
if [ -z "${__RESH_VERSION-}" ]; then
printf 'You will need to restart your terminal first!\n'
fi
printf '
Full-text search your shell history.
Relevant results are displayed first based on current directory, git repo, and exit status.
RESH will locally record and save shell history with context (directory, time, exit status, ...)
Start using RESH right away because bash and zsh history are also searched.
Update RESH by running: reshctl update
Thank you for using RESH!
'
# Show banner if RESH is not loaded in the terminal
if [ -z "${__RESH_VERSION-}" ]; then printf '
┌────────────────────────────────────────────────────────────┐
│ │
│ Finish the installation by RESTARTING this terminal! │
│ │
└────────────────────────────────────────────────────────────┘
'
fi