core_dl.sh 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. if [ -f "${lgsmlog}" ]; then
  69. echo "${extractcmd}" >> "${lgsmlog}"
  70. fi
  71. echo "${extractcmd}"
  72. core_exit.sh
  73. else
  74. fn_print_ok_eol_nl
  75. fn_script_log_pass "Extracting download: OK"
  76. fi
  77. }
  78. # Trap to remove file download if canceled before completed
  79. fn_fetch_trap(){
  80. echo ""
  81. echo -ne "downloading ${local_filename}..."
  82. fn_print_canceled_eol_nl
  83. fn_script_log_info "Downloading ${local_filename}...CANCELED"
  84. sleep 1
  85. rm -f "${local_filedir}/${local_filename}"
  86. echo -ne "downloading ${local_filename}..."
  87. fn_print_removed_eol_nl
  88. fn_script_log_info "Downloading ${local_filename}...REMOVED"
  89. core_exit.sh
  90. }
  91. fn_fetch_file(){
  92. remote_fileurl="${1}"
  93. local_filedir="${2}"
  94. local_filename="${3}"
  95. chmodx="${4:-0}"
  96. run="${5:-0}"
  97. forcedl="${6:-0}"
  98. md5="${7:-0}"
  99. # If the file is missing, then download
  100. if [ ! -f "${local_filedir}/${local_filename}" ]; then
  101. if [ ! -d "${local_filedir}" ]; then
  102. mkdir -p "${local_filedir}"
  103. fi
  104. # Trap will remove part downloaded files if canceled
  105. trap fn_fetch_trap INT
  106. # if larger file shows progress bar
  107. if [ "${local_filename##*.}" == "bz2" ]||[ "${local_filename##*.}" == "gz" ]||[ "${local_filename##*.}" == "zip" ]||[ "${local_filename##*.}" == "jar" ]; then
  108. echo -ne "downloading ${local_filename}..."
  109. sleep 1
  110. curlcmd=$(${curlpath} --progress-bar --fail -L -o "${local_filedir}/${local_filename}" "${remote_fileurl}")
  111. echo -ne "downloading ${local_filename}..."
  112. else
  113. echo -ne " fetching ${local_filename}...\c"
  114. curlcmd=$(${curlpath} -s --fail -L -o "${local_filedir}/${local_filename}" "${remote_fileurl}" 2>&1)
  115. fi
  116. local exitcode=$?
  117. if [ ${exitcode} -ne 0 ]; then
  118. fn_print_fail_eol_nl
  119. if [ -f "${lgsmlog}" ]; then
  120. fn_script_log_fatal "Downloading ${local_filename}: FAIL"
  121. echo -e "${remote_fileurl}" >> "${lgsmlog}"
  122. echo "${curlcmd}" >> "${lgsmlog}"
  123. fi
  124. echo -e "${remote_fileurl}"
  125. echo "${curlcmd}"
  126. core_exit.sh
  127. else
  128. fn_print_ok_eol_nl
  129. if [ -f "${lgsmlog}" ]; then
  130. fn_script_log_pass "Downloading ${local_filename}: OK"
  131. fi
  132. fi
  133. # Remove trap
  134. trap - INT
  135. # Make file executable if chmodx is set
  136. if [ "${chmodx}" == "chmodx" ]; then
  137. chmod +x "${local_filedir}/${local_filename}"
  138. fi
  139. fi
  140. if [ -f "${local_filedir}/${local_filename}" ]; then
  141. fn_dl_md5
  142. # Execute file if run is set
  143. if [ "${run}" == "run" ]; then
  144. source "${local_filedir}/${local_filename}"
  145. fi
  146. fi
  147. }
  148. # GitHub file download functions
  149. # Used to simplify downloading specific files from GitHub
  150. # github_file_url_dir: the directory of the file in the GitHub: lgsm/functions
  151. # github_file_url_name: the filename of the file to download from GitHub: core_messages.sh
  152. # githuburl: the full GitHub url
  153. # remote_fileurl: The URL of the file: http://example.com/dl/File.tar.bz2
  154. # local_filedir: location the file is to be saved: /home/server/lgsm/tmp
  155. # local_filename: name of file (this can be different from the url name): file.tar.bz2
  156. # chmodx: Optional, set to "chmodx" to make file executable using chmod +x
  157. # run: Optional, set run to execute the file after download
  158. # forcedl: Optional, force re-download of file even if exists
  159. # md5: Optional, set an md5 sum and will compare it against the file.
  160. # Fetches any files from the GitHub repo
  161. fn_fetch_file_github(){
  162. github_file_url_dir="${1}"
  163. github_file_url_name="${2}"
  164. githuburl="https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${githubbranch}/${github_file_url_dir}/${github_file_url_name}"
  165. remote_fileurl="${githuburl}"
  166. local_filedir="${3}"
  167. local_filename="${github_file_url_name}"
  168. chmodx="${4:-0}"
  169. run="${5:-0}"
  170. forcedl="${6:-0}"
  171. md5="${7:-0}"
  172. # Passes vars to the file download function
  173. fn_fetch_file "${remote_fileurl}" "${local_filedir}" "${local_filename}" "${chmodx}" "${run}" "${forcedl}" "${md5}"
  174. }
  175. fn_fetch_config(){
  176. github_file_url_dir="${1}"
  177. github_file_url_name="${2}"
  178. githuburl="https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${githubbranch}/${github_file_url_dir}/${github_file_url_name}"
  179. remote_fileurl="${githuburl}"
  180. local_filedir="${3}"
  181. local_filename="${4}"
  182. chmodx="nochmodx"
  183. run="norun"
  184. forcedl="noforce"
  185. md5="nomd5"
  186. # Passes vars to the file download function
  187. fn_fetch_file "${remote_fileurl}" "${local_filedir}" "${local_filename}" "${chmodx}" "${run}" "${forcedl}" "${md5}"
  188. }
  189. # Fetches functions
  190. fn_fetch_function(){
  191. github_file_url_dir="lgsm/functions"
  192. github_file_url_name="${functionfile}"
  193. githuburl="https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${githubbranch}/${github_file_url_dir}/${github_file_url_name}"
  194. remote_fileurl="${githuburl}"
  195. local_filedir="${functionsdir}"
  196. local_filename="${github_file_url_name}"
  197. chmodx="chmodx"
  198. run="run"
  199. forcedl="noforce"
  200. md5="nomd5"
  201. # Passes vars to the file download function
  202. fn_fetch_file "${remote_fileurl}" "${local_filedir}" "${local_filename}" "${chmodx}" "${run}" "${forcedl}" "${md5}"
  203. }
  204. fn_update_function(){
  205. exitbypass=1
  206. github_file_url_dir="lgsm/functions"
  207. github_file_url_name="${functionfile}"
  208. githuburl="https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${githubbranch}/${github_file_url_dir}/${github_file_url_name}"
  209. remote_fileurl="${githuburl}"
  210. local_filedir="${functionsdir}"
  211. local_filename="${github_file_url_name}"
  212. chmodx="chmodx"
  213. run="norun"
  214. forcedl="noforce"
  215. md5="nomd5"
  216. fn_fetch_file "${remote_fileurl}" "${local_filedir}" "${local_filename}" "${chmodx}" "${run}" "${forcedl}" "${md5}"
  217. }
  218. # Defines curl path
  219. 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)
  220. for curlpath in "${curl_paths_array}"
  221. do
  222. if [ -x "${curlpath}" ]; then
  223. break
  224. fi
  225. done
  226. if [ "$(basename ${curlpath})" != "curl" ]; then
  227. echo "[ FAIL ] Curl is not installed"
  228. exit 1
  229. fi