2022-09-15 23:14:20 +00:00
|
|
|
#!/bin/bash
|
|
|
|
pidof -sq -o %PPID -x "$(basename "$0")" && exit
|
2022-09-19 18:35:32 +00:00
|
|
|
datastore="$HOME"/.local/share/desktop-file-data
|
|
|
|
bakdir="$datastore"/backup
|
|
|
|
data="$datastore"/tweaked-desktop-files
|
|
|
|
|
|
|
|
mkdir -p "$bakdir" || { echo "failed to make directory $bakdir"; exit 1; }
|
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
|
|
|
|
|
|
|
|
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 18:35:32 +00:00
|
|
|
mv "$file" "$bakdir/$file.bak"
|
2022-09-15 23:14:20 +00:00
|
|
|
done < "$data"
|
|
|
|
|
|
|
|
[ "$copied" = 'true' ] && [ "$(stat -c '%Y' "$data")" = "$lastmod" ] && sort "$tmp" | uniq > "$data"
|
|
|
|
|
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
|
|
|
existing_dirs=()
|
|
|
|
for dir in "$user" "$pacman_global" "$pacman_local" "$flatpak_global" "$flatpak_local" "$snap"; do
|
|
|
|
[ -d "$dir" ] && existing_dirs=("${existing_dirs[@]}" "$dir")
|
|
|
|
done
|
|
|
|
|
|
|
|
while read -r line; do
|
|
|
|
if [ "$line" = "$user/" ]; then
|
|
|
|
detectfiles
|
|
|
|
else
|
|
|
|
fixfiles
|
|
|
|
fi
|
|
|
|
done < <(inotifywait -qm --format '%w' --include '\.desktop$' -e 'move,move_self,create,delete,delete_self,unmount' "${existing_dirs[@]}")
|