update_tm2020.sh 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #!/bin/bash
  2. # LinuxGSM update_tm2020.sh module
  3. # Author: Daniel Gibbs
  4. # Contributors: https://linuxgsm.com/contrib
  5. # Website: https://linuxgsm.com
  6. # Description: Handles updating of Trackmania 2020 servers.
  7. moduleselfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")"
  8. fn_update_dl() {
  9. fn_fetch_file "${remotebuildurl}" "" "" "" "${tmpdir}" "${remotebuildfilename}" "nochmodx" "norun" "force" "nohash"
  10. fn_dl_extract "${tmpdir}" "${remotebuildfilename}" "${serverfiles}"
  11. chmod +x "${executabledir}/${executable#./}"
  12. echo "${remotebuild}" > "${serverfiles}/.tm2020-build"
  13. fn_clear_tmp
  14. }
  15. fn_update_localbuild() {
  16. local mode="${1:-}"
  17. if [ "${mode}" != "silent" ]; then
  18. fn_print_dots "Checking local build: ${remotelocation}"
  19. fi
  20. localbuild=$(cat "${serverfiles}/.tm2020-build" 2> /dev/null)
  21. localbuild="${localbuild:-0}"
  22. if [ "${mode}" != "silent" ]; then
  23. fn_print_ok "Checking local build: ${remotelocation}"
  24. fi
  25. }
  26. fn_update_remotebuild() {
  27. remotebuildurl="https://nadeo-download.cdn.ubi.com/trackmania/TrackmaniaServer_Latest.zip"
  28. remotebuildfilename="TrackmaniaServer_Latest.zip"
  29. apiurl="${remotebuildurl}"
  30. remotebuildheader=$(curl -fsSIL --retry 3 "${remotebuildurl}" | tr -d '\r' | awk -F': ' 'tolower($1) == "last-modified" {print $2}' | tail -n 1)
  31. remotebuild=$(date -u -d "${remotebuildheader}" '+%Y-%m-%d %H:%M:%S' 2> /dev/null)
  32. if [ -z "${remotebuild}" ]; then
  33. fn_print_fail_nl "Unable to get the Trackmania 2020 server build date"
  34. fn_script_log_fail "Unable to get the Trackmania 2020 server build date"
  35. core_exit.sh
  36. fi
  37. }
  38. fn_update_compare() {
  39. fn_print_dots "Checking for update: ${remotelocation}"
  40. # Update has been found or force update.
  41. if [ "${localbuild}" != "${remotebuild}" ] || [ "${forceupdate}" == "1" ]; then
  42. # Create update lockfile.
  43. date '+%s' > "${lockdir:?}/update.lock"
  44. fn_print_ok_nl "Checking for update: ${remotelocation}"
  45. fn_print "\n"
  46. fn_print_nl "${bold}${underline}Update${default} available"
  47. fn_print_nl "* Local build: ${red}${localbuild}${default}"
  48. fn_print_nl "* Remote build: ${green}${remotebuild}${default}"
  49. if [ -n "${branch}" ]; then
  50. fn_print_nl "* Branch: ${branch}"
  51. fi
  52. if [ -f "${rootdir}/.dev-debug" ]; then
  53. fn_print_nl "Remote build info"
  54. fn_print_nl "* apiurl: ${apiurl}"
  55. fn_print_nl "* remotebuildfilename: ${remotebuildfilename}"
  56. fn_print_nl "* remotebuildurl: ${remotebuildurl}"
  57. fn_print_nl "* remotebuild: ${remotebuild}"
  58. fi
  59. fn_print "\n"
  60. fn_script_log_info "Update available"
  61. fn_script_log_info "Local build: ${localbuild}"
  62. fn_script_log_info "Remote build: ${remotebuild}"
  63. if [ -n "${branch}" ]; then
  64. fn_script_log_info "Branch: ${branch}"
  65. fi
  66. fn_script_log_info "${localbuild} > ${remotebuild}"
  67. if [ "${commandname}" == "UPDATE" ]; then
  68. # Keep track of the build before updating for alert purposes.
  69. previousbuild="${localbuild}"
  70. unset updateonstart
  71. fn_update_apply fn_update_dl
  72. # Refresh local build value after update (silent) so alerts show new version.
  73. fn_update_localbuild silent
  74. # Verify the update applied correctly unless forced update to same version.
  75. if [ "${localbuild}" != "${remotebuild}" ]; then
  76. # If forced update and version unchanged treat as acceptable; otherwise failure.
  77. if [ "${forceupdate}" != "1" ] || [ "${previousbuild}" = "${localbuild}" ]; then
  78. fn_script_log_error "Update verification failed: expected ${remotebuild}, got ${localbuild}"
  79. fn_print_fail_nl "Update verification failed: expected ${remotebuild}, got ${localbuild}"
  80. updatefailureexpected="${remotebuild}"
  81. updatefailuregot="${localbuild}"
  82. alert="update-failed"
  83. alert.sh
  84. core_exit.sh
  85. fi
  86. else
  87. fn_script_log_pass "Update verification success: ${previousbuild} -> ${localbuild}"
  88. fi
  89. unset exitbypass
  90. alert="update"
  91. elif [ "${commandname}" == "CHECK-UPDATE" ]; then
  92. alert="check-update"
  93. fi
  94. alert.sh
  95. else
  96. fn_print_ok_nl "Checking for update: ${remotelocation}"
  97. fn_print "\n"
  98. fn_print_nl "${bold}${underline}No update${default} available"
  99. fn_print_nl "* Local build: ${green}${localbuild}${default}"
  100. fn_print_nl "* Remote build: ${green}${remotebuild}${default}"
  101. if [ -n "${branch}" ]; then
  102. fn_print_nl "* Branch: ${branch}"
  103. fi
  104. fn_print "\n"
  105. fn_script_log_info "No update available"
  106. fn_script_log_info "Local build: ${localbuild}"
  107. fn_script_log_info "Remote build: ${remotebuild}"
  108. if [ -n "${branch}" ]; then
  109. fn_script_log_info "Branch: ${branch}"
  110. fi
  111. if [ -f "${rootdir}/.dev-debug" ]; then
  112. fn_print_nl "Remote build info"
  113. fn_print_nl "* apiurl: ${apiurl}"
  114. fn_print_nl "* remotebuildfilename: ${remotebuildfilename}"
  115. fn_print_nl "* remotebuildurl: ${remotebuildurl}"
  116. fn_print_nl "* remotebuild: ${remotebuild}"
  117. fi
  118. fi
  119. }
  120. # The location where the builds are checked and downloaded.
  121. remotelocation="nadeo-download.cdn.ubi.com"
  122. if [ "${firstcommandname}" == "INSTALL" ]; then
  123. fn_update_remotebuild
  124. fn_update_dl
  125. else
  126. fn_print_dots "Checking for update"
  127. fn_print_dots "Checking for update: ${remotelocation}"
  128. fn_script_log_info "Checking for update: ${remotelocation}"
  129. fn_update_localbuild
  130. fn_update_remotebuild
  131. fn_update_compare
  132. fi