#!/bin/bash pidof -sq -o %PPID -x "$(basename "$0")" && exit 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; } # Directories where desktop files are stored user="$HOME"/.local/share/applications pacman_global=/usr/share/applications pacman_local=/usr/local/share/applications flatpak_global=/var/lib/flatpak/exports/share/applications flatpak_local="$HOME"/.local/share/flatpak/exports/share/applications snap=/var/lib/snapd/desktop/applications detectfiles() { for file in "$user"/*.desktop; do name=$(basename "$file") || continue [ -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" done } fixfiles() { lastmod=$(stat -c "%Y" "$data") tmp="$(mktemp)" trap 'rm $tmp' EXIT [ -f "$data" ] && cp "$data" "$tmp" && copied='true' lineno=0 [ "$copied" = 'true' ] && while IFS= read -r name; do ((lineno++)) file="$user/$name" [ -f "$file" ] || { sed -i "${lineno}d" "$tmp" && ((lineno--)) ; continue; } [ -f "$pacman_global/$name" ] || [ -f "$pacman_local/$name" ] || [ -f "$flatpak_global/$name" ] || [ -f "$flatpak_local/$name" ] || [ -f "$snap/$name" ] || mv "$file" "$bakdir/$name.bak" done < "$data" [ "$copied" = 'true' ] && [ "$(stat -c '%Y' "$data")" = "$lastmod" ] && sort "$tmp" | uniq > "$data" for bakfile in "$bakdir"/*.desktop.bak; do name=$(basename "${bakfile%.bak}") || continue origfile="$user/$name" [ -f "$origfile" ] && continue [ -f "$pacman_global/$name" ] || [ -f "$pacman_local/$name" ] || [ -f "$flatpak_global/$name" ] || [ -f "$flatpak_local/$name" ] || [ -f "$snap/$name" ] && mv "$bakfile" "$origfile" done } existing_dirs=() for dir in "$user" "$pacman_global" "$pacman_local" "$flatpak_global" "$flatpak_local" "$snap"; do [ -d "$dir" ] && existing_dirs=("${existing_dirs[@]}" "$dir") done detectfiles fixfiles 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[@]}")