core_dl.sh 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. # extracts archives
  33. :
  34. }
  35. # Trap to remove file download if canceled before completed
  36. fn_fetch_trap() {
  37. echo ""
  38. fn_printinfomationnl "Cancelling download"
  39. sleep 1
  40. fn_printinfomation "Removing ${filename}"
  41. rm -f "${filedir}/${filename}"
  42. }
  43. # Downloads file using curl and run it if required
  44. # fn_fetch_file "fileurl" "filedir" "filename" "run" "force" "md5"
  45. fn_fetch_file(){
  46. fileurl=${1}
  47. filedir=${2}
  48. filename=${3}
  49. run=${4:-0}
  50. force=${5:-0}
  51. md5=${6}
  52. # If the file is missing, then download
  53. if [ ! -f "${filedir}/${filename}" ]; then
  54. if [ ! -d "${filedir}" ]; then
  55. mkdir -p "${filedir}"
  56. fi
  57. echo -ne " fetching ${filename}...\c"
  58. # Check curl exists and use available path
  59. 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")"
  60. for curlcmd in ${curlpaths}
  61. do
  62. if [ -x "${curlcmd}" ]; then
  63. break
  64. fi
  65. done
  66. # If curl exists download file
  67. if [ "$(basename ${curlcmd})" == "curl" ]; then
  68. # trap to remove part downloaded files
  69. trap fn_fetch_trap INT
  70. # if larger file shows progress bar
  71. if [[ $filename == *"tar"* ]]; then
  72. curlfetch=$(${curlcmd} --progress-bar --fail -o "${filedir}/${filename}" "${fileurl}")
  73. else
  74. curlfetch=$(${curlcmd} -s --fail -o "${filedir}/${filename}" "${fileurl}" 2>&1)
  75. fi
  76. if [ $? -ne 0 ]; then
  77. fn_printfaileol
  78. echo "${curlfetch}"
  79. echo -e "${fileurl}\n"
  80. exit 1
  81. else
  82. fn_printokeol
  83. fi
  84. # remove trap
  85. trap - INT
  86. else
  87. fn_printfaileol
  88. echo "Curl is not installed!"
  89. echo -e ""
  90. exit 1
  91. fi
  92. fn_dl_md5
  93. # make file executable if run is set
  94. if [ "${run}" == "run" ]; then
  95. chmod +x "${filedir}/${filename}"
  96. fi
  97. fi
  98. # run file if run is set
  99. if [ "${run}" == "run" ]; then
  100. source "${filedir}/${filename}"
  101. fi
  102. }
  103. # fn_fetch_file_github
  104. # Parameters:
  105. # github_file_url_dir: The directory the file is located in teh GitHub repo
  106. # github_file_url_name: name of file
  107. # filepath: location file to be saved
  108. # run: Optional, set to 1 to make file executable
  109. # force: force download of file even if exists
  110. fn_fetch_file_github(){
  111. github_file_url_dir=${1}
  112. github_file_url_name=${2}
  113. filepath=${3}
  114. filename="${github_file_url_name}"
  115. run=${4:-0}
  116. force=${5:-0}
  117. githuburl="https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${githubbranch}/${github_file_url_dir}/${github_file_url_name}"
  118. echo -e " fetching ${filename}...\c"
  119. fn_fetch_file "${githuburl}" "${filepath}" "${filename}" "${run}" "${force}"
  120. }
  121. # Fetches functions
  122. fn_fetch_function(){
  123. github_file_url_dir="functions" # github dir containing the file
  124. github_file_url_name="${functionfile}" # name of the github file
  125. githuburl="https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${githubbranch}/${github_file_url_dir}/${github_file_url_name}"
  126. filedir="${functionsdir}" # local dir that will contain the file
  127. filename="${github_file_url_name}" # name of the local file
  128. run="run"
  129. fn_fetch_file "${githuburl}" "${filedir}" "${filename}" "${run}"
  130. }