Clean code and useful comments #20
@ -4,23 +4,26 @@
|
||||
pidof -sq -o %PPID -x "$(basename "$0")" && exit
|
||||
|
||||
# Setting read-only variables
|
||||
declare -r \
|
||||
datadir="$HOME"/.local/share/fix-zombie-appicons \
|
||||
backup_dir="$datadir"/backup \
|
||||
datafile="$datadir"/tweaked-desktop-files
|
||||
declare -r datadir="$HOME"/.local/share/fix-zombie-appicons
|
||||
declare -r backup_dir="$datadir"/backup
|
||||
declare -r datafile="$datadir"/tweaked-desktop-files
|
||||
|
||||
# Directories where desktop files are stored
|
||||
declare -r home_local_applications_dir="$HOME/.local/share/applications" \
|
||||
home_local_flatpak_applications_dir="$HOME/.local/share/flatpak/exports/share/applications"
|
||||
# Desktop files stored here will take precedence over the other ones
|
||||
declare -r home_local_applications_dir="$HOME/.local/share/applications"
|
||||
|
||||
# All the other directories except home_local_applications_dir where desktop files are stored
|
||||
declare -r other_applications_dirs=(
|
||||
'/usr/share/applications'
|
||||
'/usr/local/share/applications'
|
||||
'/var/lib/flatpak/exports/share/applications'
|
||||
"$home_local_flatpak_applications_dir"
|
||||
'/var/lib/snapd/desktop/applications'
|
||||
)
|
||||
# Go through each item in $XDG_DATA_DIRS to find all other directories
|
||||
# except home_local_applications_dir where desktop files are stored, and
|
||||
# save these to other_applications_dirs
|
||||
while read -r dir; do
|
||||
full_dir_path=${dir%/}/applications
|
||||
other_applications_dirs+=("$full_dir_path")
|
||||
|
||||
# Directories inside user's home directory
|
||||
[[ "$full_dir_path" = "$HOME"/* ]] && dirs_inside_user_home+=("$full_dir_path")
|
||||
|
||||
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)"
|
||||
@ -92,23 +95,26 @@ fix_desktop_files() {
|
||||
}
|
||||
|
||||
# Create some directories in the user's home directory if they don't already exist
|
||||
mkdir -p "$backup_dir" "$home_local_applications_dir" "$home_local_flatpak_applications_dir" ||
|
||||
{ echo "failed to make directories $backup_dir, $home_local_applications_dir & $home_local_flatpak_applications_dir"; exit 1; }
|
||||
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" ] && existing_dirs+=("$dir")
|
||||
[ -d "$dir" ] && currently_existing_dirs+=("$dir")
|
||||
done
|
||||
|
||||
# Both functions will be run once when the script first starts
|
||||
detect_new_desktop_files
|
||||
fix_desktop_files
|
||||
|
||||
# Inotifywait monitors existing_dirs and the functions are run each time when there is a change in relevant directories
|
||||
# Inotifywait monitors currently_existing_dirs and the functions are run each time when there is a change in relevant directories
|
||||
while IFS='' read -r line; do
|
||||
if [ "$line" = "$home_local_applications_dir/" ]; then
|
||||
detect_new_desktop_files
|
||||
else
|
||||
fix_desktop_files
|
||||
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