-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinstall-themes
executable file
·482 lines (435 loc) · 18.6 KB
/
install-themes
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
#!/bin/sh
set -e
# ------------------------------------------------------------------------------
# Print usage
# ------------------------------------------------------------------------------
print_usage()
{
echo "Usage:"
echo " install.sh [options] <theme_name>"
echo
echo "Options:"
echo " --mc-16m - Use truecolor mc theme."
echo
echo "Arguments:"
echo " <theme_name> - Name of the theme to install, e.g. dark."
}
# ------------------------------------------------------------------------------
# Create a safe link to a file and make backup if needed.
# Args:
# $1 - Target
# $2 - Link name
# ------------------------------------------------------------------------------
create_link()
{
if [ -d "$2" ] && [ ! -L "$2" ]
then # Do not overwrite existing folders
echo "ERROR: A directory $2 already exists!"
exit 1
fi
if [ -e "$2" ] || [ -h "$2" ]
then # To prevent creation of a link on another link (e.g. link to folder)
mv "$2" "$2.bak"
fi
ln -s "$1" "$2"
}
# ------------------------------------------------------------------------------
# Create a safe copy of a file and make backup if needed.
# Args:
# $1 - From
# $2 - To
# ------------------------------------------------------------------------------
create_copy()
{
if [ -d "$2" ] && [ ! -L "$2" ]
then # Do not overwrite existing folders
echo "ERROR: A directory $2 already exists!"
exit 1
fi
if [ -e "$2" ] || [ -h "$2" ]
then # To prevent copying to a link
mv "$2" "$2.bak"
fi
cp "$1" "$2"
}
# ------------------------------------------------------------------------------
# Install Midnight Commander theme
# ------------------------------------------------------------------------------
install_mc()
{
echo "Installing Midnight Commander theme..."
# Detect MC config location
local config_dir=""
if [ -d "$HOME/.config/mc" ]
then
# Newer mc tends to keep config in .config
config_dir="$HOME/.config/mc"
echo "-> Found MC configuration in $config_dir"
elif [ -d "$HOME/.mc" ]
then
# Older style location
config_dir="$HOME/.mc"
echo "-> Found MC configuration in $config_dir"
else
# No config at all, we create one ourselves and link to both locations
echo "-> No existing MC configuration found, creating new directories"
config_dir="$HOME/.config/mc"
mkdir -p "$HOME/.config/mc"
ln -s "$HOME/.config/mc" "$HOME/.mc"
fi
# Create skins folder
if [ ! -e "$config_dir/skins" ]
then
echo "-> Creating skins directory."
mkdir "$config_dir/skins"
fi
# Choose variant
local mc_theme=""
if [ -n "$USE_MC_16M" ]
then
mc_theme="material16m.ini"
else
mc_theme="material256.ini"
fi
# Copy the theme (both variants)
echo "-> Copying the theme files"
create_copy "$THEME_DIR/mc/material16m.ini" "$config_dir/skins/material16m.ini"
create_copy "$THEME_DIR/mc/material256.ini" "$config_dir/skins/material256.ini"
# Update mc configuration
echo "-> Updating MC configuration"
local tmp=""
if [ -e "$config_dir/ini" ]
then
# Make backup
cp "$config_dir/ini" "$config_dir/ini.bak"
# Update
if grep -q "skin" "$config_dir/ini"
then
# Config file exists and has skin entry, replace
# To preserve links, we do not use sed -i, we use printf since echo interprets \\ in some shells
tmp=$(sed "/skin/c\\skin=$config_dir/skins/$mc_theme" "$config_dir/ini"); printf "%s\n" "$tmp" > "$config_dir/ini"
else
if grep -q "\[Midnight-Commander\]" "$config_dir/ini"
then
# Config files exists but has no skin entry and has [MC] entry
# To preserve links, we do not use sed -i, we use printf since echo interprets \\ in some shells
tmp=$(sed "/Midnight-Commander/a\\skin=$config_dir/skins/$mc_theme" "$config_dir/ini"); printf "%s\n" "$tmp" > "$config_dir/ini"
else
# Config files exists but has no skin entry and no [MC] entry
# To preserve links, we do not use sed -i, we use printf since echo interprets \\ in some shells
tmp=$(sed "1s:^:[Midnight-Commander]\nskin=$config_dir/skins/$mc_theme\n\n:" "$config_dir/ini"); printf "%s\n" "$tmp" > "$config_dir/ini"
fi
fi
else
# Config file does not exist, create and add only skin entry
cat <<EOT >> "$config_dir/ini"
[Midnight-Commander]
skin=/home/czikus/.config/mc/skins/$mc_theme
EOT
fi
echo "Done!"
}
# ------------------------------------------------------------------------------
# Install i3 window manager theme
# ------------------------------------------------------------------------------
install_i3()
{
echo "Installing i3 Window Manager theme..."
# Check if configuration file exists
if [ -e "$HOME/.i3/config" ]
then
echo "-> Found i3 configuration in $HOME/.i3/config"
# Make backup
cp "$HOME/.i3/config" "$HOME/.i3/config.bak"
# Update
echo "-> Updating i3 configuration"
# Replace/add each window configuration line
local i=""
local tmp=""
i=$(grep "^client.focused[ \t]" "$THEME_DIR/i3/config")
if grep -q "^client.focused[ \t]" "$HOME/.i3/config"
then # Entry found
# To preserve links, we do not use sed -i, we use printf since echo interprets \\ in some shells
tmp=$(sed "/^client.focused[ \t]/c\\$i" "$HOME/.i3/config"); printf "%s\n" "$tmp" > "$HOME/.i3/config"
else # Entry not found
# To preserve links, we do not use sed -i, we use printf since echo interprets \\ in some shells
tmp=$(sed "1s/^/$i\n/" "$HOME/.i3/config"); printf "%s\n" "$tmp" > "$HOME/.i3/config"
fi
i=$(grep "^client.focused_inactive[ \t]" "$THEME_DIR/i3/config")
if grep -q "^client.focused_inactive[ \t]" "$HOME/.i3/config"
then # Entry found
# To preserve links, we do not use sed -i, we use printf since echo interprets \\ in some shells
tmp=$(sed "/^client.focused_inactive[ \t]/c\\$i" "$HOME/.i3/config"); printf "%s\n" "$tmp" > "$HOME/.i3/config"
else # Entry not found
# To preserve links, we do not use sed -i, we use printf since echo interprets \\ in some shells
tmp=$(sed "1s/^/$i\n/" "$HOME/.i3/config"); printf "%s\n" "$tmp" > "$HOME/.i3/config"
fi
i=$(grep "^client.unfocused[ \t]" "$THEME_DIR/i3/config")
if grep -q "^client.unfocused[ \t]" "$HOME/.i3/config"
then # Entry found
# To preserve links, we do not use sed -i, we use printf since echo interprets \\ in some shells
tmp=$(sed "/^client.unfocused[ \t]/c\\$i" "$HOME/.i3/config"); printf "%s\n" "$tmp" > "$HOME/.i3/config"
else # Entry not found
# To preserve links, we do not use sed -i, we use printf since echo interprets \\ in some shells
tmp=$(sed "1s/^/$i\n/" "$HOME/.i3/config"); printf "%s\n" "$tmp" > "$HOME/.i3/config"
fi
i=$(grep "^client.urgent[ \t]" "$THEME_DIR/i3/config")
if grep -q "^client.urgent[ \t]" "$HOME/.i3/config"
then # Entry found
# To preserve links, we do not use sed -i, we use printf since echo interprets \\ in some shells
tmp=$(sed "/^client.urgent[ \t]/c\\$i" "$HOME/.i3/config"); printf "%s\n" "$tmp" > "$HOME/.i3/config"
else # Entry not found
# To preserve links, we do not use sed -i, we use printf since echo interprets \\ in some shells
tmp=$(sed "1s/^/$i\n/" "$HOME/.i3/config"); printf "%s\n" "$tmp" > "$HOME/.i3/config"
fi
# Replace/add bar color configuration
# Get the new bar color config
i=$(sed -n "/^[ \t]*colors[ \t]*{/,/}/p" "$THEME_DIR/i3/config")
# Replace newlines with \n
i=$(printf "%s" "$i" | awk 'BEGIN{ORS="\\n"}{print $0}')
# Trim the last '\n' newline awk ads
i=${i%??}
# Check if there is a bar configuration
if grep -q "^[ \t]*bar[ \t]*{" "$HOME/.i3/config"
then
if grep -q "^[ \t]*colors[ \t]*{" "$HOME/.i3/config"
then # Colors configuration exists
# Delete existing colors config
# To preserve links, we do not use sed -i, we use printf since echo interprets \\ in some shells
tmp=$(sed "/^[ \t]*colors[ \t]*{/,/}/d" "$HOME/.i3/config"); printf "%s\n" "$tmp" > "$HOME/.i3/config"
fi
# Add the colors section to existing bar section
# We use awk below instead of sed since sed has some weird problems with multiline strings on busybox
tmp=$(awk "/^[ \t]*bar[ \t]*\{[ \t]*$/{print \$0 \"\\n$i\"; next}{print}" "$HOME/.i3/config"); printf "%s\n" "$tmp" > "$HOME/.i3/config"
fi
else
# No config file, we use the theme as a stub
echo "-> No existing i3 configuration found, using the theme as a stub"
mkdir -p "$HOME/.i3"
cp "$THEME_DIR/i3/config" "$HOME/.i3/config"
fi
echo "Done!"
}
# ------------------------------------------------------------------------------
# Install Emacs theme
# ------------------------------------------------------------------------------
install_emacs()
{
echo "Installing Emacs theme..."
mkdir -p "$HOME/.emacs.d/themes"
create_copy "$THEME_DIR/emacs/material-theme.el" "$HOME/.emacs.d/themes/material-theme.el"
create_copy "$THEME_DIR/emacs/smart-mode-line-material-theme.el" "$HOME/.emacs.d/themes/smart-mode-line-material-theme.el"
echo "Done!"
echo
echo "To finish installation of the Emacs theme, add the following lines to"
echo "your Emacs initialization files:"
echo " (add-to-list 'custom-theme-load-path \"$HOME/.emacs.d/themes\")"
echo " (load-theme 'material t)"
echo " (setq sml/theme 'material)"
}
# ------------------------------------------------------------------------------
# Install KDE 5.0
# ------------------------------------------------------------------------------
install_kde5()
{
echo "Installing KDE 5.0 theme..."
echo "-> Copying theme files"
mkdir -p "$HOME/.local/share/color-schemes"
create_copy "$THEME_DIR/kde5/colors" "$HOME/.local/share/color-schemes/Material.colors"
mkdir -p "$HOME/.local/share/plasma/desktoptheme/material-breeze"
create_copy "$THEME_DIR/kde5/breeze/colors" "$HOME/.local/share/plasma/desktoptheme/material-breeze/colors"
create_copy "$THEME_DIR/kde5/breeze/metadata.desktop" "$HOME/.local/share/plasma/desktoptheme/material-breeze/metadata.desktop"
# Delete cache created by plasma
echo "-> Clearing Plasma 5 cache"
rm -f $HOME/.cache/plasma-svgelements-material-breeze*
rm -f $HOME/.cache/plasma_theme_material-breeze*
if which kwriteconfig5 > /dev/null
then
echo "-> Updating Plasma 5 configuration"
kwriteconfig5 --file "plasmarc" --group "Theme" --key "name" "material-breeze"
fi
echo "Done!"
echo
echo "To finish installation of the KDE theme, you need to select the"
echo "Material color scheme and Material Breeze desktop theme in your"
echo "KDE system settings."
}
# ------------------------------------------------------------------------------
# Install GTK
# ------------------------------------------------------------------------------
install_gtk()
{
echo "Installing GTK theme..."
# Copy the theme
local theme_name=""
echo "-> Copying the theme files"
if [ -n "$USE_GTK_BREEZE" ]
then
# GTK2 themes work only from .themes, while GTK3 from .local/share/themes
theme_name="Material-Breeze"
rm -rf "$HOME/.themes/$theme_name"
rm -rf "$HOME/.local/share/themes/$theme_name"
mkdir -p "$HOME/.themes/$theme_name/gtk-2.0"
mkdir -p "$HOME/.themes/$theme_name/assets"
mkdir -p "$HOME/.local/share/themes/$theme_name/gtk-3.0"
mkdir -p "$HOME/.local/share/themes/$theme_name/assets"
cp -r $THEME_DIR/gtk_breeze/gtk-2.0/* "$HOME/.themes/$theme_name/gtk-2.0/"
cp -r $THEME_DIR/gtk_breeze/assets/* "$HOME/.themes/$theme_name/assets/"
cp $THEME_DIR/gtk_breeze/index.theme "$HOME/.themes/$theme_name/index.theme"
cp -r $THEME_DIR/gtk_breeze/gtk-3.0/* "$HOME/.local/share/themes/$theme_name/gtk-3.0/"
cp -r $THEME_DIR/gtk_breeze/assets/* "$HOME/.local/share/themes/$theme_name/assets/"
cp $THEME_DIR/gtk_breeze/index.theme "$HOME/.local/share/themes/$theme_name/index.theme"
cp $THEME_DIR/gtk_breeze/gtk.css "$HOME/.config/gtk-3.0/gtk.css"
cp $THEME_DIR/gtk_breeze/colors.css "$HOME/.config/gtk-3.0/colors.css"
else
theme_name="Material-Orion"
rm -rf "$HOME/.themes/$theme_name"
rm -rf "$HOME/.local/share/themes/$theme_name"
mkdir -p "$HOME/.themes/$theme_name/gtk-2.0"
mkdir -p "$HOME/.local/share/themes/$theme_name/gtk-3.0"
cp -r $THEME_DIR/gtk_orion/gtk-2.0/* "$HOME/.themes/$theme_name/gtk-2.0/"
cp $THEME_DIR/gtk_orion/index.theme "$HOME/.themes/$theme_name/index.theme"
cp -r $THEME_DIR/gtk_orion/gtk-3.0/* "$HOME/.local/share/themes/$theme_name/gtk-3.0/"
cp $THEME_DIR/gtk_orion/index.theme "$HOME/.local/share/themes/$theme_name/index.theme"
fi
echo "-> Updating GTK 2.0 configuration"
if [ -e "$HOME/.gtkrc-2.0" ]
then
# Include
if grep -q "include[ \t]*\".*themes.*\"" "$HOME/.gtkrc-2.0"
then # Entry found, replace
# To preserve links, we do not use sed -i, we use printf since echo interprets \\ in some shells
tmp=$(sed "/include[ \t]*\".*themes.*\"/c\\include \"$HOME/.local/share/themes/$theme_name/gtk-2.0/gtkrc\"" "$HOME/.gtkrc-2.0"); printf "%s\n" "$tmp" > "$HOME/.gtkrc-2.0"
else # Entry not found, prepend entry to the file
# To preserve links, we do not use sed -i, we use printf since echo interprets \\ in some shells
tmp=$(sed "1s:^:include \"$HOME/.local/share/themes/$theme_name/gtk-2.0/gtkrc\"\n:" "$HOME/.gtkrc-2.0"); printf "%s\n" "$tmp" > "$HOME/.gtkrc-2.0"
fi
# Theme setting
if grep -q "gtk-theme-name" "$HOME/.gtkrc-2.0"
then # Entry found, replace
# To preserve links, we do not use sed -i, we use printf since echo interprets \\ in some shells
tmp=$(sed "/gtk-theme-name/c\\gtk-theme-name=\"$theme_name\"" "$HOME/.gtkrc-2.0"); printf "%s\n" "$tmp" > "$HOME/.gtkrc-2.0"
else # Entry not found, prepend entry to the file
# To preserve links, we do not use sed -i, we use printf since echo interprets \\ in some shells
echo "gtk-theme-name=\"$theme_name\"" >> "$HOME/.gtkrc-2.0"
fi
else
# Config file does not exist, create and add theme entry
cat <<EOT > "$HOME/.gtkrc-2.0"
include "$HOME/.local/share/themes/$theme_name/gtk-2.0/gtkrc"
gtk-theme-name="$theme_name"
EOT
fi
echo "-> Updating GTK 3.0 configuration"
if [ -e "$HOME/.config/gtk-3.0/settings.ini" ]
then
if grep -q "\[Settings\]" "$HOME/.config/gtk-3.0/settings.ini"
then # Settings group exists
if grep -q "gtk-theme-name" "$HOME/.config/gtk-3.0/settings.ini"
then # gtk-theme-name entry exists, replace
# To preserve links, we do not use sed -i, we use printf since echo interprets \\ in some shells
tmp=$(sed "/gtk-theme-name/c\\gtk-theme-name=$theme_name" "$HOME/.config/gtk-3.0/settings.ini"); printf "%s\n" "$tmp" > "$HOME/.config/gtk-3.0/settings.ini"
else # gtk-theme-name entry does not exists, add to [Settings]
# To preserve links, we do not use sed -i, we use printf since echo interprets \\ in some shells
tmp=$(sed "/\[Settings\]/a\\gtk-theme-name=$theme_name" "$HOME/.config/gtk-3.0/settings.ini"); printf "%s\n" "$tmp" > "$HOME/.config/gtk-3.0/settings.ini"
fi
else # Settings group does not exist, append it
cat <<EOT >> "$HOME/.config/gtk-3.0/settings.ini"
[Settings]
gtk-theme-name=$theme_name
EOT
fi
else
cat <<EOT > "$HOME/.config/gtk-3.0/settings.ini"
[Settings]
gtk-theme-name=$theme_name
EOT
fi
echo "Done!"
}
# ------------------------------------------------------------------------------
# Install XTerm
# ------------------------------------------------------------------------------
install_xterm()
{
echo "Installing XTerm theme..."
# Copy the theme
echo "-> Copying the theme file"
create_copy "$THEME_DIR/xterm/Xresources-xterm-colors" "$HOME/.Xresources-xterm-colors"
# Update .Xresources
echo "-> Adding include to $HOME/.Xresources"
local tmp=""
if [ -e "$HOME/.Xresources" ]
then
# Make backup
cp "$HOME/.Xresources" "$HOME/.Xresources.bak"
# Update
if grep -q ".Xresources-xterm-colors" "$HOME/.Xresources"
then
# File exists and has include, replace
# To preserve links, we do not use sed -i, we use printf since echo interprets \\ in some shells
tmp=$(sed "/Xresources-xterm-colors/c\\#include \"$HOME/.Xresources-xterm-colors\"" "$HOME/.Xresources"); printf "%s\n" "$tmp" > "$HOME/.Xresources"
else
# File exists but has no include, append
echo "#include \"$HOME/.Xresources-xterm-colors\"" >> "$HOME/.Xresources"
fi
else
# File does not exist, create and add include
echo "#include \"$HOME/.Xresources-xterm-colors\"" > "$HOME/.Xresources"
fi
# Load .Xresources
echo "-> Loading .Xresources"
if which xrdb > /dev/null
then
set +e
DISPLAY=:0 xrdb -merge "$HOME/.Xresources"
if [ $? -ne 0 ]
then
echo "---> Warning: Could not find display, .Xresources will not be loaded now."
fi
set -e
else
echo "---> Warning: xrdb not found, .Xresources will not be loaded now."
fi
echo "Done!"
}
# ------------------------------------------------------------------------------
# Main
# ------------------------------------------------------------------------------
# Parse args
while [ "$1" != "${1#--}" ]
do
case "$1" in
"--mc-16m")
USE_MC_16M=1
;;
"--gtk-breeze")
USE_GTK_BREEZE=1
;;
*)
print_usage
exit 1
;;
esac
shift
done
theme_name=$1
if [ -z "$theme_name" ]
then
print_usage
exit 1
fi
# Get theme dir
THEME_DIR=$( cd "${0%/*}/out/$theme_name" && pwd )
# Install
install_kde5
echo
install_gtk
echo
install_xterm
echo
install_mc
echo
install_i3
echo
install_emacs