perform_update.sh 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #!/usr/bin/env bash
  2. # Utility script to unzip an archive to $PWD and process HDiffPatch updates
  3. # md5 archive sanity checks must be done by the caller function/application
  4. #
  5. # Usage: ./perform_update.sh "/path/to/archive.zip"
  6. # Exit on error.
  7. set -e
  8. fatal() {
  9. echo
  10. for arg in "$@"; do
  11. echo " * $arg" >&2
  12. done
  13. echo
  14. exit 1
  15. }
  16. do_cleanup() {
  17. # Old patch files
  18. find . -type f -name "*.hdiff" | while read filename; do
  19. rm "$filename"
  20. done
  21. # Prevent deleting of the possibly necessary files in case of full update.
  22. rm -f "deletefiles.txt"
  23. rm -f "hdifffiles.txt"
  24. }
  25. # ======== Evaluated variables
  26. THIS_FILE=$(basename "$0")
  27. THIS_PATH=$(realpath "$(dirname "$0")")
  28. HDIFF_APP="${THIS_PATH}/HDiffPatch/$(uname)/hpatchz"
  29. ZIP_FILE="$1"
  30. # ======== Sanity checks
  31. [ ! -e "config.ini" ] && fatal \
  32. "Game information file not found."
  33. [ ! -e "$ZIP_FILE" ] && fatal \
  34. "Archive '${ZIP_FILE}' not found."
  35. chmod +x "$HDIFF_APP"
  36. # ======== Preparation
  37. do_cleanup
  38. # ======== Unzipping
  39. short_name=$(basename "${ZIP_FILE}")
  40. echo "--- Installing ${short_name} ... "
  41. unzip -o "${ZIP_FILE}"
  42. echo ""
  43. # ======== *.hdiff files
  44. set +e # to capture "$?"
  45. find . -type f -name "*.hdiff" | while read diffname; do
  46. realname="${diffname::-6}" # trim ".hdiff"
  47. if [ -e "$realname" ]; then
  48. echo "--- Patching file ${realname}"
  49. output=$("$HDIFF_APP" -f "$realname" "$diffname" "${realname}.tmp_hdiff")
  50. if [ $? -eq 0 ]; then
  51. mv -f "${realname}.tmp_hdiff" "$realname"
  52. else
  53. echo "$output"
  54. rm -f "${realname}.tmp_hdiff" # just in case
  55. fi
  56. else
  57. # let the game re-download
  58. echo "--- [WARNING] HDiff source not found: ${realname}"
  59. fi
  60. rm "$diffname"
  61. done
  62. set -e
  63. # ======== deletefiles.txt
  64. if [ -f "deletefiles.txt" ]; then
  65. while read -r filename; do
  66. filename=$(echo "${filename}" | tr -d '\r\n')
  67. # Safety check. File must be within the game directory.
  68. [ ! -f "${filename}" ] && continue;
  69. filepath_abs=$(readlink -nf "${PWD}/${filename}")
  70. case "$filepath_abs" in
  71. ("$PWD"/*)
  72. echo "--- Removing old file: ${filename}"
  73. rm -f "$filepath_abs"
  74. continue
  75. ;;
  76. esac
  77. echo "--- [WARNING] Attempt to remove: ${filename}"
  78. done < "deletefiles.txt"
  79. fi
  80. # ======== Final cleanup
  81. do_cleanup
  82. exit 0