core_dl.sh 7.1 KB

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