-
Notifications
You must be signed in to change notification settings - Fork 21
/
stylepak
executable file
·154 lines (121 loc) · 4.09 KB
/
stylepak
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
#!/bin/bash
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
set -e
GTK_THEME_VER=3.22
[[ -n "$STYLEPAK_VERBOSE" ]] && set -x ||:
usage='usage: stylepak install-system|install-user|clear-cache [<theme>]'
die() {
echo "$@" >&2
exit 1
}
cache_home="${XDG_CACHE_HOME:-$HOME/.cache}"
data_home="${XDG_DATA_HOME:-$HOME/.local/share}"
stylepak_cache="$cache_home/stylepak"
repo_dir="$stylepak_cache/repo"
old_cache="$cache_home/pakitheme"
if [[ -e "$old_cache" ]]; then
# If the cache under the old name still exists, try to preserve it under the new name.
# But, if the new cache also already exists, just trash the old one instead.
[[ -e "$stylepak_cache" ]] && rm -rf "$old_cache" || mv "$old_cache" "$stylepak_cache"
fi
install_target=""
theme=""
for arg in "$@"; do
case "$arg" in
-h|--help) echo "$usage"; exit ;;
-*) die "$usage" ;;
*)
if [[ -z "$install_target" ]]; then
case "$arg" in
install-system) install_target=system ;;
install-user) install_target=user ;;
*) die "$usage" ;;
esac
elif [[ -z "$theme" ]]; then
theme="$arg"
else
die "$usage"
fi
;;
esac
done
[[ -n "$install_target" ]] || die "$usage"
[[ -n "$theme" ]] || \
theme=$(gsettings get org.gnome.desktop.interface gtk-theme | tr -d "'")
echo "$theme" | grep -Eq '^[A-Za-z0-9._\-]+$' || \
die "Invalid theme name (may only contain letters, digits, '_', '-'): $theme"
echo "Converting theme: $theme"
app_id=org.gtk.Gtk3theme.$theme
for location in "$data_home/themes" "$HOME/.themes" /usr/share/themes; do
if [[ -d "$location/$theme" ]]; then
echo "Found theme located at: $location/$theme"
theme_path="$location/$theme"
break
fi
done
[[ -n "$theme_path" ]] || die 'Could not locate theme.'
root_dir="$stylepak_cache/$theme"
repo_dir="$root_dir/repo"
rm -rf "$root_dir" "$repo_dir"
mkdir -p "$repo_dir"
ostree --repo="$repo_dir" init --mode=archive
ostree --repo="$repo_dir" config set core.min-free-space-percent 0
build_dir="$root_dir/build"
rm -rf "$build_dir"
mkdir -p "$build_dir/files"
theme_gtk_version=$(ls -1d "$theme_path"/* 2>/dev/null \
| grep -Po 'gtk-3\.\K\d+$' \
| sort -nr \
| head -1)
[[ -n "$theme_gtk_version" ]] || \
die "Theme directory did not contain any recognized GTK themes."
cp -a "$theme_path/gtk-3.$theme_gtk_version/"* "$build_dir/files"
mkdir -p "$build_dir/files/share/appdata"
cat >"$build_dir/files/share/appdata/$app_id.appdata.xml" <<EOF
<?xml version="1.0" encoding="UTF-8"?>
<component type="runtime">
<id>$app_id</id>
<metadata_license>CC0-1.0</metadata_license>
<name>$theme GTK Theme</name>
<summary>$theme (generated via stylepak)</summary>
</component>
EOF
appstream-compose \
--prefix="$build_dir/files" \
--basename="$app_id" \
--origin=flatpak "$app_id"
ostree --repo="$repo_dir" commit -b base --tree=dir="$build_dir"
bundles=()
while read -r arch; do
bundle="$root_dir/$app_id-$arch.flatpak"
rm -rf "$build_dir"
ostree --repo="$repo_dir" checkout -U base "$build_dir"
read -rd '' metadata <<EOF ||:
[Runtime]
name=$app_id
runtime=$app_id/$arch/$GTK_THEME_VER
sdk=$app_id/$arch/$GTK_THEME_VER
EOF
# Make sure there is no trailing newline, so xa.metadata doesn't get confused later
echo -n "$metadata" > "$build_dir/metadata"
ostree --repo="$repo_dir" commit \
-b "runtime/$app_id/$arch/$GTK_THEME_VER" \
--add-metadata-string "xa.metadata=$(cat $build_dir/metadata)" \
--link-checkout-speedup \
"$build_dir"
flatpak build-bundle --runtime --arch="$arch" \
"$repo_dir" "$bundle" "$app_id" "$GTK_THEME_VER"
trap 'rm "$bundle"' EXIT
bundles+=("$bundle")
# Note: a pipe can't be used because it will mess with subshells and cause the append
# to bundles to fail.
done < <(flatpak list --runtime --columns=arch:f | sort -u)
for bundle in "${bundles[@]}"; do
if [ "$install_target" == "system" ]; then
sudo flatpak install -y --$install_target "$bundle"
else
flatpak install -y --$install_target "$bundle"
fi
done