core_dl.sh 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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_file(){
  32. # defines variables from other script file
  33. dl_filename=$1
  34. dl_filepath=$2
  35. dl_url=$3
  36. dl_md5=$4
  37. if [ ! -f "${dl_filepath}/${dl_filename}" ]||[ -n "${retry_dl}" ]; then
  38. echo -ne "downloading ${dl_filename}..."
  39. dl=$(curl --progress-bar --fail -o "${dl_filepath}/${dl_filename}" "${dl_url}")
  40. exitcode=$?
  41. echo -ne "downloading ${dl_filename}...\c"
  42. if [ ${exitcode} -ne 0 ]; then
  43. fn_printfaileol
  44. echo -e "${dl_url}\n"
  45. exit ${exitcode}
  46. else
  47. fn_printokeol
  48. fi
  49. else
  50. echo -e "${dl_filename} already exists...\c"
  51. fn_dl_md5
  52. while true; do
  53. read -e -i "n" -p "Download again? [y/N]" yn
  54. case $yn in
  55. [Yy]* ) fn_dl; retry_dl=1;;
  56. [Nn]* ) break;;
  57. * ) echo "Please answer yes or no.";;
  58. esac
  59. done
  60. fi
  61. fn_dl_md5
  62. }
  63. # Downloads file using curl and run it if required
  64. # fn_fetch_file "fileurl" "filedir" "filename" "run" "force" "md5"
  65. fn_fetch_file(){
  66. fileurl=${1}
  67. filedir=${2}
  68. filename=${3}
  69. run=${4:-0}
  70. force=${5:-0}
  71. md5=${6}
  72. # If the file is missing, then download
  73. if [ ! -f "${filedir}/${filename}" ]; then
  74. if [ ! -d "${filedir}" ]; then
  75. mkdir -p "${filedir}"
  76. fi
  77. echo -e " fetching ${filename}...\c"
  78. # Check curl exists and use available path
  79. 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")"
  80. for curlcmd in ${curlpaths}
  81. do
  82. if [ -x "${curlcmd}" ]; then
  83. break
  84. fi
  85. done
  86. # If curl exists download file
  87. if [ "$(basename ${curlcmd})" == "curl" ]; then
  88. # if larger file shows progress bar
  89. if [ "${filename}" == *".tar"* ]; then
  90. curlfetch=$(${curlcmd} --progress-bar -s --fail -o "${filedir}/${filename}" "${fileurl}" 2>&1)
  91. else
  92. curlfetch=$(${curlcmd} -s --fail -o "${filedir}/${filename}" "${fileurl}" 2>&1)
  93. fi
  94. if [ $? -ne 0 ]; then
  95. fn_printfaileol
  96. echo "${curlfetch}"
  97. echo -e "${fileurl}\n"
  98. exit 1
  99. else
  100. fn_printokeol
  101. fi
  102. else
  103. fn_printfaileol
  104. echo "Curl is not installed!"
  105. echo -e ""
  106. exit 1
  107. fi
  108. fn_dl_md5
  109. # make file executable if run is set
  110. if [ "${run}" == "run" ]; then
  111. chmod +x "${filedir}/${filename}"
  112. fi
  113. fi
  114. # run file if run is set
  115. if [ "${run}" == "run" ]; then
  116. source "${filedir}/${filename}"
  117. fi
  118. }
  119. # fn_fetch_file_github
  120. # Parameters:
  121. # github_file_url_dir: The directory the file is located in teh GitHub repo
  122. # github_file_url_name: name of file
  123. # filepath: location file to be saved
  124. # run: Optional, set to 1 to make file executable
  125. # force: force download of file even if exists
  126. fn_fetch_file_github(){
  127. github_file_url_dir=${1}
  128. github_file_url_name=${2}
  129. filepath=${3}
  130. filename="${github_file_url_name}"
  131. run=${4:-0}
  132. force=${5:-0}
  133. githuburl="https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${githubbranch}/${github_file_url_dir}/${github_file_url_name}"
  134. echo -e " fetching ${filename}...\c"
  135. fn_fetch_file "${githuburl}" "${filepath}" "${filename}" "${run}" "${force}"
  136. }
  137. # Fetches functions
  138. fn_fetch_function(){
  139. github_file_url_dir="functions" # github dir containing the file
  140. github_file_url_name="${functionfile}" # name of the github file
  141. githuburl="https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${githubbranch}/${github_file_url_dir}/${github_file_url_name}"
  142. filedir="${functionsdir}" # local dir that will contain the file
  143. filename="${github_file_url_name}" # name of the local file
  144. run="run"
  145. fn_fetch_file "${githuburl}" "${filedir}" "${filename}" "${run}"
  146. }