instant_play 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #!/bin/sh
  2. # Uncomment the following line to get debug info
  3. #set -x
  4. SELF="$(basename ${0})"
  5. INSTANT_PLAY_FILE="/mnt/instant_play"
  6. RESUME_PLAY_FILE="/mnt/resume_play"
  7. LAST_OPK_FILE="/mnt/last_opk"
  8. PID_PATH="/var/run/pid_path"
  9. REBOOTING_FILE="/run/rebooting"
  10. usage() {
  11. >&2 echo "Usage: ${SELF} load"
  12. >&2 echo " ${SELF} save application args..."
  13. exit 1
  14. }
  15. # Check number of arguments
  16. if [ ${#} -lt 1 ]; then
  17. usage
  18. fi
  19. case ${1} in
  20. load)
  21. if [ ${#} -ne 1 ]; then
  22. usage
  23. fi
  24. # Umount any remaining OPK, if any
  25. umount /opk >/dev/null 2>&1
  26. # Mount last OPK, if any
  27. if [ -r "${LAST_OPK_FILE}" ]; then
  28. last_opk=$(cat "${LAST_OPK_FILE}")
  29. mount -t squashfs "${last_opk}" /opk
  30. fi
  31. # Remove unnecessary files
  32. rm -f "${RESUME_PLAY_FILE}"
  33. # Launch Previous Game if any
  34. if [ -f "${INSTANT_PLAY_FILE}" ]; then
  35. keymap resume
  36. echo -n "Found Instant Play file, restarting previous game with command: "
  37. cat "${INSTANT_PLAY_FILE}"
  38. mv "${INSTANT_PLAY_FILE}" "${RESUME_PLAY_FILE}"
  39. source "${RESUME_PLAY_FILE}"
  40. rm -f "${RESUME_PLAY_FILE}"
  41. keymap default
  42. termfix_all
  43. fi
  44. # Unmount last OPK, if any
  45. if [ -r "${LAST_OPK_FILE}" -a ! -f "${REBOOTING_FILE}" ]; then
  46. umount /opk
  47. rm "${LAST_OPK_FILE}"
  48. fi
  49. ;;
  50. save)
  51. if [ ${#} -lt 2 ]; then
  52. usage
  53. fi
  54. shift
  55. # Write quick load file args
  56. echo -n "" > "${INSTANT_PLAY_FILE}"
  57. printf "'" >> "${INSTANT_PLAY_FILE}"
  58. # First arg is prog name, forcing real path
  59. bin=$(printf %s "$1" | sed "s/'/'\\\\''/g")
  60. bin_name=$(basename "$bin")
  61. bin_path="$(cat $PID_PATH)"/"$bin_name"
  62. echo -n "$bin_path" >> "${INSTANT_PLAY_FILE}"
  63. printf "' " >> "${INSTANT_PLAY_FILE}"
  64. shift
  65. while [ "$#" != "0" ]
  66. do
  67. printf "'" >> "${INSTANT_PLAY_FILE}"
  68. printf %s "$1" | sed "s/'/'\\\\''/g" >> "${INSTANT_PLAY_FILE}"
  69. printf "' " >> "${INSTANT_PLAY_FILE}"
  70. shift
  71. done
  72. # Add the magic sauce to launch the process in background,
  73. # record the PID into a file, wait for the process to
  74. # terminate and erase the recorded PID
  75. cat << EOF >> "${INSTANT_PLAY_FILE}"
  76. &
  77. pid record \$!
  78. wait \$!
  79. pid erase
  80. EOF
  81. # Now terminate gracefully
  82. exec powerdown now
  83. ;;
  84. *)
  85. usage
  86. ;;
  87. esac
  88. exit 0