fixes-package/bin/periodic/fix-zombie-appicons

78 lines
2.3 KiB
Plaintext
Raw Normal View History

2022-09-15 23:14:20 +00:00
#!/bin/bash
pidof -sq -o %PPID -x "$(basename "$0")" && exit
2022-09-20 00:06:23 +00:00
datastore="$HOME"/.local/share/fix-zombie-appicons
2022-09-19 18:35:32 +00:00
bakdir="$datastore"/backup
data="$datastore"/tweaked-desktop-files
2022-09-15 23:14:20 +00:00
# Directories where desktop files are stored
user="$HOME"/.local/share/applications
pacman_global=/usr/share/applications
2022-09-19 18:35:32 +00:00
pacman_local=/usr/local/share/applications
flatpak_global=/var/lib/flatpak/exports/share/applications
flatpak_local="$HOME"/.local/share/flatpak/exports/share/applications
2022-09-15 23:14:20 +00:00
snap=/var/lib/snapd/desktop/applications
2022-10-01 16:43:10 +00:00
mkdir -p "$bakdir" "$user" "$flatpak_local" ||
{ echo "failed to make directoris $bakdir, $user & $flatpak_local"; exit 1; }
2022-09-19 23:44:56 +00:00
2022-09-15 23:14:20 +00:00
detectfiles() {
for file in "$user"/*.desktop; do
name=$(basename "$file") || continue
2022-09-19 18:35:32 +00:00
[ -d "$file" ] || grep -xq "$name" "$data" && continue
[ -f "$pacman_global/$name" ] ||
[ -f "$pacman_local/$name" ] ||
[ -f "$flatpak_global/$name" ] ||
[ -f "$flatpak_local/$name" ] ||
[ -f "$snap/$name" ] &&
echo "$name" >> "$data"
2022-09-15 23:14:20 +00:00
done
}
fixfiles() {
lastmod=$(stat -c "%Y" "$data")
tmp="$(mktemp)"
trap 'rm $tmp' EXIT
[ -f "$data" ] && cp "$data" "$tmp" && copied='true'
lineno=0
2022-09-19 18:35:32 +00:00
[ "$copied" = 'true' ] && while IFS= read -r name; do
2022-09-15 23:14:20 +00:00
((lineno++))
file="$user/$name"
2022-09-19 18:35:32 +00:00
[ -f "$file" ] || { sed -i "${lineno}d" "$tmp" && ((lineno--)) ; continue; }
[ -f "$pacman_global/$name" ] ||
2022-09-15 23:14:20 +00:00
[ -f "$pacman_local/$name" ] ||
2022-09-19 18:35:32 +00:00
[ -f "$flatpak_global/$name" ] ||
[ -f "$flatpak_local/$name" ] ||
2022-09-15 23:14:20 +00:00
[ -f "$snap/$name" ] ||
2022-09-19 23:09:11 +00:00
mv "$file" "$bakdir/$name.bak"
2022-09-15 23:14:20 +00:00
done < "$data"
[ "$copied" = 'true' ] && [ "$(stat -c '%Y' "$data")" = "$lastmod" ] && sort -u "$tmp" > "$data"
2022-09-15 23:14:20 +00:00
2022-09-19 18:35:32 +00:00
for bakfile in "$bakdir"/*.desktop.bak; do
name=$(basename "${bakfile%.bak}") || continue
origfile="$user/$name"
2022-09-15 23:14:20 +00:00
[ -f "$origfile" ] && continue
2022-09-19 18:35:32 +00:00
[ -f "$pacman_global/$name" ] ||
2022-09-15 23:14:20 +00:00
[ -f "$pacman_local/$name" ] ||
2022-09-19 18:35:32 +00:00
[ -f "$flatpak_global/$name" ] ||
[ -f "$flatpak_local/$name" ] ||
2022-09-15 23:14:20 +00:00
[ -f "$snap/$name" ] &&
mv "$bakfile" "$origfile"
done
}
2022-09-19 18:35:32 +00:00
for dir in "$user" "$pacman_global" "$pacman_local" "$flatpak_global" "$flatpak_local" "$snap"; do
[ -d "$dir" ] && existing_dirs+=("$dir")
2022-09-19 18:35:32 +00:00
done
2022-09-19 23:09:11 +00:00
detectfiles
fixfiles
2022-09-19 18:35:32 +00:00
while read -r line; do
if [ "$line" = "$user/" ]; then
detectfiles
else
fixfiles
fi
2022-10-01 16:43:10 +00:00
done < <(inotifywait -qm --format '%w' --include '\.desktop$' -e 'move,move_self,create,delete,delete_self,unmount' "${existing_dirs[@]}")