update_xnt.sh 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. #!/bin/bash
  2. # LinuxGSM command_ut99.sh module
  3. # Author: Daniel Gibbs
  4. # Contributors: https://linuxgsm.com/contrib
  5. # Website: https://linuxgsm.com
  6. # Description: Handles updating of Unreal Tournament 99 servers.
  7. moduleselfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")"
  8. fn_update_dl() {
  9. # Download and extract files to serverfiles.
  10. fn_fetch_file "${remotebuildurl}" "" "" "" "${tmpdir}" "${remotebuildfilename}" "nochmodx" "norun" "force" "${remotebuildhash}"
  11. fn_dl_extract "${tmpdir}" "${remotebuildfilename}" "${serverfiles}" "Xonotic"
  12. fn_clear_tmp
  13. }
  14. fn_update_localbuild() {
  15. # Gets local build info.
  16. fn_print_dots "Checking local build: ${remotelocation}"
  17. # Send version command to Xonotic server.
  18. tmux -L "${socketname}" send-keys -t "${sessionname}" "version\r" > /dev/null 2>&1
  19. fn_sleep_time_1
  20. # Uses log file to get local build.
  21. localbuild=$(grep "SVQC version: xonotic-v" "${consolelogdir}"/* 2> /dev/null | tail -1 | sed 's/.*SVQC version: \(xonotic-v[0-9.]*\).*/\1/' | tr -d '\000-\011\013-\037')
  22. if [ -z "${localbuild}" ]; then
  23. fn_print_error "Checking local build: ${remotelocation}: missing local build info"
  24. fn_script_log_error "Missing local build info"
  25. fn_script_log_error "Set localbuild to 0"
  26. localbuild="0"
  27. else
  28. fn_print_ok "Checking local build: ${remotelocation}"
  29. fn_script_log_pass "Checking local build"
  30. fi
  31. }
  32. fn_update_remotebuild() {
  33. # Gets remote build info.
  34. apiurl="https://api.github.com/repos/xonotic/xonotic/tags"
  35. remotebuildresponse=$(curl -s "${apiurl}")
  36. remotebuildtag=$(echo "${remotebuildresponse}" | jq -r '.[0].name')
  37. remotebuildfilename=$(echo "${remotebuildtag}" | tr -d 'v')
  38. remotebuildfilename="${remotebuildfilename}.zip"
  39. remotebuildurl="https://dl.xonotic.org/${remotebuildfilename}"
  40. remotebuildversion="${remotebuildtag}"
  41. if [ "${firstcommandname}" != "INSTALL" ]; then
  42. fn_print_dots "Checking remote build: ${remotelocation}"
  43. # Checks if remotebuildversion variable has been set.
  44. if [ -z "${remotebuildversion}" ] || [ "${remotebuildversion}" == "null" ]; then
  45. fn_print_fail "Checking remote build: ${remotelocation}"
  46. fn_script_log_fail "Checking remote build"
  47. core_exit.sh
  48. else
  49. fn_print_ok "Checking remote build: ${remotelocation}"
  50. fn_script_log_pass "Checking remote build"
  51. fi
  52. else
  53. # Checks if remotebuild variable has been set.
  54. if [ -z "${remotebuildversion}" ] || [ "${remotebuildversion}" == "null" ]; then
  55. fn_print_failure "Unable to get remote build"
  56. fn_script_log_fail "Unable to get remote build"
  57. core_exit.sh
  58. fi
  59. fi
  60. }
  61. fn_update_compare() {
  62. fn_print_dots "Checking for update: ${remotelocation}"
  63. # Update has been found or force update.
  64. if [ "${localbuild}" != "${remotebuildversion}" ] || [ "${forceupdate}" == "1" ]; then
  65. # Create update lockfile.
  66. date '+%s' > "${lockdir:?}/update.lock"
  67. fn_print_ok_nl "Checking for update: ${remotelocation}"
  68. echo -en "\n"
  69. echo -e "Update available"
  70. echo -e "* Local build: ${red}${localbuild}${default}"
  71. echo -e "* Remote build: ${green}${remotebuildversion}${default}"
  72. if [ -n "${branch}" ]; then
  73. echo -e "* Branch: ${branch}"
  74. fi
  75. if [ -f "${rootdir}/.dev-debug" ]; then
  76. echo -e "Remote build info"
  77. echo -e "* apiurl: ${apiurl}"
  78. echo -e "* remotebuildfilename: ${remotebuildfilename}"
  79. echo -e "* remotebuildurl: ${remotebuildurl}"
  80. echo -e "* remotebuildversion: ${remotebuildversion}"
  81. fi
  82. echo -en "\n"
  83. fn_script_log_info "Update available"
  84. fn_script_log_info "Local build: ${localbuild}"
  85. fn_script_log_info "Remote build: ${remotebuildversion}"
  86. if [ -n "${branch}" ]; then
  87. fn_script_log_info "Branch: ${branch}"
  88. fi
  89. fn_script_log_info "${localbuild} > ${remotebuildversion}"
  90. if [ "${commandname}" == "UPDATE" ]; then
  91. date +%s > "${lockdir}/last-updated.lock"
  92. unset updateonstart
  93. check_status.sh
  94. # If server stopped.
  95. if [ "${status}" == "0" ]; then
  96. fn_update_dl
  97. if [ "${localbuild}" == "0" ]; then
  98. exitbypass=1
  99. command_start.sh
  100. fn_firstcommand_reset
  101. exitbypass=1
  102. fn_sleep_time_5
  103. command_stop.sh
  104. fn_firstcommand_reset
  105. fi
  106. # If server started.
  107. else
  108. fn_print_restart_warning
  109. exitbypass=1
  110. command_stop.sh
  111. fn_firstcommand_reset
  112. exitbypass=1
  113. fn_update_dl
  114. exitbypass=1
  115. command_start.sh
  116. fn_firstcommand_reset
  117. fi
  118. unset exitbypass
  119. alert="update"
  120. elif [ "${commandname}" == "CHECK-UPDATE" ]; then
  121. alert="check-update"
  122. fi
  123. alert.sh
  124. else
  125. fn_print_ok_nl "Checking for update: ${remotelocation}"
  126. echo -en "\n"
  127. echo -e "No update available"
  128. echo -e "* Local build: ${green}${localbuild}${default}"
  129. echo -e "* Remote build: ${green}${remotebuildversion}${default}"
  130. if [ -n "${branch}" ]; then
  131. echo -e "* Branch: ${branch}"
  132. fi
  133. echo -en "\n"
  134. fn_script_log_info "No update available"
  135. fn_script_log_info "Local build: ${localbuild}"
  136. fn_script_log_info "Remote build: ${remotebuildversion}"
  137. if [ -n "${branch}" ]; then
  138. fn_script_log_info "Branch: ${branch}"
  139. fi
  140. if [ -f "${rootdir}/.dev-debug" ]; then
  141. echo -e "Remote build info"
  142. echo -e "* apiurl: ${apiurl}"
  143. echo -e "* remotebuildfilename: ${remotebuildfilename}"
  144. echo -e "* remotebuildurl: ${remotebuildurl}"
  145. echo -e "* remotebuildversion: ${remotebuildversion}"
  146. fi
  147. fi
  148. }
  149. # The location where the builds are checked and downloaded.
  150. remotelocation="github.com"
  151. if [ ! "$(command -v jq 2> /dev/null)" ]; then
  152. fn_print_fail_nl "jq is not installed"
  153. fn_script_log_fail "jq is not installed"
  154. core_exit.sh
  155. fi
  156. if [ "${firstcommandname}" == "INSTALL" ]; then
  157. fn_update_remotebuild
  158. fn_update_dl
  159. else
  160. fn_print_dots "Checking for update"
  161. fn_print_dots "Checking for update: ${remotelocation}"
  162. fn_script_log_info "Checking for update: ${remotelocation}"
  163. fn_update_localbuild
  164. fn_update_remotebuild
  165. fn_update_compare
  166. fi