4
0

update_jk2.sh 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. #!/bin/bash
  2. # LinuxGSM update_jk2.sh module
  3. # Author: Daniel Gibbs
  4. # Contributors: https://linuxgsm.com/contrib
  5. # Website: https://linuxgsm.com
  6. # Description: Handles updating of Jedi Knight 2 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" "nohash"
  11. fn_dl_extract "${tmpdir}" "${remotebuildfilename}" "${serverfiles}/GameData" "linux-amd64"
  12. fn_clear_tmp
  13. }
  14. fn_update_localbuild() {
  15. # Gets local build info.
  16. fn_print_dots "Checking local build: ${remotelocation}"
  17. # Uses log file to get local build.
  18. localbuild=$(grep "\"version\"" "${consolelogdir}"/* 2> /dev/null | sed 's/.*://' | awk '{print $1}' | head -n 1 | sed 's/v//')
  19. if [ -z "${localbuild}" ]; then
  20. fn_print_error "Checking local build: ${remotelocation}: missing local build info"
  21. fn_script_log_error "Missing local build info"
  22. fn_script_log_error "Set localbuild to 0"
  23. localbuild="0"
  24. else
  25. fn_print_ok "Checking local build: ${remotelocation}"
  26. fn_script_log_pass "Checking local build"
  27. fi
  28. }
  29. fn_update_remotebuild() {
  30. # Gets remote build info.
  31. apiurl="https://api.github.com/repos/mvdevs/jk2mv/releases/latest"
  32. remotebuildresponse=$(curl -s "${apiurl}")
  33. remotebuildfilename=$(echo "${remotebuildresponse}" | jq -r '.assets[]|select(.browser_download_url | contains("dedicated.zip")) | .name')
  34. remotebuildurl=$(echo "${remotebuildresponse}" | jq -r '.assets[]|select(.browser_download_url | contains("dedicated.zip")) | .browser_download_url')
  35. remotebuild=$(echo "${remotebuildresponse}" | jq -r '.tag_name')
  36. if [ "${firstcommandname}" != "INSTALL" ]; then
  37. fn_print_dots "Checking remote build: ${remotelocation}"
  38. # Checks if remotebuild variable has been set.
  39. if [ -z "${remotebuild}" ] || [ "${remotebuild}" == "null" ]; then
  40. fn_print_fail "Checking remote build: ${remotelocation}"
  41. fn_script_log_fail "Checking remote build"
  42. core_exit.sh
  43. else
  44. fn_print_ok "Checking remote build: ${remotelocation}"
  45. fn_script_log_pass "Checking remote build"
  46. fi
  47. else
  48. # Checks if remotebuild variable has been set.
  49. if [ -z "${remotebuild}" ] || [ "${remotebuild}" == "null" ]; then
  50. fn_print_failure "Unable to get remote build"
  51. fn_script_log_fail "Unable to get remote build"
  52. core_exit.sh
  53. fi
  54. fi
  55. }
  56. fn_update_compare() {
  57. fn_print_dots "Checking for update: ${remotelocation}"
  58. # Update has been found or force update.
  59. if [ "${localbuild}" != "${remotebuild}" ] || [ "${forceupdate}" == "1" ]; then
  60. # Create update lockfile.
  61. date '+%s' > "${lockdir:?}/update.lock"
  62. fn_print_ok_nl "Checking for update: ${remotelocation}"
  63. fn_print "\n"
  64. fn_print_nl "${bold}${underline}Update${default} available"
  65. fn_print_nl "* Local build: ${red}${localbuild}${default}"
  66. fn_print_nl "* Remote build: ${green}${remotebuild}${default}"
  67. if [ -n "${branch}" ]; then
  68. fn_print_nl "* Branch: ${branch}"
  69. fi
  70. if [ -f "${rootdir}/.dev-debug" ]; then
  71. fn_print_nl "Remote build info"
  72. fn_print_nl "* apiurl: ${apiurl}"
  73. fn_print_nl "* remotebuildfilename: ${remotebuildfilename}"
  74. fn_print_nl "* remotebuildurl: ${remotebuildurl}"
  75. fn_print_nl "* remotebuild: ${remotebuild}"
  76. fi
  77. fn_print "\n"
  78. fn_script_log_info "Update available"
  79. fn_script_log_info "Local build: ${localbuild}"
  80. fn_script_log_info "Remote build: ${remotebuild}"
  81. if [ -n "${branch}" ]; then
  82. fn_script_log_info "Branch: ${branch}"
  83. fi
  84. fn_script_log_info "${localbuild} > ${remotebuild}"
  85. if [ "${commandname}" == "UPDATE" ]; then
  86. date +%s > "${lockdir:?}/last-updated.lock"
  87. unset updateonstart
  88. check_status.sh
  89. # If server stopped.
  90. if [ "${status}" == "0" ]; then
  91. fn_update_dl
  92. if [ "${localbuild}" == "0" ]; then
  93. exitbypass=1
  94. command_start.sh
  95. fn_firstcommand_reset
  96. exitbypass=1
  97. fn_sleep_time_5
  98. command_stop.sh
  99. fn_firstcommand_reset
  100. fi
  101. # If server started.
  102. else
  103. fn_print_restart_warning
  104. exitbypass=1
  105. command_stop.sh
  106. fn_firstcommand_reset
  107. exitbypass=1
  108. fn_update_dl
  109. exitbypass=1
  110. command_start.sh
  111. fn_firstcommand_reset
  112. fi
  113. unset exitbypass
  114. alert="update"
  115. elif [ "${commandname}" == "CHECK-UPDATE" ]; then
  116. alert="check-update"
  117. fi
  118. alert.sh
  119. else
  120. fn_print_ok_nl "Checking for update: ${remotelocation}"
  121. fn_print "\n"
  122. fn_print_nl "${bold}${underline}No update${default} available"
  123. fn_print_nl "* Local build: ${green}${localbuild}${default}"
  124. fn_print_nl "* Remote build: ${green}${remotebuild}${default}"
  125. if [ -n "${branch}" ]; then
  126. fn_print_nl "* Branch: ${branch}"
  127. fi
  128. fn_print "\n"
  129. fn_script_log_info "No update available"
  130. fn_script_log_info "Local build: ${localbuild}"
  131. fn_script_log_info "Remote build: ${remotebuild}"
  132. if [ -n "${branch}" ]; then
  133. fn_script_log_info "Branch: ${branch}"
  134. fi
  135. if [ -f "${rootdir}/.dev-debug" ]; then
  136. fn_print_nl "Remote build info"
  137. fn_print_nl "* apiurl: ${apiurl}"
  138. fn_print_nl "* remotebuildfilename: ${remotebuildfilename}"
  139. fn_print_nl "* remotebuildurl: ${remotebuildurl}"
  140. fn_print_nl "* remotebuild: ${remotebuild}"
  141. fi
  142. fi
  143. }
  144. # The location where the builds are checked and downloaded.
  145. remotelocation="github.com"
  146. if [ "${firstcommandname}" == "INSTALL" ]; then
  147. fn_update_remotebuild
  148. fn_update_dl
  149. else
  150. update_steamcmd.sh
  151. fn_print_dots "Checking for update"
  152. fn_print_dots "Checking for update: ${remotelocation}"
  153. fn_script_log_info "Checking for update: ${remotelocation}"
  154. fn_update_localbuild
  155. fn_update_remotebuild
  156. fn_update_compare
  157. fi