core_dl.sh 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. #!/bin/bash
  2. # LinuxGSM core_dl.sh function
  3. # Author: Daniel Gibbs
  4. # Contributor: UltimateByte
  5. # Website: https://linuxgsm.com
  6. # Description: Deals with all downloads for LinuxGSM.
  7. # remote_fileurl: The URL of the file: http://example.com/dl/File.tar.bz2
  8. # local_filedir: location the file is to be saved: /home/server/lgsm/tmp
  9. # local_filename: name of file (this can be different from the url name): file.tar.bz2
  10. # chmodx: Optional, set to "chmodx" to make file executable using chmod +x
  11. # run: Optional, set run to execute the file after download
  12. # forcedl: Optional, force re-download of file even if exists
  13. # md5: Optional, set an md5 sum and will compare it against the file.
  14. #
  15. # Downloads can be defined in code like so:
  16. # fn_fetch_file "${remote_fileurl}" "${local_filedir}" "${local_filename}" "${chmodx}" "${run}" "${forcedl}" "${md5}"
  17. # fn_fetch_file "http://example.com/file.tar.bz2" "/some/dir" "file.tar.bz2" "chmodx" "run" "forcedl" "10cd7353aa9d758a075c600a6dd193fd"
  18. commandname="DOWNLOAD"
  19. commandaction="Download"
  20. 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 ${local_filename} with MD5..."
  25. sleep 0.5
  26. local md5sumcmd=$(md5sum "${local_filedir}/${local_filename}"|awk '{print $1;}')
  27. if [ "${md5sumcmd}" != "${md5}" ]; then
  28. fn_print_fail_eol_nl
  29. echo "${local_filename} returned MD5 checksum: ${md5sumcmd}"
  30. echo "expected MD5 checksum: ${md5}"
  31. fn_script_log_fatal "Verifying ${local_filename} with MD5: FAIL"
  32. fn_script_log_info "${local_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 ${local_filename} with MD5: OK"
  38. fn_script_log_info "${local_filename} returned MD5 checksum: ${md5sumcmd}"
  39. fn_script_log_info "Expected MD5 checksum: ${md5}"
  40. fi
  41. fi
  42. }
  43. # Extracts bzip2, gzip or zip files
  44. # Extracts can be defined in code like so:
  45. # fn_dl_extract "${local_filedir}" "${local_filename}" "${extractdir}"
  46. # fn_dl_extract "/home/gameserver/lgsm/tmp" "file.tar.bz2" "/home/gamserver/serverfiles"
  47. fn_dl_extract(){
  48. local_filedir="${1}"
  49. local_filename="${2}"
  50. extractdir="${3}"
  51. # extracts archives
  52. echo -ne "extracting ${local_filename}..."
  53. mime=$(file -b --mime-type "${local_filedir}/${local_filename}")
  54. if [ ! -d "${extractdir}" ]; then
  55. mkdir "${extractdir}"
  56. fi
  57. if [ "${mime}" == "application/gzip" ]||[ "${mime}" == "application/x-gzip" ]; then
  58. extractcmd=$(tar -zxf "${local_filedir}/${local_filename}" -C "${extractdir}")
  59. elif [ "${mime}" == "application/x-bzip2" ]; then
  60. tarcmd=$(tar -jxf "${local_filedir}/${local_filename}" -C "${extractdir}")
  61. elif [ "${mime}" == "application/x-xz" ]; then
  62. tarcmd=$(tar -xf "${local_filedir}/${local_filename}" -C "${extractdir}")
  63. elif [ "${mime}" == "application/zip" ]; then
  64. extractcmd=$(unzip -d "${extractdir}" "${local_filedir}/${local_filename}")
  65. fi
  66. local exitcode=$?
  67. if [ ${exitcode} -ne 0 ]; then
  68. fn_print_fail_eol_nl
  69. fn_script_log_fatal "Extracting download: FAIL"
  70. if [ -f "${lgsmlog}" ]; then
  71. echo "${extractcmd}" >> "${lgsmlog}"
  72. fi
  73. echo "${extractcmd}"
  74. core_exit.sh
  75. else
  76. fn_print_ok_eol_nl
  77. fn_script_log_pass "Extracting download: OK"
  78. fi
  79. }
  80. # Trap to remove file download if canceled before completed
  81. fn_fetch_trap(){
  82. echo ""
  83. echo -ne "downloading ${local_filename}..."
  84. fn_print_canceled_eol_nl
  85. fn_script_log_info "Downloading ${local_filename}...CANCELED"
  86. sleep 0.5
  87. rm -f "${local_filedir}/${local_filename}"
  88. echo -ne "downloading ${local_filename}..."
  89. fn_print_removed_eol_nl
  90. fn_script_log_info "Downloading ${local_filename}...REMOVED"
  91. core_exit.sh
  92. }
  93. fn_fetch_file(){
  94. remote_fileurl="${1}"
  95. local_filedir="${2}"
  96. local_filename="${3}"
  97. chmodx="${4:-0}"
  98. run="${5:-0}"
  99. forcedl="${6:-0}"
  100. md5="${7:-0}"
  101. # Download file if missing or download forced
  102. if [ ! -f "${local_filedir}/${local_filename}" ]||[ "${forcedl}" == "forcedl" ]; then
  103. if [ ! -d "${local_filedir}" ]; then
  104. mkdir -p "${local_filedir}"
  105. fi
  106. # Trap will remove part downloaded files if canceled
  107. trap fn_fetch_trap INT
  108. # if larger file shows progress bar
  109. if [ "${local_filename##*.}" == "bz2" ]||[ "${local_filename##*.}" == "gz" ]||[ "${local_filename##*.}" == "zip" ]||[ "${local_filename##*.}" == "jar" ]; then
  110. echo -ne "downloading ${local_filename}..."
  111. sleep 0.5
  112. echo -ne "downloading ${local_filename}..."
  113. curlcmd=$(${curlpath} --progress-bar --fail -L -o "${local_filedir}/${local_filename}" "${remote_fileurl}")
  114. else
  115. echo -ne " fetching ${local_filename}...\c"
  116. curlcmd=$(${curlpath} -s --fail -L -o "${local_filedir}/${local_filename}" "${remote_fileurl}" 2>&1)
  117. fi
  118. local exitcode=$?
  119. if [ ${exitcode} -ne 0 ]; then
  120. fn_print_fail_eol_nl
  121. if [ -f "${lgsmlog}" ]; then
  122. fn_script_log_fatal "Downloading ${local_filename}: FAIL"
  123. echo -e "${remote_fileurl}" >> "${lgsmlog}"
  124. echo "${curlcmd}" >> "${lgsmlog}"
  125. fi
  126. echo -e "${remote_fileurl}"
  127. echo "${curlcmd}"
  128. core_exit.sh
  129. else
  130. fn_print_ok_eol_nl
  131. if [ -f "${lgsmlog}" ]; then
  132. fn_script_log_pass "Downloading ${local_filename}: OK"
  133. fi
  134. fi
  135. # Remove trap
  136. trap - INT
  137. # Make file executable if chmodx is set
  138. if [ "${chmodx}" == "chmodx" ]; then
  139. chmod +x "${local_filedir}/${local_filename}"
  140. fi
  141. fi
  142. if [ -f "${local_filedir}/${local_filename}" ]; then
  143. fn_dl_md5
  144. # Execute file if run is set
  145. if [ "${run}" == "run" ]; then
  146. source "${local_filedir}/${local_filename}"
  147. fi
  148. fi
  149. }
  150. # GitHub file download functions
  151. # Used to simplify downloading specific files from GitHub
  152. # github_file_url_dir: the directory of the file in the GitHub: lgsm/functions
  153. # github_file_url_name: the filename of the file to download from GitHub: core_messages.sh
  154. # githuburl: the full GitHub url
  155. # remote_fileurl: The URL of the file: http://example.com/dl/File.tar.bz2
  156. # local_filedir: location the file is to be saved: /home/server/lgsm/tmp
  157. # local_filename: name of file (this can be different from the url name): file.tar.bz2
  158. # chmodx: Optional, set to "chmodx" to make file executable using chmod +x
  159. # run: Optional, set run to execute the file after download
  160. # forcedl: Optional, force re-download of file even if exists
  161. # md5: Optional, set an md5 sum and will compare it against the file.
  162. # Fetches any files from the GitHub repo
  163. fn_fetch_file_github(){
  164. github_file_url_dir="${1}"
  165. github_file_url_name="${2}"
  166. githuburl="https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${githubbranch}/${github_file_url_dir}/${github_file_url_name}"
  167. remote_fileurl="${githuburl}"
  168. local_filedir="${3}"
  169. local_filename="${github_file_url_name}"
  170. chmodx="${4:-0}"
  171. run="${5:-0}"
  172. forcedl="${6:-0}"
  173. md5="${7:-0}"
  174. # Passes vars to the file download function
  175. fn_fetch_file "${remote_fileurl}" "${local_filedir}" "${local_filename}" "${chmodx}" "${run}" "${forcedl}" "${md5}"
  176. }
  177. fn_fetch_config(){
  178. github_file_url_dir="${1}"
  179. github_file_url_name="${2}"
  180. githuburl="https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${githubbranch}/${github_file_url_dir}/${github_file_url_name}"
  181. remote_fileurl="${githuburl}"
  182. local_filedir="${3}"
  183. local_filename="${4}"
  184. chmodx="nochmodx"
  185. run="norun"
  186. forcedl="noforce"
  187. md5="nomd5"
  188. # Passes vars to the file download function
  189. fn_fetch_file "${remote_fileurl}" "${local_filedir}" "${local_filename}" "${chmodx}" "${run}" "${forcedl}" "${md5}"
  190. }
  191. # Fetches functions
  192. fn_fetch_function(){
  193. github_file_url_dir="lgsm/functions"
  194. github_file_url_name="${functionfile}"
  195. githuburl="https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${githubbranch}/${github_file_url_dir}/${github_file_url_name}"
  196. remote_fileurl="${githuburl}"
  197. local_filedir="${functionsdir}"
  198. local_filename="${github_file_url_name}"
  199. chmodx="chmodx"
  200. run="run"
  201. forcedl="noforce"
  202. md5="nomd5"
  203. # Passes vars to the file download function
  204. fn_fetch_file "${remote_fileurl}" "${local_filedir}" "${local_filename}" "${chmodx}" "${run}" "${forcedl}" "${md5}"
  205. }
  206. fn_update_function(){
  207. exitbypass=1
  208. github_file_url_dir="lgsm/functions"
  209. github_file_url_name="${functionfile}"
  210. githuburl="https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${githubbranch}/${github_file_url_dir}/${github_file_url_name}"
  211. remote_fileurl="${githuburl}"
  212. local_filedir="${functionsdir}"
  213. local_filename="${github_file_url_name}"
  214. chmodx="chmodx"
  215. run="norun"
  216. forcedl="noforce"
  217. md5="nomd5"
  218. fn_fetch_file "${remote_fileurl}" "${local_filedir}" "${local_filename}" "${chmodx}" "${run}" "${forcedl}" "${md5}"
  219. }
  220. # Defines curl path
  221. curlpath=$(command -v curl 2>/dev/null)
  222. if [ "$(basename "${curlpath}")" != "curl" ]; then
  223. echo "[ FAIL ] Curl is not installed"
  224. exit 1
  225. fi