core_dl.sh 7.0 KB

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