core_dl.sh 8.6 KB

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