core_dl.sh 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. remote_fileurl_backup="${2}"
  109. remote_fileurl_name="${3}"
  110. remote_fileurl_backup_name="${4}"
  111. local_filedir="${5}"
  112. local_filename="${6}"
  113. chmodx="${7:-0}"
  114. run="${8:-0}"
  115. forcedl="${9:-0}"
  116. md5="${10:-0}"
  117. # Download file if missing or download forced.
  118. if [ ! -f "${local_filedir}/${local_filename}" ]||[ "${forcedl}" == "forcedl" ]; then
  119. # If backup fileurl exists include it.
  120. if [ -n "${remote_fileurl_backup}" ]; then
  121. # counter set to 0 to allow second try
  122. counter=0
  123. remote_fileurls_array=( remote_fileurl remote_fileurl_backup )
  124. else
  125. # counter set to 1 to not allow second try
  126. counter=1
  127. remote_fileurls_array=( remote_fileurl )
  128. fi
  129. for remote_fileurl_array in "${remote_fileurls_array[@]}"
  130. do
  131. if [ "${remote_fileurl_array}" == "remote_fileurl" ]; then
  132. fileurl="${remote_fileurl}"
  133. fileurl_name="${remote_fileurl_name}"
  134. elif [ "${remote_fileurl_array}" == "remote_fileurl_backup" ]; then
  135. fileurl="${remote_fileurl_backup}"
  136. fileurl_name="${remote_fileurl_backup_name}"
  137. fi
  138. counter=$((counter+1))
  139. if [ ! -d "${local_filedir}" ]; then
  140. mkdir -p "${local_filedir}"
  141. fi
  142. # Trap will remove part downloaded files if canceled.
  143. trap fn_fetch_trap INT
  144. # Larger files show a progress bar.
  145. if [ "${local_filename##*.}" == "bz2" ]||[ "${local_filename##*.}" == "gz" ]||[ "${local_filename##*.}" == "zip" ]||[ "${local_filename##*.}" == "jar" ]||[ "${local_filename##*.}" == "xz" ]; then
  146. echo -en "downloading ${local_filename}..."
  147. fn_sleep_time
  148. echo -en "\033[1K"
  149. curlcmd=$(curl --progress-bar --fail -L -o "${local_filedir}/${local_filename}" "${fileurl}")
  150. echo -en "downloading ${local_filename}..."
  151. else
  152. echo -en "fetching ${fileurl_name} ${local_filename}...\c"
  153. curlcmd=$(curl -s --fail -L -o "${local_filedir}/${local_filename}" "${fileurl}" 2>&1)
  154. fi
  155. local exitcode=$?
  156. # Download will fail if downloads a html file.
  157. if [ -f "${local_filedir}/${local_filename}" ]; then
  158. if [ -n "$(head "${local_filedir}/${local_filename}" | grep "DOCTYPE" )" ]; then
  159. rm "${local_filedir:?}/${local_filename:?}"
  160. local exitcode=2
  161. fi
  162. fi
  163. # On first try will error. On second try will fail.
  164. if [ ${exitcode} -ne 0 ]; then
  165. if [ ${counter} -ge 2 ]; then
  166. fn_print_fail_eol_nl
  167. if [ -f "${lgsmlog}" ]; then
  168. fn_script_log_fatal "Downloading ${local_filename}"
  169. fn_script_log_fatal "${fileurl}"
  170. fi
  171. core_exit.sh
  172. else
  173. fn_print_error_eol_nl
  174. if [ -f "${lgsmlog}" ]; then
  175. fn_script_log_error "Downloading ${local_filename}"
  176. fn_script_log_error "${fileurl}"
  177. fi
  178. fi
  179. else
  180. fn_print_ok_eol
  181. sleep 0.3
  182. echo -en "\033[2K\\r"
  183. if [ -f "${lgsmlog}" ]; then
  184. fn_script_log_pass "Downloading ${local_filename}"
  185. fi
  186. # Make file executable if chmodx is set.
  187. if [ "${chmodx}" == "chmodx" ]; then
  188. chmod +x "${local_filedir}/${local_filename}"
  189. fi
  190. # Remove trap.
  191. trap - INT
  192. break
  193. fi
  194. done
  195. fi
  196. if [ -f "${local_filedir}/${local_filename}" ]; then
  197. fn_dl_md5
  198. # Execute file if run is set.
  199. if [ "${run}" == "run" ]; then
  200. # shellcheck source=/dev/null
  201. source "${local_filedir}/${local_filename}"
  202. fi
  203. fi
  204. }
  205. # GitHub file download functions.
  206. # Used to simplify downloading specific files from GitHub.
  207. # github_file_url_dir: the directory of the file in the GitHub: lgsm/functions
  208. # github_file_url_name: the filename of the file to download from GitHub: core_messages.sh
  209. # githuburl: the full GitHub url
  210. # remote_fileurl: The URL of the file: http://example.com/dl/File.tar.bz2
  211. # local_filedir: location the file is to be saved: /home/server/lgsm/tmp
  212. # local_filename: name of file (this can be different from the url name): file.tar.bz2
  213. # chmodx: Optional, set to "chmodx" to make file executable using chmod +x
  214. # run: Optional, set run to execute the file after download
  215. # forcedl: Optional, force re-download of file even if exists
  216. # md5: Optional, set an md5 sum and will compare it against the file.
  217. # Fetches files from the Git repo.
  218. fn_fetch_file_github(){
  219. github_file_url_dir="${1}"
  220. github_file_url_name="${2}"
  221. if [ "${legacymode}" == "1" ]; then
  222. # For legacy versions - code can be removed at a future date
  223. remote_fileurl="https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${githubbranch}/${github_file_url_dir}/${github_file_url_name}"
  224. remote_fileurl_backup="https://bitbucket.org/${githubuser}/${githubrepo}/raw/${githubbranch}/${github_file_url_dir}/${github_file_url_name}"
  225. elif [ "${githubbranch}" == "master" ]&&[ "${commandname}" != "UPDATE-LGSM" ]; then
  226. remote_fileurl="https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${version}/${github_file_url_dir}/${github_file_url_name}"
  227. remote_fileurl_backup="https://bitbucket.org/${githubuser}/${githubrepo}/raw/${version}/${github_file_url_dir}/${github_file_url_name}"
  228. else
  229. remote_fileurl="https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${githubbranch}/${github_file_url_dir}/${github_file_url_name}"
  230. remote_fileurl_backup="https://bitbucket.org/${githubuser}/${githubrepo}/raw/${githubbranch}/${github_file_url_dir}/${github_file_url_name}"
  231. fi
  232. remote_fileurl_name="GitHub"
  233. remote_fileurl_backup_name="Bitbucket"
  234. local_filedir="${3}"
  235. local_filename="${github_file_url_name}"
  236. chmodx="${4:-0}"
  237. run="${5:-0}"
  238. forcedl="${6:-0}"
  239. md5="${7:-0}"
  240. # Passes vars to the file download function.
  241. fn_fetch_file "${remote_fileurl}" "${remote_fileurl_backup}" "${remote_fileurl_name}" "${remote_fileurl_backup_name}" "${local_filedir}" "${local_filename}" "${chmodx}" "${run}" "${forcedl}" "${md5}"
  242. }
  243. # Fetches config files from the Git repo.
  244. fn_fetch_config(){
  245. github_file_url_dir="${1}"
  246. github_file_url_name="${2}"
  247. if [ "${githubbranch}" == "master" ]&&[ "${commandname}" != "UPDATE-LGSM" ]; then
  248. remote_fileurl="https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${version}/${github_file_url_dir}/${github_file_url_name}"
  249. remote_fileurl_backup="https://bitbucket.org/${githubuser}/${githubrepo}/raw/${version}/${github_file_url_dir}/${github_file_url_name}"
  250. else
  251. remote_fileurl="https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${githubbranch}/${github_file_url_dir}/${github_file_url_name}"
  252. remote_fileurl_backup="https://bitbucket.org/${githubuser}/${githubrepo}/raw/${githubbranch}/${github_file_url_dir}/${github_file_url_name}"
  253. fi
  254. remote_fileurl_name="GitHub"
  255. remote_fileurl_backup_name="Bitbucket"
  256. local_filedir="${3}"
  257. local_filename="${4}"
  258. chmodx="nochmodx"
  259. run="norun"
  260. forcedl="noforce"
  261. md5="nomd5"
  262. # Passes vars to the file download function.
  263. fn_fetch_file "${remote_fileurl}" "${remote_fileurl_backup}" "${remote_fileurl_name}" "${remote_fileurl_backup_name}" "${local_filedir}" "${local_filename}" "${chmodx}" "${run}" "${forcedl}" "${md5}"
  264. }
  265. # Fetches modules from the Git repo during first download.
  266. fn_fetch_function(){
  267. github_file_url_dir="lgsm/functions"
  268. github_file_url_name="${functionfile}"
  269. if [ "${githubbranch}" == "master" ]&&[ "${commandname}" != "UPDATE-LGSM" ]; then
  270. remote_fileurl="https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${version}/${github_file_url_dir}/${github_file_url_name}"
  271. remote_fileurl_backup="https://bitbucket.org/${githubuser}/${githubrepo}/raw/${version}/${github_file_url_dir}/${github_file_url_name}"
  272. else
  273. remote_fileurl="https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${githubbranch}/${github_file_url_dir}/${github_file_url_name}"
  274. remote_fileurl_backup="https://bitbucket.org/${githubuser}/${githubrepo}/raw/${githubbranch}/${github_file_url_dir}/${github_file_url_name}"
  275. fi
  276. remote_fileurl_name="GitHub"
  277. remote_fileurl_backup_name="Bitbucket"
  278. local_filedir="${functionsdir}"
  279. local_filename="${github_file_url_name}"
  280. chmodx="chmodx"
  281. run="run"
  282. forcedl="noforce"
  283. md5="nomd5"
  284. # Passes vars to the file download function.
  285. fn_fetch_file "${remote_fileurl}" "${remote_fileurl_backup}" "${remote_fileurl_name}" "${remote_fileurl_backup_name}" "${local_filedir}" "${local_filename}" "${chmodx}" "${run}" "${forcedl}" "${md5}"
  286. }
  287. # Fetches modules from the Git repo during update-lgsm.
  288. fn_update_function(){
  289. github_file_url_dir="lgsm/functions"
  290. github_file_url_name="${functionfile}"
  291. if [ "${githubbranch}" == "master" ]&&[ "${commandname}" != "UPDATE-LGSM" ]; then
  292. remote_fileurl="https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${version}/${github_file_url_dir}/${github_file_url_name}"
  293. remote_fileurl_backup="https://bitbucket.org/${githubuser}/${githubrepo}/raw/${version}/${github_file_url_dir}/${github_file_url_name}"
  294. else
  295. remote_fileurl="https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${githubbranch}/${github_file_url_dir}/${github_file_url_name}"
  296. remote_fileurl_backup="https://bitbucket.org/${githubuser}/${githubrepo}/raw/${githubbranch}/${github_file_url_dir}/${github_file_url_name}"
  297. fi
  298. remote_fileurl_name="GitHub"
  299. remote_fileurl_backup_name="Bitbucket"
  300. local_filedir="${functionsdir}"
  301. local_filename="${github_file_url_name}"
  302. chmodx="chmodx"
  303. run="norun"
  304. forcedl="noforce"
  305. md5="nomd5"
  306. # Passes vars to the file download function.
  307. fn_fetch_file "${remote_fileurl}" "${remote_fileurl_backup}" "${remote_fileurl_name}" "${remote_fileurl_backup_name}" "${local_filedir}" "${local_filename}" "${chmodx}" "${run}" "${forcedl}" "${md5}"
  308. }
  309. # Check that curl is installed
  310. if [ ! "$(command -v curl 2>/dev/null)" ]; then
  311. echo -e "[ FAIL ] Curl is not installed"
  312. exit 1
  313. fi