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