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