qubes.GetAppmenus 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #!/bin/bash
  2. # shellcheck disable=SC2016
  3. # send .desktop files from directories persisting across VM restarts, specifically:
  4. # - any directory in case of "full" persistence
  5. # - directories stored on /rw in case of "rw-only" persistence
  6. # - nothing, otherwise
  7. # Reload scripts in /etc/profile.d/, in case they register additional
  8. # directories in XDG_DATA_DIRS and we forgot them
  9. # (e.g. because we are running under sudo).
  10. for i in /etc/profile.d/*.sh ; do
  11. if [ -r "$i" ]; then
  12. . "$i" >/dev/null
  13. fi
  14. done
  15. if [ -z "$XDG_DATA_HOME" ]; then
  16. XDG_DATA_HOME="$HOME/.local/share"
  17. fi
  18. if [ -z "$XDG_DATA_DIRS" ]; then
  19. XDG_DATA_DIRS="/usr/local/share/:/usr/share/"
  20. fi
  21. # if read fails for some reason, default to full
  22. persistence=$(qubesdb-read /qubes-vm-persistence || echo full)
  23. rw_devno=$(stat -c %D /rw)
  24. apps_dirs_to_consider=( "$XDG_DATA_HOME" )
  25. old_IFS="$IFS"
  26. IFS=:
  27. # shellcheck disable=SC2206
  28. apps_dirs_to_consider+=( $XDG_DATA_DIRS )
  29. IFS="$old_IFS"
  30. apps_dirs=()
  31. for dir in "${apps_dirs_to_consider[@]}"; do
  32. if [ "$persistence" = "full" ]; then
  33. apps_dirs+=( "$dir/applications" )
  34. elif [ "$persistence" = "rw-only" ] && \
  35. [ "$(stat -c %D "$dir")" = "$rw_devno" ]; then
  36. apps_dirs+=( "$dir/applications" )
  37. fi
  38. done
  39. if [ "${#apps_dirs[@]}" -eq "0" ]; then
  40. # nothing to send, exit early to not let `find` browse the current
  41. # directory
  42. exit 0
  43. fi
  44. find "${apps_dirs[@]}" -name '*.desktop' -print0 2>/dev/null | \
  45. xargs -0 awk '
  46. BEGINFILE { entry="" }
  47. /^\[/ { if (tolower($0) != "\[desktop entry\]") nextfile }
  48. /^Exec=/ { entry = entry FILENAME ":Exec=qubes-desktop-run " FILENAME "\n"; next }
  49. /^NoDisplay *= *true$/ { entry=""; nextfile }
  50. /=/ { entry = entry FILENAME ":" $0 "\n" }
  51. ENDFILE { print entry }
  52. ' 2> /dev/null