patcher-wrapper 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #!/bin/bash
  2. set -eu -o pipefail
  3. helpexit() {
  4. echo "$0 version action"
  5. exit 1
  6. }
  7. test -n "${1:-}" -a -n "${2:-}" || helpexit
  8. version=${1//.}
  9. action=$2
  10. GIT_URL="https://notabug.org/Krock/dawn.git"
  11. PATCHER_HOME="$HOME/.local/share/paimon-launcher/dawn"
  12. CACHE_TIME=5 # don't update patcher if it was updated within last 5 minutes
  13. TIMESTAMP_FILE="$PATCHER_HOME.timestamp"
  14. scripts_path=`cd "\`dirname \"$0\"\`"; echo "$PWD"`
  15. patcher_dir="$PATCHER_HOME/$version"
  16. patchsh="$patcher_dir/patch.sh"
  17. # clone or update patcher dir
  18. if [ ! -d "$PATCHER_HOME/.git" ]; then
  19. echo "Cloning patcher..."
  20. mkdir -p "$(dirname $PATCHER_HOME)"
  21. git clone $GIT_URL "$PATCHER_HOME"
  22. touch "$TIMESTAMP_FILE"
  23. else
  24. if [ ! -e "$TIMESTAMP_FILE" -o -n "`find \"$TIMESTAMP_FILE\" -mmin +$CACHE_TIME`" ]; then
  25. echo "Updating patcher..."
  26. git -C "$PATCHER_HOME" pull
  27. touch "$TIMESTAMP_FILE"
  28. fi
  29. fi
  30. get_sudo_content() {
  31. echo 'set -eu -o pipefail'
  32. echo "cd '"""$PWD"""'"
  33. sed "1,/DO NOT REMOVE START/!d" "$patchsh" | sed "s:^DIR=.*$:DIR='$patcher_dir':"
  34. sed '/# START OF SUDO DANGER ZONE/,/# END OF SUDO DANGER ZONE/!d' "$patchsh"
  35. }
  36. # unfortunately pkexec doesn't work with pipes like sudo, so we have no chance to execute script as is
  37. # we cut out the sudo part and executing it to test if superuser rights needed (exit code 42 then)
  38. # if so, we do the same part again with su rights provided by pkexec (or not)
  39. # after that we can start the script itself
  40. case $action in
  41. check)
  42. echo "SERVERS_BEGIN"
  43. sed '/servers=/,/EOF/!d;/EOF/d' "$patchsh"
  44. echo "SERVERS_END"
  45. # make script exit with code 42 in case it wants sudo
  46. check_script=`get_sudo_content | sed 's/ sudo / exit 42 # /g'`
  47. bash -c "$check_script"
  48. ;;
  49. block)
  50. block_script=`get_sudo_content`
  51. # User already accepted, so reply 'y'
  52. QUIET=1 "$scripts_path/sudo-wrapper" bash -c "$block_script" <<< 'y'
  53. ;;
  54. patch*)
  55. # User already accepted, so reply 'y'
  56. echo "Starting $patchsh"
  57. if [ "$action" = "patch-untested" ]
  58. then
  59. sed '0,/exit 1/{/exit 1/d}' "$patchsh" >"$patchsh.tmp"
  60. bash "$patchsh.tmp" <<< 'y'
  61. rm "$patchsh.tmp"
  62. else
  63. bash "$patchsh" <<< 'y'
  64. fi
  65. if [ -f "$patcher_dir/patch_anti_logincrash.sh" ]; then
  66. # User already accepted, so reply 'y'
  67. bash "$patcher_dir/patch_anti_logincrash.sh" <<< 'y'
  68. fi
  69. echo
  70. echo "Note: removing 'start' from launcher.bat to make Start/Stop button work properly"
  71. sed -i 's/^start\s\+\(Gen\)/\1/' launcher.bat
  72. ;;
  73. revert)
  74. bash "$patcher_dir/patch_revert.sh"
  75. ;;
  76. *)
  77. echo "unsupported action $action"
  78. helpexit
  79. ;;
  80. esac