-
Notifications
You must be signed in to change notification settings - Fork 5
/
eos-download-wallpapers
executable file
·233 lines (189 loc) · 6.69 KB
/
eos-download-wallpapers
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
#!/bin/bash
# Copy additional wallpapers from Github's EOS site.
source /usr/share/endeavouros/scripts/eos-script-lib-yad || exit 1
DIE() { echo "$progname: error: $1" >&2 ; exit 1 ; }
WARN() { echo "$progname: warning: $1" >&2 ; }
pushd() { command pushd "$@" >/dev/null ; }
popd() { command popd "$@" >/dev/null ; }
echo2() { echo "$@" >&2 ; }
echo2i() { echo "$@" | sed 's|^| |' >&2 ; }
# TestingTimeStamp() { [ "$TESTING" = "yes" ] && date "+%H:%M:%S" ; }
GetWallsCurl() {
local repofolders
local subfolder
local subfolderfiles
local directurl=https://raw.githubusercontent.com/EndeavourOS-Community-Editions/Community-wallpapers/main
local file files
local timeout=10 # for fetching info page
local timeoutfiles=30 # for fetching actual wallpaper files
local gotwalls=no
readarray -t repofolders <<< $(curl -Lsm $timeout "$repopage" | grep /tree/main/ | sed -e 's|.*.*/main/||' -e 's|".*||')
if [ -n "$repofolders" ] ; then
echo2 "====> The following wallpaper folders are available:"
for subfolder in "${repofolders[@]}" ; do
echo2i "$subfolder"
done
echo2 ""
mkdir -p $mainfolder
pushd $mainfolder
for subfolder in "${repofolders[@]}" ; do
read -p "====> Fetch wallpapers from $subfolder (Y/n)? " >&2
case "$REPLY" in
""|[Yy]*) ;;
*) continue ;;
esac
echo2 "====> Fetching ..."
readarray -t subfolderfiles <<< $(curl -Lsm $timeout "$repopage/tree/main/$subfolder" | grep /blob/main/ | sed -e "s|.*/main/$subfolder/||" -e 's|".*||')
if [ -z "$subfolderfiles" ] ; then
WARN "$FUNCNAME: did not find files in '$repopage/$subfolder'."
continue
fi
mkdir -p "$subfolder"
pushd "$subfolder"
files=()
for file in "${subfolderfiles[@]}" ; do
files+=("$directurl/$subfolder/$file")
done
if (curl -Lsm $timeoutfiles --fail-early --remote-name-all "${files[@]}") ; then
gotwalls=yes
else
WARN "$FUNCNAME: fetching files from '$repopage/$subfolder' failed."
retval=1
fi
popd
done
popd
else
WARN "$FUNCNAME: fetching subfolder names from '$repopage' failed."
retval=1
fi
[ "$gotwalls" = "no" ] && retval=1
}
gh2gl() {
local url="$1"
case "$EOS_FILESERVER_SITE" in
github)
echo "$url"
;;
gitlab | *)
case "$url" in
"https://github.com/EndeavourOS-Community-Editions/Community-wallpapers")
echo "https://gitlab.com/endeavouros-filemirror/Community-wallpapers"
;;
*)
DIE "sorry, this program can't use 'curl' with gitlab. Use 'git' instead."
;;
esac
;;
esac
}
GetWalls() {
local retval=0
case "$implementation" in
curl) GetWallsCurl ;;
git) git clone "$url" || retval=1 ;;
esac
return $retval
}
FetchWallpapers() {
# Copy new wallpapers into their place.
# Remove old wallpapers first.
local mainfolder=Community-wallpapers
local repopage=$(gh2gl https://github.com/EndeavourOS-Community-Editions/$mainfolder)
local url=$repopage.git
local urlsubdirs=(
eos_wallpapers_classic
eos_wallpapers_community
)
local targetroot=/usr/share/endeavouros/backgrounds
local subdir
local cmd # real commands here
local cmd2 # displayed commands here
GetWalls || return 1
pushd "$mainfolder"
# Collect all required commands into $cmd:
cmd="mkdir -p $targetroot" # collect all needed commands into $cmd
for subdir in "${urlsubdirs[@]}" ; do
[ -d "$subdir" ] || continue # skip if not folder
if [ -d "$targetroot/$subdir" ] ; then
echo2 "====> Note: will remove existing $targetroot/$subdir"
cmd+=" ; rm -rf '$targetroot/$subdir'" # will remove old wallpapers
fi
cmd+=" ; cp -r '$PWD/$subdir' $targetroot/" # will copy new wallpapers in place
done
cmd2=$(echo "$cmd" | sed 's| ; |\n|g') # reformat commands suitable for displaying
# Now do the actual wallpaper changes:
echo2 ""
echo2 "Running the following commands:"
echo2 "$cmd2"
echo2 ""
$EOS_ROOTER "$cmd"
popd
}
Options() {
local arg
local default="[default]"
for arg in "$@" ; do
case "$arg" in
--curl) implementation=curl ;;
--git) implementation=git ;;
--no-choose) make_choose=no ;;
--help | -h)
cat <<EOF
Usage: $progname [options]
Options:
--curl Download using curl (should use less bandwidth). $( [ $implementation_default = curl ] && echo $default )
--git Download using git. $( [ $implementation_default = git ] && echo $default )
--no-choose Do not make user to choose wallpaper right after downloading.
--help | -h This help.
EOF
exit 0
;;
*) DIE "unsupported parameter '$arg'" ;;
esac
done
}
Main()
{
local progname="$(basename "$0")"
local tmpdirbase="$HOME/.cache/$progname"
local workdir
local implementation_default="$EOS_WALLPAPER_FETCHER"
local implementation=$implementation_default
local make_choose=yes
local retval=0
local oldies
eos_assert_deps $progname yad || return 1
[ -n "$implementation_default" ] || implementation_default=git
# options will override the default values
Options "$@"
if [ "$EOS_FILESERVER_SITE" != "github" ] ; then
if [ "$implementation" != "git" ] ; then
echo2 "$progname: info: using 'git' instead of 'curl'."
implementation=git
fi
fi
workdir=$(mktemp -d "$tmpdirbase.XXXXX")
pushd "$workdir"
FetchWallpapers || retval=1
popd
rm -rf "$workdir" # cleanup
oldies="$(/usr/bin/ls -1d "$tmpdirbase".* 2>/dev/null)"
if [ -n "$oldies" ] ; then
echo2 ""
echo2 "====> Warning: the following old temporary folders can be deleted."
echo2i "$oldies"
read -p "====> Delete now (Y/n)? " >&2
case "$REPLY" in
""|[Yy]*) rm -rf $oldies ;;
esac
fi
# Allow user to change the wallpaper:
if [ "$make_choose" = "yes" ] ; then
if [ $retval -eq 0 ] ; then
echo2 "You can choose your new wallpaper now:"
/usr/bin/eos-wallpaper-set &
fi
fi
}
Main "$@"