core_dl.sh 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. #!/bin/bash
  2. # LinuxGSM core_dl.sh function
  3. # Author: Daniel Gibbs
  4. # Contributor: UltimateByte
  5. # Website: https://linuxgsm.com
  6. # Description: Deals with all downloads for LinuxGSM.
  7. # remote_fileurl: The URL of the file: http://example.com/dl/File.tar.bz2
  8. # local_filedir: location the file is to be saved: /home/server/lgsm/tmp
  9. # local_filename: name of file (this can be different from the url name): file.tar.bz2
  10. # chmodx: Optional, set to "chmodx" to make file executable using chmod +x
  11. # run: Optional, set run to execute the file after download
  12. # forcedl: Optional, force re-download of file even if exists
  13. # md5: Optional, set an md5 sum and will compare it against the file.
  14. #
  15. # Downloads can be defined in code like so:
  16. # fn_fetch_file "${remote_fileurl}" "${local_filedir}" "${local_filename}" "${chmodx}" "${run}" "${forcedl}" "${md5}"
  17. # fn_fetch_file "http://example.com/file.tar.bz2" "/some/dir" "file.tar.bz2" "chmodx" "run" "forcedl" "10cd7353aa9d758a075c600a6dd193fd"
  18. functionselfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")"
  19. # Emptys contents of the LinuxGSM tmpdir.
  20. fn_clear_tmp(){
  21. echo -en "clearing LinuxGSM tmp directory..."
  22. if [ -d "${tmpdir}" ]; then
  23. rm -rf "${tmpdir:?}/"*
  24. local exitcode=$?
  25. if [ ${exitcode} -eq 0 ]; then
  26. fn_print_ok_eol_nl
  27. fn_script_log_pass "clearing LinuxGSM tmp directory"
  28. else
  29. fn_print_error_eol_nl
  30. fn_script_log_error "clearing LinuxGSM tmp directory"
  31. fi
  32. fi
  33. }
  34. fn_dl_md5(){
  35. # Runs MD5 Check if available.
  36. if [ "${md5}" != "0" ]&&[ "${md5}" != "nomd5" ]; then
  37. echo -en "verifying ${local_filename} with MD5..."
  38. fn_sleep_time
  39. md5sumcmd=$(md5sum "${local_filedir}/${local_filename}"|awk '{print $1;}')
  40. if [ "${md5sumcmd}" != "${md5}" ]; then
  41. fn_print_fail_eol_nl
  42. echo -e "${local_filename} returned MD5 checksum: ${md5sumcmd}"
  43. echo -e "expected MD5 checksum: ${md5}"
  44. fn_script_log_fatal "Verifying ${local_filename} with MD5"
  45. fn_script_log_info "${local_filename} returned MD5 checksum: ${md5sumcmd}"
  46. fn_script_log_info "Expected MD5 checksum: ${md5}"
  47. core_exit.sh
  48. else
  49. fn_print_ok_eol_nl
  50. fn_script_log_pass "Verifying ${local_filename} with MD5"
  51. fn_script_log_info "${local_filename} returned MD5 checksum: ${md5sumcmd}"
  52. fn_script_log_info "Expected MD5 checksum: ${md5}"
  53. fi
  54. fi
  55. }
  56. # Extracts bzip2, gzip or zip files.
  57. # Extracts can be defined in code like so:
  58. # fn_dl_extract "${local_filedir}" "${local_filename}" "${extractdir}"
  59. # fn_dl_extract "/home/gameserver/lgsm/tmp" "file.tar.bz2" "/home/gamserver/serverfiles"
  60. fn_dl_extract(){
  61. local_filedir="${1}"
  62. local_filename="${2}"
  63. extractdir="${3}"
  64. # Extracts archives.
  65. echo -en "extracting ${local_filename}..."
  66. mime=$(file -b --mime-type "${local_filedir}/${local_filename}")
  67. if [ ! -d "${extractdir}" ]; then
  68. mkdir "${extractdir}"
  69. fi
  70. if [ "${mime}" == "application/gzip" ]||[ "${mime}" == "application/x-gzip" ]; then
  71. extractcmd=$(tar -zxf "${local_filedir}/${local_filename}" -C "${extractdir}")
  72. elif [ "${mime}" == "application/x-bzip2" ]; then
  73. extractcmd=$(tar -jxf "${local_filedir}/${local_filename}" -C "${extractdir}")
  74. elif [ "${mime}" == "application/x-xz" ]; then
  75. extractcmd=$(tar -xf "${local_filedir}/${local_filename}" -C "${extractdir}")
  76. elif [ "${mime}" == "application/zip" ]; then
  77. extractcmd=$(unzip -qo -d "${extractdir}" "${local_filedir}/${local_filename}")
  78. fi
  79. local exitcode=$?
  80. if [ ${exitcode} -ne 0 ]; then
  81. fn_print_fail_eol_nl
  82. fn_script_log_fatal "Extracting download"
  83. if [ -f "${lgsmlog}" ]; then
  84. echo -e "${extractcmd}" >> "${lgsmlog}"
  85. fi
  86. echo -e "${extractcmd}"
  87. core_exit.sh
  88. else
  89. fn_print_ok_eol_nl
  90. fn_script_log_pass "Extracting download"
  91. fi
  92. }
  93. # Trap to remove file download if canceled before completed.
  94. fn_fetch_trap(){
  95. echo -e ""
  96. echo -en "downloading ${local_filename}..."
  97. fn_print_canceled_eol_nl
  98. fn_script_log_info "Downloading ${local_filename}...CANCELED"
  99. fn_sleep_time
  100. rm -f "${local_filedir:?}/${local_filename}"
  101. echo -en "downloading ${local_filename}..."
  102. fn_print_removed_eol_nl
  103. fn_script_log_info "Downloading ${local_filename}...REMOVED"
  104. core_exit.sh
  105. }
  106. fn_fetch_file(){
  107. remote_fileurl="${1}"
  108. local_filedir="${2}"
  109. local_filename="${3}"
  110. chmodx="${4:-0}"
  111. run="${5:-0}"
  112. forcedl="${6:-0}"
  113. md5="${7:-0}"
  114. # Download file if missing or download forced.
  115. if [ ! -f "${local_filedir}/${local_filename}" ]||[ "${forcedl}" == "forcedl" ]; then
  116. if [ ! -d "${local_filedir}" ]; then
  117. mkdir -p "${local_filedir}"
  118. fi
  119. # Trap will remove part downloaded files if canceled.
  120. trap fn_fetch_trap INT
  121. # Larger files show a progress bar.
  122. if [ "${local_filename##*.}" == "bz2" ]||[ "${local_filename##*.}" == "gz" ]||[ "${local_filename##*.}" == "zip" ]||[ "${local_filename##*.}" == "jar" ]||[ "${local_filename##*.}" == "xz" ]; then
  123. echo -en "downloading ${local_filename}..."
  124. fn_sleep_time
  125. echo -en "\033[1K"
  126. curlcmd=$(curl --progress-bar --fail -L -o "${local_filedir}/${local_filename}" "${remote_fileurl}")
  127. echo -en "downloading ${local_filename}..."
  128. else
  129. echo -en " fetching ${local_filename}...\c"
  130. curlcmd=$(curl -s --fail -L -o "${local_filedir}/${local_filename}" "${remote_fileurl}" 2>&1)
  131. fi
  132. local exitcode=$?
  133. if [ ${exitcode} -ne 0 ]; then
  134. fn_print_fail_eol_nl
  135. if [ -f "${lgsmlog}" ]; then
  136. fn_script_log_fatal "Downloading ${local_filename}"
  137. echo -e "${remote_fileurl}" >> "${lgsmlog}"
  138. echo -e "${curlcmd}" >> "${lgsmlog}"
  139. fi
  140. echo -e "${remote_fileurl}"
  141. echo -e "${curlcmd}"
  142. core_exit.sh
  143. else
  144. fn_print_ok_eol_nl
  145. if [ -f "${lgsmlog}" ]; then
  146. fn_script_log_pass "Downloading ${local_filename}"
  147. fi
  148. fi
  149. # Remove trap.
  150. trap - INT
  151. # Make file executable if chmodx is set.
  152. if [ "${chmodx}" == "chmodx" ]; then
  153. chmod +x "${local_filedir}/${local_filename}"
  154. fi
  155. fi
  156. if [ -f "${local_filedir}/${local_filename}" ]; then
  157. fn_dl_md5
  158. # Execute file if run is set.
  159. if [ "${run}" == "run" ]; then
  160. # shellcheck source=/dev/null
  161. source "${local_filedir}/${local_filename}"
  162. fi
  163. fi
  164. }
  165. # GitHub file download functions.
  166. # Used to simplify downloading specific files from GitHub.
  167. # github_file_url_dir: the directory of the file in the GitHub: lgsm/functions
  168. # github_file_url_name: the filename of the file to download from GitHub: core_messages.sh
  169. # githuburl: the full GitHub url
  170. # remote_fileurl: The URL of the file: http://example.com/dl/File.tar.bz2
  171. # local_filedir: location the file is to be saved: /home/server/lgsm/tmp
  172. # local_filename: name of file (this can be different from the url name): file.tar.bz2
  173. # chmodx: Optional, set to "chmodx" to make file executable using chmod +x
  174. # run: Optional, set run to execute the file after download
  175. # forcedl: Optional, force re-download of file even if exists
  176. # md5: Optional, set an md5 sum and will compare it against the file.
  177. # Fetches any files from the GitHub repo.
  178. fn_fetch_file_github(){
  179. github_file_url_dir="${1}"
  180. github_file_url_name="${2}"
  181. githuburl="https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${githubbranch}/${github_file_url_dir}/${github_file_url_name}"
  182. remote_fileurl="${githuburl}"
  183. local_filedir="${3}"
  184. local_filename="${github_file_url_name}"
  185. chmodx="${4:-0}"
  186. run="${5:-0}"
  187. forcedl="${6:-0}"
  188. md5="${7:-0}"
  189. # Passes vars to the file download function.
  190. fn_fetch_file "${remote_fileurl}" "${local_filedir}" "${local_filename}" "${chmodx}" "${run}" "${forcedl}" "${md5}"
  191. }
  192. fn_fetch_config(){
  193. github_file_url_dir="${1}"
  194. github_file_url_name="${2}"
  195. githuburl="https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${githubbranch}/${github_file_url_dir}/${github_file_url_name}"
  196. remote_fileurl="${githuburl}"
  197. local_filedir="${3}"
  198. local_filename="${4}"
  199. chmodx="nochmodx"
  200. run="norun"
  201. forcedl="noforce"
  202. md5="nomd5"
  203. # Passes vars to the file download function.
  204. fn_fetch_file "${remote_fileurl}" "${local_filedir}" "${local_filename}" "${chmodx}" "${run}" "${forcedl}" "${md5}"
  205. }
  206. # Fetches functions.
  207. fn_fetch_function(){
  208. github_file_url_dir="lgsm/functions"
  209. github_file_url_name="${functionfile}"
  210. githuburl="https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${githubbranch}/${github_file_url_dir}/${github_file_url_name}"
  211. remote_fileurl="${githuburl}"
  212. local_filedir="${functionsdir}"
  213. local_filename="${github_file_url_name}"
  214. chmodx="chmodx"
  215. run="run"
  216. forcedl="noforce"
  217. md5="nomd5"
  218. # Passes vars to the file download function.
  219. fn_fetch_file "${remote_fileurl}" "${local_filedir}" "${local_filename}" "${chmodx}" "${run}" "${forcedl}" "${md5}"
  220. }
  221. fn_update_function(){
  222. exitbypass=1
  223. github_file_url_dir="lgsm/functions"
  224. github_file_url_name="${functionfile}"
  225. githuburl="https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${githubbranch}/${github_file_url_dir}/${github_file_url_name}"
  226. remote_fileurl="${githuburl}"
  227. local_filedir="${functionsdir}"
  228. local_filename="${github_file_url_name}"
  229. chmodx="chmodx"
  230. run="norun"
  231. forcedl="noforce"
  232. md5="nomd5"
  233. fn_fetch_file "${remote_fileurl}" "${local_filedir}" "${local_filename}" "${chmodx}" "${run}" "${forcedl}" "${md5}"
  234. }
  235. # Check that curl is installed
  236. if [ ! "$(command -v curl 2>/dev/null)" ]; then
  237. echo -e "[ FAIL ] Curl is not installed"
  238. exit 1
  239. fi