qubes.GetAppmenus 2.0 KB

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