core_dl.sh 4.7 KB

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