Compare commits
3 Commits
3140881902
...
3a669f0664
Author | SHA1 | Date | |
---|---|---|---|
3a669f0664 | |||
a0fd0f4856 | |||
fb3b3e1cf5 |
|
@ -1,77 +1,120 @@
|
||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
|
# Exit out if the same script is already running in the background
|
||||||
pidof -sq -o %PPID -x "$(basename "$0")" && exit
|
pidof -sq -o %PPID -x "$(basename "$0")" && exit
|
||||||
datastore="$HOME"/.local/share/fix-zombie-appicons
|
|
||||||
bakdir="$datastore"/backup
|
|
||||||
data="$datastore"/tweaked-desktop-files
|
|
||||||
|
|
||||||
# Directories where desktop files are stored
|
# Setting read-only variables
|
||||||
user="$HOME"/.local/share/applications
|
declare -r datadir="$HOME"/.local/share/fix-zombie-appicons
|
||||||
pacman_global=/usr/share/applications
|
declare -r backup_dir="$datadir"/backup
|
||||||
pacman_local=/usr/local/share/applications
|
declare -r datafile="$datadir"/tweaked-desktop-files
|
||||||
flatpak_global=/var/lib/flatpak/exports/share/applications
|
|
||||||
flatpak_local="$HOME"/.local/share/flatpak/exports/share/applications
|
|
||||||
snap=/var/lib/snapd/desktop/applications
|
|
||||||
|
|
||||||
mkdir -p "$bakdir" "$user" "$flatpak_local" ||
|
# Desktop files stored here will take precedence over the other ones
|
||||||
{ echo "failed to make directoris $bakdir, $user & $flatpak_local"; exit 1; }
|
declare -r home_local_applications_dir="$HOME/.local/share/applications"
|
||||||
|
|
||||||
detectfiles() {
|
# Go through each item in $XDG_DATA_DIRS to find all other directories
|
||||||
for file in "$user"/*.desktop; do
|
# except home_local_applications_dir where desktop files are stored, and
|
||||||
name=$(basename "$file") || continue
|
# save these to other_applications_dirs
|
||||||
[ -d "$file" ] || grep -xq "$name" "$data" && continue
|
while read -r dir; do
|
||||||
[ -f "$pacman_global/$name" ] ||
|
full_dir_path=${dir%/}/applications
|
||||||
[ -f "$pacman_local/$name" ] ||
|
other_applications_dirs+=("$full_dir_path")
|
||||||
[ -f "$flatpak_global/$name" ] ||
|
|
||||||
[ -f "$flatpak_local/$name" ] ||
|
# Directories inside user's home directory
|
||||||
[ -f "$snap/$name" ] &&
|
[[ "$full_dir_path" = "$HOME"/* ]] && dirs_inside_user_home+=("$full_dir_path")
|
||||||
echo "$name" >> "$data"
|
|
||||||
|
done < <(echo "$XDG_DATA_DIRS" | tr ':' '\n' | sort -u | grep -vFx "$home_local_applications_dir")
|
||||||
|
|
||||||
|
declare -r other_applications_dirs dirs_inside_user_home
|
||||||
|
|
||||||
|
# Create tempfile which is used by the fix_desktop_files function
|
||||||
|
tempfile="$(mktemp)"
|
||||||
|
trap 'rm $tempfile' EXIT # Delete tempfile when the script exits
|
||||||
|
declare -r tempfile
|
||||||
|
|
||||||
|
# This function will check for new desktop files in home_local_applications_dir and
|
||||||
|
# record them in datafile if the same file is found in any of the other_applications_dirs.
|
||||||
|
detect_new_desktop_files() {
|
||||||
|
for file in "$home_local_applications_dir"/*.desktop; do
|
||||||
|
# This is added as a failsafe to ensure its a file and not a directory
|
||||||
|
[ -d "$file" ] && continue
|
||||||
|
filename=$(basename "$file")
|
||||||
|
# Continue looping if the filename is already recorded in the datafile
|
||||||
|
grep -xq "$filename" "$datafile" && continue
|
||||||
|
# If the same file is present in any of the other_applications_dirs then add its filename to datafile
|
||||||
|
for dir in "${other_applications_dirs[@]}"; do
|
||||||
|
[ -f "$dir/$filename" ] && echo "$filename" >> "$datafile" && break
|
||||||
|
done
|
||||||
done
|
done
|
||||||
}
|
}
|
||||||
|
|
||||||
fixfiles() {
|
# The following function will:
|
||||||
lastmod=$(stat -c "%Y" "$data")
|
# 1. Go through all the files recorded in datafile and move them to backup_dir if
|
||||||
tmp="$(mktemp)"
|
# the same file is not found in other_applications_dirs anymore.
|
||||||
trap 'rm $tmp' EXIT
|
# 2. Go through all the bakked up files in backup_dir and move them back to
|
||||||
[ -f "$data" ] && cp "$data" "$tmp" && copied='true'
|
# its original_file_path if the same file is present in any of
|
||||||
lineno=0
|
# the other_applications_dirs. This will not overwrite if a new
|
||||||
[ "$copied" = 'true' ] && while IFS= read -r name; do
|
# file already exists at original_file_path.
|
||||||
((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 -u "$tmp" > "$data"
|
fix_desktop_files() {
|
||||||
|
# This is added as a failsafe to avoid overwriting datafile in case the file got edited during this period
|
||||||
|
local -r last_modified_time=$(stat -c "%Y" "$datafile")
|
||||||
|
# Copy datafile to tempfile because its not possible to directly edit the file inside a while-read loop
|
||||||
|
[ -f "$datafile" ] && cp "$datafile" "$tempfile" && local -r datafile_copied='true'
|
||||||
|
local -i line_number=0
|
||||||
|
|
||||||
for bakfile in "$bakdir"/*.desktop.bak; do
|
# Loop through each line in datafile
|
||||||
name=$(basename "${bakfile%.bak}") || continue
|
[ "$datafile_copied" = 'true' ] && while IFS='' read -r filename; do
|
||||||
origfile="$user/$name"
|
line_number+=1
|
||||||
[ -f "$origfile" ] && continue
|
file="$home_local_applications_dir/$filename"
|
||||||
[ -f "$pacman_global/$name" ] ||
|
# If the file don't exist anymore then remove the line from datafile (as tempfile) and continue looping
|
||||||
[ -f "$pacman_local/$name" ] ||
|
[ -f "$file" ] || { sed -i "${line_number}d" "$tempfile" && line_number+=-1 ; continue; }
|
||||||
[ -f "$flatpak_global/$name" ] ||
|
|
||||||
[ -f "$flatpak_local/$name" ] ||
|
# If the same file is not present in any of the other_applications_dirs then move the file to backup_dir
|
||||||
[ -f "$snap/$name" ] &&
|
unset file_exists
|
||||||
mv "$bakfile" "$origfile"
|
for dir in "${other_applications_dirs[@]}"; do
|
||||||
|
[ -f "$dir/$filename" ] && file_exists='true' && break
|
||||||
|
done
|
||||||
|
[ "$file_exists" != 'true' ] && mv "$file" "$backup_dir/$filename.bak"
|
||||||
|
done < "$datafile"
|
||||||
|
|
||||||
|
# Write the contents from tempfile back to datafile after sorting and removing duplicate lines
|
||||||
|
[ "$datafile_copied" = 'true' ] && [ "$(stat -c '%Y' "$datafile")" = "$last_modified_time" ] && sort -u "$tempfile" > "$datafile"
|
||||||
|
|
||||||
|
for bakfile in "$backup_dir"/*.desktop.bak; do
|
||||||
|
filename=$(basename "${bakfile%.bak}")
|
||||||
|
original_file_path="$home_local_applications_dir/$filename"
|
||||||
|
|
||||||
|
# Continue looping if a new file is already present at the original_file_path,
|
||||||
|
# this will prevent the backup from overwriting the new file
|
||||||
|
[ -f "$original_file_path" ] && continue
|
||||||
|
|
||||||
|
# If the same bakked up file is present in any of the other_applications_dirs then move it back to its original_file_path
|
||||||
|
for dir in "${other_applications_dirs[@]}"; do
|
||||||
|
[ -f "$dir/$filename" ] && mv "$bakfile" "$original_file_path" && break
|
||||||
|
done
|
||||||
done
|
done
|
||||||
}
|
}
|
||||||
|
|
||||||
for dir in "$user" "$pacman_global" "$pacman_local" "$flatpak_global" "$flatpak_local" "$snap"; do
|
# Create some directories in the user's home directory if they don't already exist
|
||||||
[ -d "$dir" ] && existing_dirs+=("$dir")
|
mkdir -p "$datadir" "$backup_dir" "$home_local_applications_dir" ||
|
||||||
|
{ echo "failed to make directories $datadir, $backup_dir & $home_local_applications_dir"; exit 1; }
|
||||||
|
|
||||||
|
# Try to create dirs_inside_user_home if they don't already exist
|
||||||
|
[ ${#dirs_inside_user_home[@]} -gt 0 ] && mkdir -p "${dirs_inside_user_home[@]}"
|
||||||
|
|
||||||
|
# Check which directories currently exist on the user's system so that inotifywait can monitor them for changes
|
||||||
|
for dir in "$home_local_applications_dir" "${other_applications_dirs[@]}"; do
|
||||||
|
[ -d "$dir" ] && currently_existing_dirs+=("$dir")
|
||||||
done
|
done
|
||||||
|
|
||||||
detectfiles
|
# Both functions will be run once when the script first starts
|
||||||
fixfiles
|
detect_new_desktop_files
|
||||||
|
fix_desktop_files
|
||||||
|
|
||||||
while read -r line; do
|
# Inotifywait monitors currently_existing_dirs and the functions are run each time when there is a change in relevant directories
|
||||||
if [ "$line" = "$user/" ]; then
|
while IFS='' read -r line; do
|
||||||
detectfiles
|
if [ "$line" = "$home_local_applications_dir/" ]; then
|
||||||
|
detect_new_desktop_files
|
||||||
else
|
else
|
||||||
fixfiles
|
fix_desktop_files
|
||||||
fi
|
fi
|
||||||
done < <(inotifywait -qm --format '%w' --include '\.desktop$' -e 'move,move_self,create,delete,delete_self,unmount' "${existing_dirs[@]}")
|
done < <(inotifywait -qm --format '%w' --include '\.desktop$' -e 'move,move_self,create,delete,delete_self,unmount' "${currently_existing_dirs[@]}")
|
||||||
|
|
Loading…
Reference in New Issue
Block a user