core_dl.sh 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 or zip 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}" --strip-components=1)
  57. elif [ "${mime}" == "application/x-bzip2" ]; then
  58. tarcmd=$(tar -jxf "${filedir}/${filename}" -C "${extractdir}")
  59. elif [ "${mime}" == "application/zip" ]; then
  60. tarcmd=$(unzip -d "${extractdir}" "${filedir}/${filename}")
  61. fi
  62. local exitcode=$?
  63. if [ ${exitcode} -ne 0 ]; then
  64. fn_print_fail_eol_nl
  65. fn_script_log_fatal "Extracting download: FAIL"
  66. echo "${tarcmd}" | tee -a "${scriptlog}"
  67. core_exit.sh
  68. else
  69. fn_print_ok_eol_nl
  70. fi
  71. }
  72. # Trap to remove file download if canceled before completed
  73. fn_fetch_trap(){
  74. echo ""
  75. echo -ne "downloading ${filename}..."
  76. fn_print_canceled_eol_nl
  77. fn_script_log_info "downloading ${filename}...CANCELED"
  78. sleep 1
  79. rm -f "${filedir}/${filename}" | tee -a "${scriptlog}"
  80. echo -ne "downloading ${filename}..."
  81. fn_print_removed_eol_nl
  82. fn_script_log_info "downloading ${filename}...REMOVED"
  83. core_exit.sh
  84. }
  85. fn_fetch_file(){
  86. fileurl="${1}"
  87. filedir="${2}"
  88. filename="${3}"
  89. executecmd="${4:-0}"
  90. run="${5:-0}"
  91. force="${6:-0}"
  92. md5="${7:-0}"
  93. # If the file is missing, then download
  94. if [ ! -f "${filedir}/${filename}" ]; then
  95. if [ ! -d "${filedir}" ]; then
  96. mkdir -p "${filedir}"
  97. fi
  98. # Check curl exists and use available path
  99. curlpaths="$(command -v curl 2>/dev/null) $(which curl >/dev/null 2>&1) /usr/bin/curl /bin/curl /usr/sbin/curl /sbin/curl)"
  100. for curlcmd in ${curlpaths}
  101. do
  102. if [ -x "${curlcmd}" ]; then
  103. break
  104. fi
  105. done
  106. # If curl exists download file
  107. if [ "$(basename ${curlcmd})" == "curl" ]; then
  108. # trap to remove part downloaded files
  109. trap fn_fetch_trap INT
  110. # if larger file shows progress bar
  111. if [ ${filename##*.} == "bz2" ]||[ ${filename##*.} == "gz" ]||[ ${filename##*.} == "zip" ]||[ ${filename##*.} == "jar" ]; then
  112. echo -ne "downloading ${filename}..."
  113. sleep 1
  114. curlcmd=$(${curlcmd} --progress-bar --fail -L -o "${filedir}/${filename}" "${fileurl}")
  115. echo -ne "downloading ${filename}..."
  116. else
  117. echo -ne " fetching ${filename}...\c"
  118. curlcmd=$(${curlcmd} -s --fail -L -o "${filedir}/${filename}" "${fileurl}" 2>&1)
  119. fi
  120. local exitcode=$?
  121. if [ ${exitcode} -ne 0 ]; then
  122. fn_print_fail_eol_nl
  123. if [ -f "${scriptlog}" ]; then
  124. fn_script_log_fatal "downloading ${filename}: FAIL"
  125. fi
  126. echo -e "${fileurl}" | tee -a "${scriptlog}"
  127. echo "${curlcmd}" | tee -a "${scriptlog}"
  128. core_exit.sh
  129. else
  130. fn_print_ok_eol_nl
  131. if [ -f "${scriptlog}" ]; then
  132. fn_script_log_pass "downloading ${filename}: OK"
  133. fi
  134. fi
  135. # remove trap
  136. trap - INT
  137. else
  138. fn_print_fail_eol_nl
  139. echo "Curl is not installed!"
  140. echo -e ""
  141. if [ -f "${scriptlog}" ]; then
  142. fn_script_log_fatal "Curl is not installed!"
  143. fi
  144. core_exit.sh
  145. fi
  146. # make file executecmd if executecmd is set
  147. if [ "${executecmd}" == "executecmd" ]; then
  148. chmod +x "${filedir}/${filename}"
  149. fi
  150. fi
  151. if [ -f "${filedir}/${filename}" ]; then
  152. fn_dl_md5
  153. # run file if run is set
  154. if [ "${run}" == "run" ]; then
  155. source "${filedir}/${filename}"
  156. fi
  157. fi
  158. }
  159. # fileurl: The directory the file is located in teh GitHub repo
  160. # filedir: name of file
  161. # filename: location file to be saved
  162. # executecmd: set to "executecmd" to make file executecmd
  163. # run: Optional, set to run to execute the file
  164. # force: force download of file even if exists
  165. # md5: Checks fail against an md5 sum
  166. # Fetches files from the github repo
  167. fn_fetch_file_github(){
  168. github_file_url_dir="${1}"
  169. github_file_url_name="${2}"
  170. githuburl="https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${githubbranch}/${github_file_url_dir}/${github_file_url_name}"
  171. fileurl="${githuburl}"
  172. filedir="${3}"
  173. filename="${github_file_url_name}"
  174. executecmd="${4:-0}"
  175. run="${5:-0}"
  176. force="${6:-0}"
  177. md5="${7:-0}"
  178. fn_fetch_file "${fileurl}" "${filedir}" "${filename}" "${executecmd}" "${run}" "${force}" "${md5}"
  179. }
  180. # Fetches functions
  181. fn_fetch_function(){
  182. github_file_url_dir="lgsm/functions" # github dir containing the file
  183. github_file_url_name="${functionfile}" # name of the github file
  184. githuburl="https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${githubbranch}/${github_file_url_dir}/${github_file_url_name}"
  185. fileurl="${githuburl}"
  186. filedir="${functionsdir}"
  187. filename="${github_file_url_name}"
  188. executecmd="executecmd"
  189. run="run"
  190. force="noforce"
  191. md5="nomd5"
  192. fn_fetch_file "${fileurl}" "${filedir}" "${filename}" "${executecmd}" "${run}" "${force}" "${md5}"
  193. }
  194. fn_update_function(){
  195. exitbypass=1
  196. github_file_url_dir="lgsm/functions" # github dir containing the file
  197. github_file_url_name="${functionfile}" # name of the github file
  198. githuburl="https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${githubbranch}/${github_file_url_dir}/${github_file_url_name}"
  199. fileurl="${githuburl}"
  200. filedir="${functionsdir}"
  201. filename="${github_file_url_name}"
  202. executecmd="executecmd"
  203. run="norun"
  204. force="noforce"
  205. md5="nomd5"
  206. fn_fetch_file "${fileurl}" "${filedir}" "${filename}" "${executecmd}" "${run}" "${force}" "${md5}"
  207. }