4
0

core_dl.sh 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 [ -n "${md5}" ]; 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_printfaileolnl
  26. echo "${filename} returned MD5 checksum: ${md5sumcmd}"
  27. echo "expected MD5 checksum: ${md5}"
  28. fn_scriptlog "failed to verify ${filename} with MD5"
  29. fn_scriptlog "${filename} returned MD5 checksum: ${md5sumcmd}"
  30. fn_scriptlog "expected MD5 checksum: ${md5}"
  31. exit 1
  32. else
  33. fn_printokeolnl
  34. fn_scriptlog "verifyed ${filename} with MD5"
  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_printfaileolnl
  60. fn_scriptlog "extracting download: FAIL"
  61. echo "${tarcmd}" | tee -a "${scriptlog}"
  62. exit ${exitcode}
  63. else
  64. fn_printokeolnl
  65. fi
  66. }
  67. # Trap to remove file download if canceled before completed
  68. fn_fetch_trap() {
  69. echo ""
  70. fn_printinfomationnl "cancelling download"
  71. fn_scriptlog "canceling download"
  72. sleep 1
  73. fn_printinfomation "removing ${filename}"
  74. fn_scriptlog "removing ${filename}"
  75. rm -f "${filedir}/${filename}" | tee -a "${scriptlog}"
  76. }
  77. fn_fetch_file(){
  78. fileurl=${1}
  79. filedir=${2}
  80. filename=${3}
  81. executecmd=${4:-0}
  82. run=${5:-0}
  83. force=${6:-0}
  84. md5=${7}
  85. # If the file is missing, then download
  86. if [ ! -f "${filedir}/${filename}" ]; then
  87. if [ ! -d "${filedir}" ]; then
  88. mkdir -p "${filedir}"
  89. fi
  90. # Check curl exists and use available path
  91. curlpaths="$(command -v curl 2>/dev/null) $(which curl >/dev/null 2>&1) /usr/bin/curl /bin/curl /usr/sbin/curl /sbin/curl $(echo $PATH | sed "s/\([:]\|\$\)/\/curl /g")"
  92. for curlcmd in ${curlpaths}
  93. do
  94. if [ -x "${curlcmd}" ]; then
  95. break
  96. fi
  97. done
  98. # If curl exists download file
  99. if [ "$(basename ${curlcmd})" == "curl" ]; then
  100. # trap to remove part downloaded files
  101. trap fn_fetch_trap INT
  102. # if larger file shows progress bar
  103. if [ ${filename##*.} == "bz2" ]; then
  104. echo -ne "downloading ${filename}..."
  105. fn_scriptlog "downloading ${filename}"
  106. sleep 1
  107. curlcmd=$(${curlcmd} --progress-bar --fail -o "${filedir}/${filename}" "${fileurl}")
  108. echo -ne "downloading ${filename}..."
  109. else
  110. echo -ne " fetching ${filename}...\c"
  111. fn_scriptlog "fetching ${filename}"
  112. curlcmd=$(${curlcmd} -s --fail -o "${filedir}/${filename}" "${fileurl}" 2>&1)
  113. fi
  114. local exitcode=$?
  115. if [ ${exitcode} -ne 0 ]; then
  116. fn_printfaileolnl
  117. fn_scriptlog "downloading ${filename}: FAIL"
  118. echo "${curlcmd}" | tee -a "${scriptlog}"
  119. echo -e "${fileurl}\n" | tee -a "${scriptlog}"
  120. exit ${exitcode}
  121. else
  122. fn_printokeolnl
  123. fn_scriptlog "downloading ${filename}: OK"
  124. fi
  125. # remove trap
  126. trap - INT
  127. else
  128. fn_printfaileolnl
  129. echo "Curl is not installed!"
  130. fn_scriptlog "Curl is not installed!"
  131. echo -e ""
  132. exit 1
  133. fi
  134. # make file executecmd if executecmd is set
  135. if [ "${executecmd}" == "executecmd" ]; then
  136. chmod +x "${filedir}/${filename}"
  137. fi
  138. fi
  139. if [ -f "${filedir}/${filename}" ]; then
  140. fn_dl_md5
  141. # run file if run is set
  142. if [ "${run}" == "run" ]; then
  143. source "${filedir}/${filename}"
  144. fi
  145. fi
  146. }
  147. # fileurl: The directory the file is located in teh GitHub repo
  148. # filedir: name of file
  149. # filename: location file to be saved
  150. # executecmd: set to "executecmd" to make file executecmd
  151. # run: Optional, set to run to execute the file
  152. # force: force download of file even if exists
  153. # md5: Checks fail against an md5 sum
  154. # Fetches files from the github repo
  155. fn_fetch_file_github(){
  156. github_file_url_dir="${1}"
  157. github_file_url_name="${2}"
  158. githuburl="https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${githubbranch}/${github_file_url_dir}/${github_file_url_name}"
  159. fileurl="${githuburl}"
  160. filedir="${3}"
  161. filename="${github_file_url_name}"
  162. executecmd="${4:-0}"
  163. run="${5:-0}"
  164. force="${6:-0}"
  165. md5="${7:-0}"
  166. fn_fetch_file "${fileurl}" "${filedir}" "${filename}" "${executecmd}" "${run}" "${force}" "${md5}"
  167. }
  168. # Fetches functions
  169. fn_fetch_function(){
  170. github_file_url_dir="functions" # github dir containing the file
  171. github_file_url_name="${functionfile}" # name of the github file
  172. githuburl="https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${githubbranch}/${github_file_url_dir}/${github_file_url_name}"
  173. fileurl="${githuburl}"
  174. filedir="${functionsdir}"
  175. filename="${github_file_url_name}"
  176. executecmd="executecmd"
  177. run="run"
  178. force="noforce"
  179. md5=""
  180. fn_fetch_file "${fileurl}" "${filedir}" "${filename}" "${executecmd}" "${run}" "${force}" "${md5}"
  181. }