update_mta.sh 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #!/bin/bash
  2. # LinuxGSM update_mta.sh function
  3. # Author: Daniel Gibbs
  4. # Website: https://linuxgsm.com
  5. # Description: Handles updating of Multi Theft Auto servers.
  6. local commandname="UPDATE"
  7. local commandaction="Update"
  8. local function_selfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")"
  9. fn_update_mta_dl(){
  10. fn_fetch_file "http://linux.mtasa.com/dl/${numversion}/multitheftauto_linux_x64-${fullversion}.tar.gz" "${tmpdir}" "multitheftauto_linux_x64-${fullversion}.tar.gz"
  11. mkdir "${tmpdir}/multitheftauto_linux_x64-${fullversion}"
  12. fn_dl_extract "${tmpdir}" "multitheftauto_linux_x64-${fullversion}.tar.gz" "${tmpdir}/multitheftauto_linux_x64-${fullversion}"
  13. echo -e "copying to ${serverfiles}...\c"
  14. fn_script_log "Copying to ${serverfiles}"
  15. cp -R "${tmpdir}/multitheftauto_linux_x64-${fullversion}/multitheftauto_linux_x64-${fullversion}/"* "${serverfiles}"
  16. local exitcode=$?
  17. if [ "${exitcode}" == "0" ]; then
  18. fn_print_ok_eol_nl
  19. else
  20. fn_print_fail_eol_nl
  21. fi
  22. }
  23. fn_update_mta_currentbuild(){
  24. # Gets current build info
  25. # Checks if current build info is available. If it fails, then a server restart will be forced to generate logs.
  26. if [ ! -f "${gamelogdir}"/server.log ]; then
  27. fn_print_error "Checking for update: linux.mtasa.com"
  28. sleep 0.5
  29. fn_print_error_nl "Checking for update: linux.mtasa.com: No logs with server version found"
  30. fn_script_log_error "Checking for update: linux.mtasa.com: No logs with server version found"
  31. sleep 0.5
  32. fn_print_info_nl "Checking for update: linux.mtasa.com: Forcing server restart"
  33. fn_script_log_info "Checking for update: linux.mtasa.com: Forcing server restart"
  34. sleep 0.5
  35. exitbypass=1
  36. command_stop.sh
  37. exitbypass=1
  38. command_start.sh
  39. sleep 0.5
  40. # Check again and exit on failure.
  41. if [ ! -f "${gamelogdir}"/server.log ]; then
  42. fn_print_fail_nl "Checking for update: linux.mtasa.com: Still No logs with server version found"
  43. fn_script_log_fatal "Checking for update: linux.mtasa.com: Still No logs with server version found"
  44. core_exit.sh
  45. fi
  46. fi
  47. # Get current build from logs
  48. currentbuild=$(grep "= Multi Theft Auto: San Andreas v" "${gamelogdir}/server.log" | awk '{ print $7 }' | sed -r 's/^.{1}//' | tail -1)
  49. if [ -z "${currentbuild}" ]; then
  50. fn_print_error_nl "Checking for update: linux.mtasa.com: Current build version not found"
  51. fn_script_log_error "Checking for update: linux.mtasa.com: Current build version not found"
  52. sleep 0.5
  53. fn_print_info_nl "Checking for update: linux.mtasa.com: Forcing server restart"
  54. fn_script_log_info "Checking for update: linux.mtasa.com: Forcing server restart"
  55. exitbypass=1
  56. command_stop.sh
  57. exitbypass=1
  58. command_start.sh
  59. currentbuild=$(grep "= Multi Theft Auto: San Andreas v" "${gamelogdir}/server.log" | awk '{ print $7 }' | sed -r 's/^.{1}//' | tail -1)
  60. if [ -z "${currentbuild}" ]; then
  61. fn_print_fail_nl "Checking for update: linux.mtasa.com: Current build version still not found"
  62. fn_script_log_fatal "Checking for update: linux.mtasa.com: Current build version still not found"
  63. core_exit.sh
  64. fi
  65. fi
  66. }
  67. fn_mta_get_availablebuild(){
  68. fn_fetch_file "https://raw.githubusercontent.com/multitheftauto/mtasa-blue/master/Server/version.h" "${tmpdir}" "version.h" # we need to find latest stable version here
  69. local majorversion="$(grep "#define MTASA_VERSION_MAJOR" "${tmpdir}/version.h" | awk '{ print $3 }' | sed 's/\r//g')"
  70. local minorversion="$(grep "#define MTASA_VERSION_MINOR" "${tmpdir}/version.h" | awk '{ print $3 }' | sed 's/\r//g')"
  71. local maintenanceversion="$(grep "#define MTASA_VERSION_MAINTENANCE" "${tmpdir}/version.h" | awk '{ print $3 }' | sed 's/\r//g')"
  72. numversion="${majorversion}${minorversion}${maintenanceversion}"
  73. fullversion="${majorversion}.${minorversion}.${maintenanceversion}"
  74. rm -f "${tmpdir}/version.h"
  75. }
  76. fn_update_mta_compare(){
  77. # Removes dots so if can compare version numbers
  78. currentbuilddigit=$(echo "${currentbuild}" | tr -cd '[:digit:]')
  79. if [ "${currentbuilddigit}" -ne "${numversion}" ]||[ "${forceupdate}" == "1" ]; then
  80. if [ "${forceupdate}" == "1" ]; then
  81. # forceupdate bypasses checks, useful for small build changes
  82. mta_update_string="forced"
  83. else
  84. mta_update_string="available"
  85. fi
  86. echo -e "\n"
  87. echo -e "Update ${mta_update_string}:"
  88. sleep 0.5
  89. echo -e " Current build: ${red}${currentbuild} ${default}"
  90. echo -e " Available build: ${green}${fullversion} ${default}"
  91. echo -e ""
  92. sleep 0.5
  93. echo -en "Applying update.\r"
  94. sleep 1
  95. echo -en "Applying update..\r"
  96. sleep 1
  97. echo -en "Applying update...\r"
  98. sleep 1
  99. echo -en "\n"
  100. fn_script_log "Update ${mta_update_string}"
  101. fn_script_log "Current build: ${currentbuild}"
  102. fn_script_log "Available build: ${fullversion}"
  103. fn_script_log "${currentbuild} > ${fullversion}"
  104. unset updateonstart
  105. check_status.sh
  106. if [ "${status}" == "0" ]; then
  107. fn_update_mta_dl
  108. exitbypass=1
  109. command_start.sh
  110. exitbypass=1
  111. command_stop.sh
  112. else
  113. exitbypass=1
  114. command_stop.sh
  115. fn_update_mta_dl
  116. exitbypass=1
  117. command_start.sh
  118. fi
  119. alert="update"
  120. alert.sh
  121. else
  122. echo -e "\n"
  123. echo -e "No update available:"
  124. echo -e " Current version: ${green}${currentbuild}${default}"
  125. echo -e " Available version: ${green}${fullversion}${default}"
  126. echo -e ""
  127. fn_print_ok_nl "No update available"
  128. fn_script_log_info "Current build: ${currentbuild}"
  129. fn_script_log_info "Available build: ${fullversion}"
  130. fi
  131. }
  132. if [ "${installer}" == "1" ]; then
  133. fn_mta_get_availablebuild
  134. fn_update_mta_dl
  135. else
  136. # Checks for server update from linux.mtasa.com using the github repo.
  137. fn_print_dots "Checking for update: linux.mtasa.com"
  138. fn_script_log_info "Checking for update: linux.mtasa.com"
  139. sleep 0.5
  140. fn_update_mta_currentbuild
  141. fn_mta_get_availablebuild
  142. fn_update_mta_compare
  143. fi