core_dl.sh 6.0 KB

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