core_dl.sh 7.4 KB

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