frontend 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #!/bin/sh
  2. # Uncomment the following line to get debug info
  3. #set -x
  4. SELF="$(basename ${0})"
  5. LAUNCHER_FILE="$HOME/Launchers/launcher.txt"
  6. FRONTEND_FILE="$HOME/.frontend"
  7. PREVENT_LAUNCHER_FILE="/mnt/prevent_launcher"
  8. DISABLE_FRONTEND_FILE="/mnt/disable_frontend"
  9. DEFAULT_FRONTEND=retrofe
  10. LOCK_FILE="/var/lock/frontend.lock"
  11. REBOOTING_FILE="/run/rebooting"
  12. LAST_OPK_FILE="/mnt/last_opk"
  13. # Convert old launcher file to new frontend
  14. if [ -f "${LAUNCHER_FILE}" ]; then
  15. mv "${LAUNCHER_FILE}" "${FRONTEND_FILE}"
  16. rm -rf $(dirname "${LAUNCHER_FILE}") 2>/dev/null
  17. fi
  18. # Convert old prevent launcher file to new frontend disable file
  19. if [ -f "${PREVENT_LAUNCHER_FILE}" ]; then
  20. mv "${PREVENT_LAUNCHER_FILE}" "${DISABLE_FRONTEND_FILE}"
  21. fi
  22. usage() {
  23. >&2 echo "Usage: ${SELF} init"
  24. >&2 echo " ${SELF} get"
  25. >&2 echo " ${SELF} set gmenu2x|retrofe|none"
  26. exit 1
  27. }
  28. set_frontend() {
  29. mkdir -p "$(dirname "$FRONTEND_FILE")"
  30. local frontend=$(get_frontend)
  31. local new_frontend="${1}"
  32. echo "Setting frontend: ${new_frontend}"
  33. if [ "${new_frontend}" = "none" ]; then
  34. touch "${DISABLE_FRONTEND_FILE}"
  35. else
  36. rm -f "${DISABLE_FRONTEND_FILE}"
  37. fi
  38. if [ "${frontend}" != "none" ]; then
  39. pkill "${frontend}"
  40. fi
  41. echo "${new_frontend}" > "${FRONTEND_FILE}"
  42. }
  43. get_frontend() {
  44. local frontend=$(cat "${FRONTEND_FILE}" 2>/dev/null | head -1)
  45. # Check if not empty
  46. if [ "x${frontend}" = "x" ]; then
  47. frontend="${DEFAULT_FRONTEND}"
  48. echo "${frontend}" > "${FRONTEND_FILE}"
  49. fi
  50. # Return frontend name
  51. echo "${frontend}"
  52. }
  53. init_frontend() {
  54. if [ -f "${LOCK_FILE}" ]; then
  55. >&2 echo "${LOCK_FILE} already exists"
  56. exit 1
  57. fi
  58. touch "${LOCK_FILE}"
  59. # Then loop to launch the frontend indefinitely
  60. while true; do
  61. # Check if frontend disable file is present
  62. if [ -f "${DISABLE_FRONTEND_FILE}" ]; then
  63. echo "${DISABLE_FRONTEND_FILE} file found, not starting frontend"
  64. sleep 5
  65. else
  66. # Umount any remaining OPK, if any
  67. umount /opk >/dev/null 2>&1
  68. rm -f "${LAST_OPK_FILE}"
  69. # Launch selected frontend
  70. local frontend="$(get_frontend)"
  71. case "${frontend}" in
  72. gmenu2x|retrofe)
  73. "${frontend}"&
  74. ;;
  75. none)
  76. echo "no frontend"
  77. sleep 5
  78. ;;
  79. *)
  80. DEFAULT_FRONTEND=retrofe
  81. echo "Unrecognized frontend: $frontend, setting $DEFAULT_FRONTEND"
  82. set_frontend "${DEFAULT_FRONTEND}"
  83. ;;
  84. esac
  85. # Record the PID into a file, wait for the process to
  86. # terminate and erase the recorded PID
  87. pid record $!
  88. wait $!
  89. pid erase
  90. # In case retrofe/opkrun quits with errors, clear graphic VT
  91. termfix_all
  92. # In case retrofe/opkrun quits with errors, reset default key mapping
  93. keymap default
  94. fi
  95. # Prevent 100% CPU usage
  96. sleep 0.5
  97. # Exit if console rebooting
  98. if [ -f "${REBOOTING_FILE}" ]; then
  99. break
  100. fi
  101. done
  102. # Remove lock file and exit
  103. rm "${LOCK_FILE}"
  104. }
  105. # Check number of arguments
  106. if [ ${#} -lt 1 -o ${#} -gt 2 ]; then
  107. usage
  108. fi
  109. case "${1}" in
  110. set)
  111. if [ ${#} -ne 2 ]; then
  112. usage
  113. fi
  114. set_frontend "${2}"
  115. ;;
  116. get)
  117. if [ ${#} -ne 1 ]; then
  118. usage
  119. fi
  120. get_frontend
  121. ;;
  122. init)
  123. if [ ${#} -ne 1 ]; then
  124. usage
  125. fi
  126. init_frontend
  127. ;;
  128. *)
  129. usage
  130. ;;
  131. esac
  132. exit 0