update_bb.sh 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #!/bin/bash
  2. # LinuxGSM update_bb.sh module
  3. # Author: Daniel Gibbs
  4. # Contributors: https://linuxgsm.com/contrib
  5. # Website: https://linuxgsm.com
  6. # Description: Handles updating of BrainBread 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}"
  12. echo "${remotebuild}" > "${serverfiles}/build.txt"
  13. fn_clear_tmp
  14. }
  15. fn_update_localbuild() {
  16. # Gets local build info.
  17. fn_print_dots "Checking local build: ${remotelocation}"
  18. # Uses build file to get local build.
  19. localbuild=$(head -n 1 "${serverfiles}/build.txt" 2> /dev/null)
  20. if [ -z "${localbuild}" ]; then
  21. fn_print_error "Checking local build: ${remotelocation}: missing local build info"
  22. fn_script_log_error "Missing local build info"
  23. fn_script_log_error "Set localbuild to 0"
  24. localbuild="0"
  25. else
  26. fn_print_ok "Checking local build: ${remotelocation}"
  27. fn_script_log_pass "Checking local build"
  28. fi
  29. }
  30. fn_update_remotebuild() {
  31. # Gets remote build info.
  32. apiurl="https://api.github.com/repos/IronOak-Studios/BrainBread/releases/latest"
  33. remotebuildresponse=$(curl -s "${apiurl}")
  34. remotebuildfilename=$(echo "${remotebuildresponse}" | jq -r '.assets[] | select(.name | test("linuxserver\\.tar\\.gz$")) | .name' | head -n 1)
  35. remotebuildurl=$(echo "${remotebuildresponse}" | jq -r '.assets[] | select(.name | test("linuxserver\\.tar\\.gz$")) | .browser_download_url' | head -n 1)
  36. remotebuildhash=$(echo "${remotebuildresponse}" | jq -r '.assets[] | select(.name | test("linuxserver\\.tar\\.gz$")) | .digest' | sed 's/^sha256://g' | head -n 1)
  37. remotebuild=$(echo "${remotebuildresponse}" | jq -r '.tag_name')
  38. if [ -z "${remotebuildhash}" ] || [ "${remotebuildhash}" == "null" ]; then
  39. remotebuildhash="nohash"
  40. fi
  41. if [ "${firstcommandname}" != "INSTALL" ]; then
  42. fn_print_dots "Checking remote build: ${remotelocation}"
  43. # Checks if remotebuild variable has been set.
  44. if [ -z "${remotebuild}" ] || [ "${remotebuild}" == "null" ] || [ -z "${remotebuildurl}" ] || [ "${remotebuildurl}" == "null" ] || [ -z "${remotebuildfilename}" ] || [ "${remotebuildfilename}" == "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 "${remotebuild}" ] || [ "${remotebuild}" == "null" ] || [ -z "${remotebuildurl}" ] || [ "${remotebuildurl}" == "null" ] || [ -z "${remotebuildfilename}" ] || [ "${remotebuildfilename}" == "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}" != "${remotebuild}" ] || [ "${forceupdate}" == "1" ]; then
  65. # Create update lockfile.
  66. date '+%s' > "${lockdir:?}/update.lock"
  67. fn_print_ok_nl "Checking for update: ${remotelocation}"
  68. fn_print "\n"
  69. fn_print_nl "${bold}${underline}Update${default} available"
  70. fn_print_nl "* Local build: ${red}${localbuild}${default}"
  71. fn_print_nl "* Remote build: ${green}${remotebuild}${default}"
  72. if [ -n "${branch}" ]; then
  73. fn_print_nl "* Branch: ${branch}"
  74. fi
  75. if [ -f "${rootdir}/.dev-debug" ]; then
  76. fn_print_nl "Remote build info"
  77. fn_print_nl "* apiurl: ${apiurl}"
  78. fn_print_nl "* remotebuildfilename: ${remotebuildfilename}"
  79. fn_print_nl "* remotebuildurl: ${remotebuildurl}"
  80. fn_print_nl "* remotebuildhash: ${remotebuildhash}"
  81. fn_print_nl "* remotebuild: ${remotebuild}"
  82. fi
  83. fn_print "\n"
  84. fn_script_log_info "Update available"
  85. fn_script_log_info "Local build: ${localbuild}"
  86. fn_script_log_info "Remote build: ${remotebuild}"
  87. if [ -n "${branch}" ]; then
  88. fn_script_log_info "Branch: ${branch}"
  89. fi
  90. fn_script_log_info "${localbuild} > ${remotebuild}"
  91. if [ "${commandname}" == "UPDATE" ]; then
  92. unset updateonstart
  93. fn_update_apply fn_update_dl
  94. if [ "${updatestatus}" == "0" ] && [ "${localbuild}" == "0" ]; then
  95. exitbypass=1
  96. command_start.sh
  97. fn_firstcommand_reset
  98. exitbypass=1
  99. fn_sleep_time_5
  100. command_stop.sh
  101. fn_firstcommand_reset
  102. fi
  103. unset exitbypass
  104. alert="update"
  105. elif [ "${commandname}" == "CHECK-UPDATE" ]; then
  106. alert="check-update"
  107. fi
  108. alert.sh
  109. else
  110. fn_print_ok_nl "Checking for update: ${remotelocation}"
  111. fn_print "\n"
  112. fn_print_nl "${bold}${underline}No update${default} available"
  113. fn_print_nl "* Local build: ${green}${localbuild}${default}"
  114. fn_print_nl "* Remote build: ${green}${remotebuild}${default}"
  115. if [ -n "${branch}" ]; then
  116. fn_print_nl "* Branch: ${branch}"
  117. fi
  118. fn_print "\n"
  119. fn_script_log_info "No update available"
  120. fn_script_log_info "Local build: ${localbuild}"
  121. fn_script_log_info "Remote build: ${remotebuild}"
  122. if [ -n "${branch}" ]; then
  123. fn_script_log_info "Branch: ${branch}"
  124. fi
  125. if [ -f "${rootdir}/.dev-debug" ]; then
  126. fn_print_nl "Remote build info"
  127. fn_print_nl "* apiurl: ${apiurl}"
  128. fn_print_nl "* remotebuildfilename: ${remotebuildfilename}"
  129. fn_print_nl "* remotebuildurl: ${remotebuildurl}"
  130. fn_print_nl "* remotebuildhash: ${remotebuildhash}"
  131. fn_print_nl "* remotebuild: ${remotebuild}"
  132. fi
  133. fi
  134. }
  135. # The location where the builds are checked and downloaded.
  136. remotelocation="github.com"
  137. if [ "${firstcommandname}" == "INSTALL" ]; then
  138. fn_update_remotebuild
  139. fn_update_dl
  140. else
  141. # BrainBread requires both Steam app updates and GitHub package updates.
  142. update_steamcmd.sh
  143. fn_print_dots "Checking for update"
  144. fn_print_dots "Checking for update: ${remotelocation}"
  145. fn_script_log_info "Checking for update: ${remotelocation}"
  146. fn_update_localbuild
  147. fn_update_remotebuild
  148. fn_update_compare
  149. fi