core_dl.sh 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. # Downloads can be defined in code like so
  8. # fn_dl "dl_filename" "dl_filepath" "dl_url" "dl_md5"
  9. # fn_dl "file.tar.bz2" "/home/gameserver" "http://example.com/file.tar/bz2" "10cd7353aa9d758a075c600a6dd193fd"
  10. fn_dl_md5(){
  11. # Runs MD5 Check if available
  12. if [ -n "${md5}" ]; then
  13. echo -ne "verifying ${filename} with MD5...\c"
  14. local md5check=$(md5sum "${filedir}/${filename}"|awk '{print $1;}')
  15. if [ "${md5check}" != "${dl_md5}" ]; then
  16. fn_printfaileol
  17. echo "${filename} MD5 checksum: ${md5check}"
  18. echo "expected MD5 checksum: ${dl_md5}"
  19. fn_scriptlog "failed to verify ${filename} with MD5"
  20. fn_scriptlog "${filename} MD5 checksum: ${md5check}"
  21. fn_scriptlog "expected MD5 checksum: ${dl_md5}"
  22. exit 1
  23. else
  24. fn_printokeol
  25. fn_scriptlog "verifyed ${filename} with MD5"
  26. fn_scriptlog "${filename} MD5 checksum: ${md5check}"
  27. fn_scriptlog "expected MD5 checksum: ${dl_md5}"
  28. fi
  29. fi
  30. }
  31. fn_dl_extract(){
  32. filedir=${1}
  33. filename=${2}
  34. extractdir=${3}
  35. # extracts archives
  36. echo -ne "extracting ${filename}..."
  37. mime=$(file -b --mime-type "${filedir}/${filename}")
  38. if [ "${mime}" == "application/gzip" ]; then
  39. tarcmd=$(tar -zxf "${filedir}/${filename}" -C "${extractdir}")
  40. elif [ "${mime}" == "application/x-bzip2" ]; then
  41. tarcmd=$(tar -jxf "${filedir}/${filename}" -C "${extractdir}")
  42. fi
  43. local exitcode=$?
  44. if [ ${exitcode} -ne 0 ]; then
  45. fn_printfaileol
  46. echo "${tarcmd}"
  47. exit ${exitcode}
  48. else
  49. fn_printokeol
  50. fi
  51. }
  52. # Trap to remove file download if canceled before completed
  53. fn_fetch_trap() {
  54. echo ""
  55. fn_printinfomationnl "Cancelling download"
  56. sleep 1
  57. fn_printinfomation "Removing ${filename}"
  58. rm -f "${filedir}/${filename}"
  59. }
  60. # Downloads file using curl and run it if required
  61. # fn_fetch_file "fileurl" "filedir" "filename" "run" "force" "md5"
  62. fn_fetch_file(){
  63. fileurl=${1}
  64. filedir=${2}
  65. filename=${3}
  66. run=${4:-0}
  67. force=${5:-0}
  68. md5=${6}
  69. # If the file is missing, then download
  70. if [ ! -f "${filedir}/${filename}" ]; then
  71. if [ ! -d "${filedir}" ]; then
  72. mkdir -p "${filedir}"
  73. fi
  74. # Check curl exists and use available path
  75. 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")"
  76. for curlcmd in ${curlpaths}
  77. do
  78. if [ -x "${curlcmd}" ]; then
  79. break
  80. fi
  81. done
  82. # If curl exists download file
  83. if [ "$(basename ${curlcmd})" == "curl" ]; then
  84. # trap to remove part downloaded files
  85. trap fn_fetch_trap INT
  86. # if larger file shows progress bar
  87. if [[ $filename == *"tar"* ]]; then
  88. echo -ne "downloading ${filename}..."
  89. sleep 1
  90. curlcmd=$(${curlcmd} --progress-bar --fail -o "${filedir}/${filename}" "${fileurl}")
  91. echo -ne "downloading ${filename}..."
  92. else
  93. echo -ne " fetching ${filename}...\c"
  94. curlcmd=$(${curlcmd} -s --fail -o "${filedir}/${filename}" "${fileurl}" 2>&1)
  95. fi
  96. local exitcode=$?
  97. if [ ${exitcode} -ne 0 ]; then
  98. fn_printfaileol
  99. echo "${curlcmd}"
  100. echo -e "${fileurl}\n"
  101. exit ${exitcode}
  102. else
  103. fn_printokeol
  104. fi
  105. # remove trap
  106. trap - INT
  107. else
  108. fn_printfaileol
  109. echo "Curl is not installed!"
  110. echo -e ""
  111. exit 1
  112. fi
  113. fn_dl_md5
  114. # make file executable if run is set
  115. if [ "${run}" == "run" ]; then
  116. chmod +x "${filedir}/${filename}"
  117. fi
  118. fi
  119. # run file if run is set
  120. if [ "${run}" == "run" ]; then
  121. source "${filedir}/${filename}"
  122. fi
  123. }
  124. # fn_fetch_file_github
  125. # Parameters:
  126. # github_file_url_dir: The directory the file is located in teh GitHub repo
  127. # github_file_url_name: name of file
  128. # filepath: location file to be saved
  129. # run: Optional, set to 1 to make file executable
  130. # force: force download of file even if exists
  131. fn_fetch_file_github(){
  132. github_file_url_dir=${1}
  133. github_file_url_name=${2}
  134. filepath=${3}
  135. filename="${github_file_url_name}"
  136. run=${4:-0}
  137. force=${5:-0}
  138. githuburl="https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${githubbranch}/${github_file_url_dir}/${github_file_url_name}"
  139. echo -e " fetching ${filename}...\c"
  140. fn_fetch_file "${githuburl}" "${filepath}" "${filename}" "${run}" "${force}"
  141. }
  142. # Fetches functions
  143. fn_fetch_function(){
  144. github_file_url_dir="functions" # github dir containing the file
  145. github_file_url_name="${functionfile}" # name of the github file
  146. githuburl="https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${githubbranch}/${github_file_url_dir}/${github_file_url_name}"
  147. filedir="${functionsdir}" # local dir that will contain the file
  148. filename="${github_file_url_name}" # name of the local file
  149. run="run"
  150. fn_fetch_file "${githuburl}" "${filedir}" "${filename}" "${run}"
  151. }