core_dl.sh 7.0 KB

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