-
Notifications
You must be signed in to change notification settings - Fork 4
/
install.sh
executable file
·207 lines (176 loc) · 7.19 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
197
198
199
200
201
202
203
204
205
206
207
getVersion () {
local ver
ver=$(grep version ~/.welcome/welcome.sh 2> /dev/null | sed 's/.*=//')
if [ -z "$ver" ]; then
echo -e "\bUnknown"
return
fi
echo "$ver"
}
# Universal read command for bash and zsh
univRead () {
echo -en "$1"
echo -en "\n[\e[36mY/n\e[0m]"
if [[ "$environment" = "bash" ]]; then
read -p " " -r
elif [[ "$environment" = "zsh" ]]; then
read -q "REPLY? " -r
fi
echo
}
uninstall () {
# Check if the user actually wishes to uninstall the script
univRead "Do you want to \e[31muninstall \e[35mwelcome.sh\e[0m?"
if ! [[ $REPLY =~ ^[Yy]$ ]]; then
tput rc el ed
echo -e "\e[31mUninstall cancelled.\e[0m"
exit 0
fi
tput rc el ed
echo "Goodbye. Uninstalling..."
tput sc
rm ~/.welcome/welcome.sh 2> /dev/null
rm ~/.welcome/install.sh 2> /dev/null
rm ~/.welcome/config.cfg 2> /dev/null
rm ~/.welcome/config_old.cfg 2> /dev/null
rm -r ~/.welcome
#remove all lines that match the string
lines=$(grep -sn 'bash ~/.welcome/welcome.sh' "$bashrc" | sed -e 's/:.*//g' && grep -sn 'bash /home/$USER/.welcome/welcome.sh' "$bashrc" | sed -e 's/:.*//g')
lines=$(printf '%s\n' "$lines" | sed '1!G;h;$!d' | sed ':a;N;$!ba;s/\n/ /g')
for i in $lines; do
sed "${i}d" "$bashrc" > file.tmp && mv file.tmp "$bashrc"
done
lines=$(grep -sn 'zsh ~/.welcome/welcome.sh' "$zshrc" | sed -e 's/:.*//g' && grep -sn 'zsh /home/$USER/.welcome/welcome.sh' "$zshrc" | sed -e 's/:.*//g')
lines=$(printf '%s\n' "$lines" | sed '1!G;h;$!d' | sed ':a;N;$!ba;s/\n/ /g')
for i in $lines; do
sed "${i}d" "$zshrc" > file.tmp && mv file.tmp "$zshrc"
done
tput rc && tput el && tput ed
echo -e "\e[36mUninstalled! \e[0m"
}
update () {
tput rc el ed
echo "Updating..."
tput sc
mkdir -p ~/.welcome
rm ~/.welcome/welcome.sh 2> /dev/null
rm ~/.welcome/install.sh 2> /dev/null
curl -SL https://github.com/G2-Games/welcome.sh/releases/download/v"${version}"/welcome.sh --output ~/.welcome/welcome.sh
curl -SL https://raw.githubusercontent.com/G2-Games/welcome.sh/main/install.sh --output ~/.welcome/install.sh
if [[ $vernum -ge 100 ]] && [[ $overcfg -gt 0 ]]; then
echo "Backing up: config.cfg >> config_old.cfg"
mv ~/.welcome/config.cfg ~/.welcome/config_old.cfg
curl -SL https://github.com/G2-Games/welcome.sh/releases/download/v"${version}"/config.cfg --output ~/.welcome/config.cfg
fi
# Check for older versions and replace bashrc lines
lines=$(grep -sn 'bash ~/.welcome/welcome.sh' "$bashrc" | sed -e 's/:.*//g' && grep -sn 'bash /home/$USER/.welcome/welcome.sh' "$bashrc" | sed -e 's/:.*//g') # Find target line number
lines=$(printf '%s\n' "$lines" | sed '1!G;h;$!d' | sed ':a;N;$!ba;s/\n/ /g') # Format the line number properly (macos doesn't have Cut)
for i in $lines; do
sed "${i}d" "$bashrc" > file.tmp && mv file.tmp "$bashrc"
done
echo 'bash ~/.welcome/welcome.sh' >> "$bashrc"
lines=$(grep -sn 'zsh ~/.welcome/welcome.sh' "$zshrc" | sed -e 's/:.*//g' && grep -sn 'zsh /home/$USER/.welcome/welcome.sh' "$zshrc" | sed -e 's/:.*//g')
lines=$(printf '%s\n' "$lines" | sed '1!G;h;$!d' | sed ':a;N;$!ba;s/\n/ /g')
for i in $lines; do
sed "${i}d" "$zshrc" > file.tmp && mv file.tmp "$zshrc"
done
echo 'zsh ~/.welcome/welcome.sh' >> "$zshrc"
tput rc el ed
echo -e "\e[32mUpdated to v$version! \e[0m"
}
tput sc
if [[ -z "$1" ]] && [[ $1 != "auto" ]]; then
echo "Checking for latest release..."
fi
if ! [[ $(command -v curl) ]]; then
exit 1
fi
latestver=$(curl -Ls https://github.com/G2-Games/welcome.sh/releases/latest/download/welcome.sh | grep version | cut -d= -f2)
oldver=$(grep version ~/.welcome/welcome.sh 2> /dev/null | cut -d= -f2 | sed 's/[.][.]*//g') && if [[ -z "$oldver" ]]; then oldver=0; fi
version="${1:-$latestver}"
if [[ $1 == "auto" ]] && [[ ${latestver//./} -le $oldver ]]; then
exit 0
elif [[ $1 == "auto" ]]; then
version=$latestver
fi
vernum=${version//./}
bashrc=~/.bashrc
zshrc=~/.zshrc
originaldir=$PWD
environment=$(ps -o args= -p $$ | grep -Em 1 -o '\w{0,5}sh' | head -1)
# Check if the environment is bash or zsh
if [[ "$environment" != "bash" ]] && [[ "$environment" != "zsh" ]]; then
tput rc
printf "\e[31;5mERROR:\e[0m \e[31;3mThis script can only be installed in Bash or Zsh.\e[0m\n"
printf "\e[32mYou appear to be using %s, which is not a valid shell for\n\
this script!\e[0m Please use \e[3mBash \e[0mor \e[3mZsh.\e[0m\n" "${environment}"
exit 1
fi
# Check if already installed
if ! grep -qs 'bash ~/.welcome/welcome.sh' $bashrc &&
! grep -qs 'zsh ~/.welcome/welcome.sh' $zshrc &&
! grep -qs 'bash /home/$USER/.welcome/welcome.sh' $bashrc &&
! grep -qs 'zsh /home/$USER/.welcome/welcome.sh' $zshrc;
then
#==== Execute if first time installing...====#
tput rc
echo "Welcome! Installing v$version in $environment..."
tput sc
cd ~/ || exit 1
mkdir -p ~/.welcome
curl -SL https://github.com/G2-Games/welcome.sh/releases/download/v"${version}"/welcome.sh --output ~/.welcome/welcome.sh
curl -SL https://raw.githubusercontent.com/G2-Games/welcome.sh/main/install.sh --output ~/.welcome/install.sh
if [[ $vernum -ge 100 ]]; then
curl -SL https://github.com/G2-Games/welcome.sh/releases/download/v"${version}"/config.cfg --output ~/.welcome/config.cfg
fi
chmod +x ~/.welcome/welcome.sh
if [[ "$environment" = "bash" ]]; then
echo "Installing to bashrc"
echo 'bash ~/.welcome/welcome.sh' >> $bashrc
elif [[ "$environment" = "zsh" ]]; then
echo "Installing to zshrc"
echo 'zsh ~/.welcome/welcome.sh' >> $zshrc
fi
cd "$originaldir" || exit 1
tput rc el ed
echo -e "\e[36mInstalled! \e[0m"
else
#==== Execute if already installed...====#
tput rc sc el ed
mkdir -p ~/.welcome
if ! [[ $1 == "auto" ]]; then
echo -e "\e[35mwelcome.sh\e[0m already installed!"
else
echo -e "\e[32mUpdate available for \e[35mwelcome.sh!\e[0m"
fi
if [[ $vernum -gt $oldver ]]; then
# Pull the new configuration version from github
newCfgVer=$(curl -Ls https://github.com/G2-Games/welcome.sh/releases/download/v"${version}"/config.cfg | grep version | cut -d= -f2)
univRead "Do you want to \e[36mupdate \e[35mwelcome.sh\e[0m? (\e[36mv$(getVersion)\e[0m => \e[32mv$version\e[0m)"
if ! [[ $REPLY =~ ^[Yy]$ ]]; then
tput rc sc el ed
echo -e "\e[31mUpdate cancelled.\e[0m"
update=0
else
update=1
fi
if [[ $newCfgVer -gt $(grep version ~/.welcome/config.cfg 2> /dev/null | cut -d= -f2) ]]; then
univRead "Newer \e[36mconfig\e[0m version available. Do you want to \e[31moverwrite\e[0m your \e[36mconfig\e[0m? \nA backup will be created in the \e[36m.welcome\e[0m folder."
if [[ $REPLY =~ ^[Yy]$ ]]; then
overcfg=1
fi
elif [[ $oldver -lt 100 ]]; then
overcfg=1
else
overcfg=0
fi
fi
if [[ update -gt 0 ]]; then
update
exit 0
fi
if ! [[ $1 == "auto" ]]; then
uninstall
exit 0
fi
fi