-
Notifications
You must be signed in to change notification settings - Fork 1
/
es-pixel-theme-extras.sh
executable file
·307 lines (243 loc) · 8.53 KB
/
es-pixel-theme-extras.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
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
300
301
302
303
304
305
306
307
#!/usr/bin/env bash
# install-es-pixel-theme-extras.sh
# Globals #############################################
user="$SUDO_USER"
[[ -z "$user" ]] && user="$(id -un)"
home="$(find /home -type d -name RetroPie -print -quit 2> /dev/null)"
home="${home%/RetroPie}"
readonly RP_DIR="$home/RetroPie"
readonly RP_CONFIG_DIR="/opt/retropie/configs"
readonly ES_THEMES_DIR="/etc/emulationstation/themes"
readonly SPLASH_LIST="/etc/splashscreen.list"
readonly TMP=".tmp"
# Variables ############################################
readonly RP_ICONS_DIR="$RP_DIR/retropiemenu/icons"
readonly RP_BACKUP_ICONS_DIR="$RP_ICONS_DIR/rp-icons-backup"
# Defaults
readonly DEFAULT_THEME="pixel"
# Global variables
THEME="$DEFAULT_THEME"
readonly THEME_ICONS_DIR="$ES_THEMES_DIR/$THEME/retropie/icons"
readonly GIT_THEME_LAUNCHING_IMAGES="https://github.com/hiulit/es-runcommand-splash.git"
readonly TMP_LAUNCHING_IMAGES_DIR="$TMP/$THEME-launching-images"
# Functions ############################################
function is_sudo() {
[[ "$(id -u)" -eq 0 ]]
}
function usage() {
echo "USAGE: 'sudo $0 --install OR --uninstall'"
exit 0
}
function check_rp_icons_dir() {
[[ ! -d "$RP_ICONS_DIR" ]] && mkdir -p "$RP_ICONS_DIR" && chown -R "$user":"$user" "$RP_ICONS_DIR"
}
function check_theme_icons_dir() {
if [[ ! -d "$THEME_ICONS_DIR" ]]; then
echo "ERROR: '$THEME' theme doesn't have any icons." >&2
return 1
else
return 0
fi
}
function check_theme() {
local theme="$1"
[[ -z "$theme" ]] && echo "ERROR: '${FUNCNAME[0]}' needs a theme as an argument!" >&2 && exit 1
if [[ ! -d "$ES_THEMES_DIR/$theme" ]]; then
echo "ERROR: '$theme' theme theme doesn't exists!" >&2
echo "Check '$ES_THEMES_DIR' to see all the available themes." >&2
exit 1
fi
}
function copy_all_files() {
local from_dir="$1"
local to_dir="$2"
[[ -z "$from_dir" || -z "$to_dir" ]] && echo "ERROR: '${FUNCNAME[0]}' needs a 'from' directory and a 'to' directory as arguments!" >&2 && exit 1
local file
for file in "$from_dir/"*; do
if [[ -f "$file" ]]; then
cp "$file" "$to_dir"
chown -R "$user":"$user" "$file"
fi
done
}
function remove_all_files() {
local from_dir="$1"
[[ -z "$from_dir" ]] && echo "ERROR: '${FUNCNAME[0]}' needs a 'from' directory as an argument!" >&2 && exit 1
for file in "$from_dir/"*; do
if [[ -f "$file" ]]; then
rm -rf "$file"
fi
done
}
function backup_icons() {
if [[ ! -d "$RP_BACKUP_ICONS_DIR" ]]; then
mkdir -p "$RP_BACKUP_ICONS_DIR" && chown -R "$user":"$user" "$RP_BACKUP_ICONS_DIR"
copy_all_files "$RP_ICONS_DIR" "$RP_BACKUP_ICONS_DIR"
fi
}
function choose_splashscreen() {
local splashscreen
local splashscreens
local options=()
local option
splashscreens=($(find "$ES_THEMES_DIR/$THEME" -maxdepth 1 -type f -name "*splash*"))
if [[ "${#splashscreens[@]}" -gt 0 ]]; then
echo "Choose a splashscreen to install:"
for splashscreen in "${splashscreens[@]}"; do
if [[ -f "$splashscreen" ]]; then
options+=("$(basename "$splashscreen")")
fi
done
select option in "${options[@]}"; do
if [[ -n "$option" ]]; then
add_splashscreen "$ES_THEMES_DIR/$THEME/$option"
break
else
echo "Invalid option. Choose a number between 1 and ${#options[@]}."
fi
done
else
add_splashscreen "$ES_THEMES_DIR/$THEME/$splashscreens"
fi
}
function add_splashscreen() {
local splashscreen="$1"
[[ -z "$splashscreen" ]] && echo "ERROR: '${FUNCNAME[0]}' needs a splashscreen path as an argument!" >&2 && exit 1
if [[ ! -f "$SPLASH_LIST" ]]; then
touch "$SPLASH_LIST" && chown -R "$user":"$user" "$SPLASH_LIST"
fi
echo "$splashscreen" > "$SPLASH_LIST"
}
function remove_splashscreen() {
echo "" > "$SPLASH_LIST"
}
function install_icons() {
echo "> Installing '$THEME' theme icons ..."
check_rp_icons_dir
if check_theme_icons_dir; then
backup_icons
remove_all_files "$RP_ICONS_DIR"
copy_all_files "$THEME_ICONS_DIR" "$RP_ICONS_DIR"
else
return 1
fi
}
function uninstall_icons() {
echo "> Uninstalling '$THEME' theme icons ..."
if [[ -d "$RP_BACKUP_ICONS_DIR" ]]; then
remove_all_files "$RP_ICONS_DIR"
copy_all_files "$RP_BACKUP_ICONS_DIR" "$RP_ICONS_DIR"
rm -rf "$RP_BACKUP_ICONS_DIR"
else
echo "Seems like '$THEME' theme icons are not installed."
return 1
fi
}
function install_launching_images() {
echo "> Installing '$THEME' theme launching images ..."
[[ ! -d "$TMP" ]] && mkdir -p "$TMP" && chown -R "$user":"$user" "$TMP"
if [[ ! -d "$TMP_LAUNCHING_IMAGES_DIR" ]]; then
echo "> Dowloading '$THEME' launching images ..."
git clone "$GIT_THEME_LAUNCHING_IMAGES" "$TMP_LAUNCHING_IMAGES_DIR"
fi
local from_dir
local to_dir
local launching_image
for from_dir in "$TMP_LAUNCHING_IMAGES_DIR/"*; do
if [[ -d "$from_dir" ]]; then
to_dir="$RP_CONFIG_DIR/$(basename "$from_dir")"
if [[ -d "$to_dir" ]]; then
launching_image=$(find "$from_dir" -type f -name 'launching.*' -print -quit 2> /dev/null)
if [[ -f "$launching_image" ]]; then
cp "$launching_image" "$to_dir/$(basename "$launching_image")"
fi
fi
fi
done
}
function uninstall_launching_images() {
echo "> Uninstalling '$THEME' theme launching images ..."
if [[ -d "$TMP_LAUNCHING_IMAGES_DIR" ]]; then
rm -rf "$TMP_LAUNCHING_IMAGES_DIR"
local dir
local launching_image
for dir in "$RP_CONFIG_DIR/"*; do
if [[ -d "$dir" ]]; then
launching_image=$(find "$dir" -type f -name 'launching.*' -print -quit 2> /dev/null)
if [[ -f "$launching_image" ]]; then
rm "$launching_image"
fi
fi
done
else
echo "Seems like '$THEME' theme launching images are not installed."
return 1
fi
}
function install_splashscreen() {
echo "> Installing '$THEME' theme splashscreen ..."
choose_splashscreen
}
function uninstall_splashscreen() {
echo "> Uninstalling '$THEME' theme splashscreen ..."
remove_splashscreen
}
function get_options() {
[[ -z "$1" ]] && usage
while [[ -n "$1" ]]; do
case "$1" in
--install)
if install_icons; then
echo "'$THEME' theme icons installed successfully!"
else
echo "ERROR: Couldn't install '$THEME' theme icons." >&2
fi
if install_launching_images; then
echo "'$THEME' theme launching images installed successfully!"
else
echo "ERROR: Couldn't install '$THEME' theme launching images." >&2
fi
if install_splashscreen; then
echo "'$THEME' theme splashscreen installed successfully!"
else
echo "ERROR: Couldn't install '$THEME' theme splashscreen." >&2
fi
;;
--uninstall)
if uninstall_icons; then
echo "'$THEME' theme icons uninstalled successfully!"
else
echo "ERROR: Couldn't uninstall '$THEME' theme icons." >&2
fi
if uninstall_launching_images; then
echo "'$THEME' theme launching images uninstalled successfully!"
else
echo "ERROR: Couldn't uninstall '$THEME' theme launching images." >&2
fi
if uninstall_splashscreen; then
echo "'$THEME' theme splashscreen uninstalled successfully!"
else
echo "ERROR: Couldn't uninstall '$THEME' theme splashscreen." >&2
fi
;;
*)
echo "ERROR: Invalid option '$1'." >&2
echo "Try 'sudo $0 --install OR --uninstall'." >&2
exit 2
;;
esac
shift
done
}
function main() {
if ! is_sudo; then
echo "ERROR: '"$0"' must be run under 'sudo'." >&2
echo "Try 'sudo "$0"'." >&2
exit 1
fi
# [[ -n "$1" ]] && THEME="$1"
check_theme "$THEME"
# shift
get_options "$@"
}
main "$@"