core_dl.sh 9.1 KB

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