iso-profiles-settings/tromjaro/xfce/desktop-overlay/usr/bin/fix-tweaked-desktop-files

89 lines
3.3 KiB
Plaintext
Raw Normal View History

2021-11-15 21:39:45 +00:00
#!/bin/bash
2021-12-16 23:11:31 +00:00
pidof -sq -o %PPID -x "$(basename "$0")" && exit
2021-11-15 21:39:45 +00:00
data="$HOME"/.local/share/applications/tweaked-desktop-files
2021-12-24 23:15:33 +00:00
data2="$HOME"/.local/share/applications/bakkedup-desktop-files
2021-12-16 23:11:31 +00:00
# Directories where desktop files are stored
user="$HOME"/.local/share/applications
flatpak_local="$HOME"/.local/share/flatpak/exports/share/applications
flatpak_global=/var/lib/flatpak/exports/share/applications
pacman_local=/usr/local/share/applications
pacman_global=/usr/share/applications
snap=/var/lib/snapd/desktop/applications
2021-11-15 21:39:45 +00:00
detectfiles() {
2021-12-16 23:11:31 +00:00
for file in "$user"/*.desktop; do
2021-11-15 21:39:45 +00:00
name=$(basename "$file") || continue
2021-12-16 23:11:31 +00:00
[ -d "$file" ] || grep -xq "\(flatpak-local\|flatpak-global\|pacman-local\|pacman-global\|snap\)/$name" "$data" && continue
if [ -f "$flatpak_local/$name" ]; then
echo "flatpak-local/$name" >> "$data"
elif [ -f "$flatpak_global/$name" ]; then
echo "flatpak-global/$name" >> "$data"
elif [ -f "$pacman_local/$name" ]; then
echo "pacman-local/$name" >> "$data"
elif [ -f "$pacman_global/$name" ]; then
echo "pacman-global/$name" >> "$data"
elif [ -f "$snap/$name" ]; then
echo "snap/$name" >> "$data"
fi
2021-11-15 21:39:45 +00:00
done
}
fixfiles() {
lastmod=$(stat -c "%Y" "$data")
2021-12-16 23:11:31 +00:00
tmp="$(mktemp)"
trap 'rm $tmp' EXIT
2021-11-15 21:39:45 +00:00
[ -f "$data" ] && cp "$data" "$tmp" && copied='true'
2021-12-16 23:11:31 +00:00
lineno=0
[ "$copied" = 'true' ] && while IFS= read -r line; do
((lineno++))
name=${line#*/}
file="$user/$name"
# shellcheck disable=2015 # To disable shellcheck error here, line below is intended to work this way
echo "$line" | grep -xq '\(flatpak-local\|flatpak-global\|pacman-local\|pacman-global\|snap\)/.\+' && [ -f "$file" ] || { sed -i "${lineno}d" "$tmp" && ((lineno--)) ; continue; }
[ -f "$flatpak_local/$name" ] ||
[ -f "$flatpak_global/$name" ] ||
[ -f "$pacman_local/$name" ] ||
[ -f "$pacman_global/$name" ] ||
[ -f "$snap/$name" ] ||
2022-06-14 14:56:42 +00:00
{ mv "$file" "$file.bak" && echo "$line" >> "$data2"; }
2021-11-15 21:39:45 +00:00
done < "$data"
2021-12-16 23:11:31 +00:00
[ "$copied" = 'true' ] && [ "$(stat -c '%Y' "$data")" = "$lastmod" ] && sort "$tmp" | uniq > "$data"
2021-11-15 21:39:45 +00:00
2022-06-14 14:56:42 +00:00
[ -n "$(sort "$data2" | uniq -d)" ] && cp "$data2" "$tmp" && sort "$tmp" | uniq > "$data2"
2021-12-16 23:11:31 +00:00
for bakfile in "$user"/*.desktop.bak; do
origfile="${bakfile%.bak}"
2021-11-15 21:39:45 +00:00
[ -f "$origfile" ] && continue
name=$(basename "$origfile") || continue
2021-12-16 23:11:31 +00:00
[ -f "$flatpak_local/$name" ] ||
[ -f "$flatpak_global/$name" ] ||
[ -f "$pacman_local/$name" ] ||
[ -f "$pacman_global/$name" ] ||
[ -f "$snap/$name" ] &&
2021-11-15 21:39:45 +00:00
mv "$bakfile" "$origfile"
done
}
while :; do
2021-12-16 23:11:31 +00:00
user_new=$(ls -ad "$user"/*.desktop 2>/dev/null)
flatpak_local_new=$(ls -ad "$flatpak_local"/*.desktop 2>/dev/null)
flatpak_global_new=$(ls -ad "$flatpak_global"/*.desktop 2>/dev/null)
pacman_local_new=$(ls -ad "$pacman_local"/*.desktop 2>/dev/null)
pacman_global_new=$(ls -ad "$pacman_global"/*.desktop 2>/dev/null)
snap_new=$(ls -ad "$snap"/*.desktop 2>/dev/null)
[ "$user_new" != "$user_old" ] && detectfiles
[ "$flatpak_local_new" != "$flatpak_local_old" ] ||
[ "$flatpak_global_new" != "$flatpak_global_old" ] ||
[ "$pacman_local_new" != "$pacman_local_old" ] ||
[ "$pacman_global_new" != "$pacman_global_old" ] ||
2021-11-15 21:39:45 +00:00
[ "$snap_new" != "$snap_old" ] && fixfiles
2021-12-16 23:11:31 +00:00
user_old=$user_new
flatpak_local_old=$flatpak_local_new
flatpak_global_old=$flatpak_global_new
pacman_local_old=$pacman_local_new
pacman_global_old=$pacman_global_new
2021-11-15 21:39:45 +00:00
snap_old=$snap_new
sleep 5
2022-06-14 14:56:42 +00:00
done