[eos-bash-shared] re-implementation of eos-download-wallpapers

This commit is contained in:
manuel
2025-07-14 18:44:50 +03:00
parent 087a8a5bc9
commit 792652f3fe
+75 -210
View File
@@ -1,233 +1,98 @@
#!/bin/bash
# Copy additional wallpapers from Github's EOS site.
# Copy additional wallpapers from Github or Gitlab EOS site.
source /usr/share/endeavouros/scripts/eos-script-lib-yad || exit 1
Main() {
local -r gitlab="https://gitlab.com/endeavouros-filemirror/Community-wallpapers/-/archive/main/Community-wallpapers-main.zip"
local -r github="https://github.com/EndeavourOS-Community-Editions/Community-wallpapers/archive/refs/heads/main.zip"
local url="$gitlab"
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 ; }
source /etc/eos-script-lib-yad.conf || return 1
case "$EOS_FILESERVER_SITE" in
github) url="$github" ;;
gitlab) ;;
*) DIE "value '$EOS_FILESERVER_SITE' of variable EOS_FILESERVER_SITE is not supported." ;;
esac
# TestingTimeStamp() { [ "$TESTING" = "yes" ] && date "+%H:%M:%S" ; }
local progname=${0##*/}
local workdir=""
local dirtodel=""
local backup=no
local -r walldir=/usr/share/endeavouros/backgrounds
local -r zipdir=Community-wallpapers-main
local dloader=wget
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
Options "$@"
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
if [ "$workdir" ] ; then
# user managed $workdir
[ -d "$workdir" ] || DIE "working directory '$workdir' does not exist."
[ -w "$workdir" ] || DIE "working directory '$workdir' cannot be written."
else
WARN "$FUNCNAME: fetching subfolder names from '$repopage' failed."
retval=1
# temporary $workdir
workdir=$(mktemp -d) || DIE "cannot create a working directory under '/tmp'."
chmod go-rwx "$workdir"
dirtodel="$workdir"
fi
echo "Downloading wallpapers, please wait ..." >&2
case "$dloader" in
wget) wget -O "$workdir"/Community-wallpapers.zip "$url" || DIE "wget failed, code $?" ;;
curl) curl -Lm 300 -o "$workdir"/Community-wallpapers.zip "$url" || DIE "curl failed, code $?" ;;
esac
echo "Moving wallpapers under $walldir ..." >&2
unzip -qd "$workdir" "$workdir"/Community-wallpapers.zip || exit 1
rm -f "$workdir"/Community-wallpapers.zip
rm -f "$workdir/$zipdir"/{community-wallpapers.png,README.md}
rm -rf "$workdir/$zipdir"/scripts
if [ $backup = yes ] ; then
sudo rm -rf $walldir/eos_wallpapers_{classic,community}.bak
sudo mv $walldir/eos_wallpapers_classic $walldir/eos_wallpapers_classic.bak
sudo mv $walldir/eos_wallpapers_community $walldir/eos_wallpapers_community.bak
else
sudo rm -rf $walldir/{eos_wallpapers_classic,eos_wallpapers_community}
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
sudo cp -r "$workdir/$zipdir"/eos_wallpapers_* $walldir/ || exit 1
if [ "$dirtodel" = "$workdir" ] ; then
rm -rf "$workdir"
else
rm -rf "$workdir/$zipdir"
fi
}
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'" ;;
--github) url="${URLs[github]}" ;;
--gitlab) url="${URLs[gitlab]}" ;;
--wget) dloader=wget ;;
--curl) dloader=curl ;;
--backup) backup=yes ;;
--workdir=*) workdir="${arg#*=}" ;;
-h | --help) Help 0 ;;
*) DIE "parameter '$arg' is not supported." ;;
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
Help() {
cat <<EOF
Usage: $progname [options]
Options: -h, --help This help.
--gitlab Download wallpapers from EndeavourOS pages at gitlab (default).
--github Download wallpapers from EndeavourOS pages at github.
--wget Use wget to download wallpapers (default).
--curl Use curl to download wallpapers.
--backup Backup existing wallpapers to $walldir/*.bak.
Default: no backups made.
EOF
[ "$1" ] && exit $1
}
DIE() { echo "$progname: error: $1" >&2; Help 1; }
Main "$@"