qubes.GetAppmenus 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. # shellcheck disable=SC1090
  13. . "$i" >/dev/null
  14. fi
  15. done
  16. if [ -z "$XDG_DATA_HOME" ]; then
  17. XDG_DATA_HOME="$HOME/.local/share"
  18. fi
  19. if [ -z "$XDG_DATA_DIRS" ]; then
  20. XDG_DATA_DIRS="/usr/local/share/:/usr/share/"
  21. fi
  22. # if read fails for some reason, default to full
  23. persistence=$(qubesdb-read /qubes-vm-persistence || echo full)
  24. rw_devno=$(stat -c %D /rw)
  25. apps_dirs_to_consider=( "$XDG_DATA_HOME" )
  26. old_IFS="$IFS"
  27. IFS=:
  28. # shellcheck disable=SC2206
  29. apps_dirs_to_consider+=( $XDG_DATA_DIRS )
  30. IFS="$old_IFS"
  31. apps_dirs=()
  32. for dir in "${apps_dirs_to_consider[@]}"; do
  33. if [ "$persistence" = "full" ]; then
  34. apps_dirs+=( "$dir/applications" )
  35. elif [ "$persistence" = "rw-only" ] && \
  36. [ "$(stat -c %D "$dir")" = "$rw_devno" ]; then
  37. apps_dirs+=( "$dir/applications" )
  38. fi
  39. done
  40. if [ "${#apps_dirs[@]}" -eq "0" ]; then
  41. # nothing to send, exit early to not let `find` browse the current
  42. # directory
  43. exit 0
  44. fi
  45. find "${apps_dirs[@]}" -name '*.desktop' -print0 2>/dev/null | \
  46. xargs -0 awk '
  47. BEGINFILE { entry="" }
  48. /^\[/ { if (tolower($0) != "\[desktop entry\]") nextfile }
  49. /^Exec=/ { entry = entry FILENAME ":Exec=qubes-desktop-run " FILENAME "\n"; next }
  50. /^NoDisplay *= *true$/ { entry=""; nextfile }
  51. /=/ { entry = entry FILENAME ":" $0 "\n" }
  52. ENDFILE { print entry }
  53. ' 2> /dev/null