core_dl.sh 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. #!/bin/bash
  2. # LinuxGSM core_dl.sh module
  3. # Author: Daniel Gibbs
  4. # Contributors: http://linuxgsm.com/contrib
  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. # hash: Optional, set an hash 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}" "${hash}"
  17. # fn_fetch_file "http://example.com/file.tar.bz2" "/some/dir" "file.tar.bz2" "chmodx" "run" "forcedl" "10cd7353aa9d758a075c600a6dd193fd"
  18. functionselfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")"
  19. fn_dl_steamcmd(){
  20. fn_print_start_nl "${remotelocation}"
  21. fn_script_log_info "${commandaction} ${selfname}: ${remotelocation}"
  22. if [ -n "${branch}" ]; then
  23. echo -e "Branch: ${branch}"
  24. fn_script_log_info "Branch: ${branch}"
  25. fi
  26. if [ -n "${betapassword}" ]; then
  27. echo -e "Branch password: ${betapassword}"
  28. fn_script_log_info "Branch password: ${betapassword}"
  29. fi
  30. if [ -d "${steamcmddir}" ]; then
  31. cd "${steamcmddir}" || exit
  32. fi
  33. # Unbuffer will allow the output of steamcmd not buffer allowing a smooth output.
  34. # unbuffer us part of the expect package.
  35. if [ "$(command -v unbuffer)" ]; then
  36. unbuffer="unbuffer"
  37. fi
  38. # Validate will be added as a parameter if required.
  39. if [ "${commandname}" == "VALIDATE" ]||[ "${commandname}" == "INSTALL" ]; then
  40. validate="validate"
  41. fi
  42. # To do error checking for SteamCMD the output of steamcmd will be saved to a log.
  43. steamcmdlog="${lgsmlogdir}/${selfname}-steamcmd.log"
  44. # clear previous steamcmd log
  45. if [ -f "${steamcmdlog}" ]; then
  46. rm -f "${steamcmdlog:?}"
  47. fi
  48. counter=0
  49. while [ "${counter}" == "0" ]||[ "${exitcode}" != "0" ]; do
  50. counter=$((counter+1))
  51. # Select SteamCMD parameters
  52. # If GoldSrc (appid 90) servers. GoldSrc (appid 90) require extra commands.
  53. if [ "${appid}" == "90" ]; then
  54. # If using a specific branch.
  55. if [ -n "${branch}" ]&&[ -n "${betapassword}" ]; then
  56. ${unbuffer} ${steamcmdcommand} +login "${steamuser}" "${steampass}" +force_install_dir "${serverfiles}" +app_set_config 90 mod "${appidmod}" +app_update "${appid}" -beta "${branch}" -betapassword "${betapassword}" ${validate} +quit | uniq | tee -a "${lgsmlog}" "${steamcmdlog}"
  57. elif [ -n "${branch}" ]; then
  58. ${unbuffer} ${steamcmdcommand} +login "${steamuser}" "${steampass}" +force_install_dir "${serverfiles}" +app_set_config 90 mod "${appidmod}" +app_update "${appid}" -beta "${branch}" ${validate} +quit | uniq | tee -a "${lgsmlog}" "${steamcmdlog}"
  59. else
  60. ${unbuffer} ${steamcmdcommand} +login "${steamuser}" "${steampass}" +force_install_dir "${serverfiles}" +app_set_config 90 mod "${appidmod}" +app_update "${appid}" ${validate} +quit | uniq | tee -a "${lgsmlog}" "${steamcmdlog}"
  61. fi
  62. # Force Windows Platform type.
  63. elif [ "${steamcmdforcewindows}" == "yes" ]; then
  64. if [ -n "${branch}" ]&&[ -n "${betapassword}" ]; then
  65. ${unbuffer} ${steamcmdcommand} +@sSteamCmdForcePlatformType windows +login "${steamuser}" "${steampass}" +force_install_dir "${serverfiles}" +app_update "${appid}" -beta "${branch}" -betapassword "${betapassword}" ${validate} +quit | uniq | tee -a "${lgsmlog}" "${steamcmdlog}"
  66. elif [ -n "${branch}" ]; then
  67. ${unbuffer} ${steamcmdcommand} +@sSteamCmdForcePlatformType windows +login "${steamuser}" "${steampass}" +force_install_dir "${serverfiles}" +app_update "${appid}" -beta "${branch}" ${validate} +quit | uniq | tee -a "${lgsmlog}" "${steamcmdlog}"
  68. else
  69. ${unbuffer} ${steamcmdcommand} +@sSteamCmdForcePlatformType windows +login "${steamuser}" "${steampass}" +force_install_dir "${serverfiles}" +app_update "${appid}" ${validate} +quit | uniq | tee -a "${lgsmlog}" "${steamcmdlog}"
  70. fi
  71. # All other servers.
  72. else
  73. if [ -n "${branch}" ]&&[ -n "${betapassword}" ]; then
  74. ${unbuffer} ${steamcmdcommand} +login "${steamuser}" "${steampass}" +force_install_dir "${serverfiles}" +app_update "${appid}" -beta "${branch}" -betapassword "${betapassword}" ${validate} +quit | uniq | tee -a "${lgsmlog}" "${steamcmdlog}"
  75. elif [ -n "${branch}" ]; then
  76. ${unbuffer} ${steamcmdcommand} +login "${steamuser}" "${steampass}" +force_install_dir "${serverfiles}" +app_update "${appid}" -beta "${branch}" ${validate} +quit | uniq | tee -a "${lgsmlog}" "${steamcmdlog}"
  77. else
  78. ${unbuffer} ${steamcmdcommand} +login "${steamuser}" "${steampass}" +force_install_dir "${serverfiles}" +app_update "${appid}" ${validate} +quit | uniq | tee -a "${lgsmlog}" "${steamcmdlog}"
  79. fi
  80. fi
  81. # Error checking for SteamCMD. Some errors will loop to try again and some will just exit.
  82. # Check also if we have more errors than retries to be sure that we do not loop to many times and error out.
  83. exitcode=$?
  84. if [ -n "$(grep -i "Error!" "${steamcmdlog}" | tail -1)" ]&&[ "$(grep -ic "Error!" "${steamcmdlog}")" -ge "${counter}" ] ; then
  85. # Not enough space.
  86. if [ -n "$(grep "0x202" "${steamcmdlog}" | tail -1)" ]; then
  87. fn_print_failure_nl "${commandaction} ${selfname}: ${remotelocation}: Not enough space to download server files"
  88. fn_script_log_fatal "${commandaction} ${selfname}: ${remotelocation}: Not enough space to download server files"
  89. core_exit.sh
  90. # Need tp purchase game.
  91. elif [ -n "$(grep "No subscription" "${steamcmdlog}" | tail -1)" ]; then
  92. fn_print_failure_nl "${commandaction} ${selfname}: ${remotelocation}: Steam account does not have a license for the required game"
  93. fn_script_log_fatal "${commandaction} ${selfname}: ${remotelocation}: Steam account does not have a license for the required game"
  94. core_exit.sh
  95. # Two-factor authentication failure
  96. elif [ -n "$(grep "Two-factor code mismatch" "${steamcmdlog}" | tail -1)" ]; then
  97. fn_print_failure_nl "${commandaction} ${selfname}: ${remotelocation}: Two-factor authentication failure"
  98. fn_script_log_fatal "${commandaction} ${selfname}: ${remotelocation}: Two-factor authentication failure"
  99. core_exit.sh
  100. # Incorrect Branch password
  101. elif [ -n "$(grep "Password check for AppId" "${steamcmdlog}" | tail -1)" ]; then
  102. fn_print_failure_nl "${commandaction} ${selfname}: ${remotelocation}: betapassword is incorrect"
  103. fn_script_log_fatal "${commandaction} ${selfname}: ${remotelocation}: betapassword is incorrect"
  104. core_exit.sh
  105. # Update did not finish.
  106. elif [ -n "$(grep "0x402" "${steamcmdlog}" | tail -1)" ]||[ -n "$(grep "0x602" "${steamcmdlog}" | tail -1)" ]; then
  107. fn_print_error2_nl "${commandaction} ${selfname}: ${remotelocation}: Update required but not completed - check network"
  108. fn_script_log_error "${commandaction} ${selfname}: ${remotelocation}: Update required but not completed - check network"
  109. else
  110. fn_print_error2_nl "${commandaction} ${selfname}: ${remotelocation}: Unknown error occured"
  111. fn_script_log_error "${commandaction} ${selfname}: ${remotelocation}: Unknown error occured"
  112. fi
  113. elif [ "${exitcode}" != "0" ]; then
  114. fn_print_error2_nl "${commandaction} ${selfname}: ${remotelocation}: Exit code: ${exitcode}"
  115. fn_script_log_error "${commandaction} ${selfname}: ${remotelocation}: Exit code: ${exitcode}"
  116. else
  117. fn_print_complete_nl "${commandaction} ${selfname}: ${remotelocation}"
  118. fn_script_log_pass "${commandaction} ${selfname}: ${remotelocation}"
  119. fi
  120. if [ "${counter}" -gt "10" ]; then
  121. fn_print_failure_nl "${commandaction} ${selfname}: ${remotelocation}: Did not complete the download, too many retrys"
  122. fn_script_log_fatal "${commandaction} ${selfname}: ${remotelocation}: Did not complete the download, too many retrys"
  123. core_exit.sh
  124. fi
  125. done
  126. }
  127. # Emptys contents of the LinuxGSM tmpdir.
  128. fn_clear_tmp(){
  129. echo -en "clearing LinuxGSM tmp directory..."
  130. if [ -d "${tmpdir}" ]; then
  131. rm -rf "${tmpdir:?}/"*
  132. local exitcode=$?
  133. if [ "${exitcode}" == 0 ]; then
  134. fn_print_ok_eol_nl
  135. fn_script_log_pass "clearing LinuxGSM tmp directory"
  136. else
  137. fn_print_error_eol_nl
  138. fn_script_log_error "clearing LinuxGSM tmp directory"
  139. fi
  140. fi
  141. }
  142. fn_dl_hash(){
  143. # Runs Hash Check if available.
  144. if [ "${hash}" != "0" ]&&[ "${hash}" != "nohash" ]&&[ "${hash}" != "nomd5" ]; then
  145. # MD5
  146. if [ "${#hash}" == "32" ]; then
  147. hashbin="md5sum"
  148. hashtype="MD5"
  149. # SHA1
  150. elif [ "${#hash}" == "40" ]; then
  151. hashbin="sha1sum"
  152. hashtype="SHA1"
  153. # SHA256
  154. elif [ "${#hash}" == "64" ]; then
  155. hashbin="sha256sum"
  156. hashtype="SHA256"
  157. # SHA512
  158. elif [ "${#hash}" == "128" ]; then
  159. hashbin="sha512sum"
  160. hashtype="SHA512"
  161. else
  162. fn_script_log_error "hash lengh not known for hash type"
  163. fn_print_error_nl "hash lengh not known for hash type"
  164. core_exit.sh
  165. fi
  166. echo -en "verifying ${local_filename} with ${hashtype}..."
  167. fn_sleep_time
  168. hashsumcmd=$(${hashbin} "${local_filedir}/${local_filename}" | awk '{print $1}')
  169. if [ "${hashsumcmd}" != "${hash}" ]; then
  170. fn_print_fail_eol_nl
  171. echo -e "${local_filename} returned ${hashtype} checksum: ${hashsumcmd}"
  172. echo -e "expected ${hashtype} checksum: ${hash}"
  173. fn_script_log_fatal "Verifying ${local_filename} with ${hashtype}"
  174. fn_script_log_info "${local_filename} returned ${hashtype} checksum: ${hashsumcmd}"
  175. fn_script_log_info "Expected ${hashtype} checksum: ${hash}"
  176. core_exit.sh
  177. else
  178. fn_print_ok_eol_nl
  179. fn_script_log_pass "Verifying ${local_filename} with ${hashtype}"
  180. fn_script_log_info "${local_filename} returned ${hashtype} checksum: ${hashsumcmd}"
  181. fn_script_log_info "Expected ${hashtype} checksum: ${hash}"
  182. fi
  183. fi
  184. }
  185. # Extracts bzip2, gzip or zip files.
  186. # Extracts can be defined in code like so:
  187. # fn_dl_extract "${local_filedir}" "${local_filename}" "${extractdir}"
  188. # fn_dl_extract "/home/gameserver/lgsm/tmp" "file.tar.bz2" "/home/gamserver/serverfiles"
  189. fn_dl_extract(){
  190. local_filedir="${1}"
  191. local_filename="${2}"
  192. extractdir="${3}"
  193. # Extracts archives.
  194. echo -en "extracting ${local_filename}..."
  195. mime=$(file -b --mime-type "${local_filedir}/${local_filename}")
  196. if [ ! -d "${extractdir}" ]; then
  197. mkdir "${extractdir}"
  198. fi
  199. if [ "${mime}" == "application/gzip" ]||[ "${mime}" == "application/x-gzip" ]; then
  200. extractcmd=$(tar -zxf "${local_filedir}/${local_filename}" -C "${extractdir}")
  201. elif [ "${mime}" == "application/x-bzip2" ]; then
  202. extractcmd=$(tar -jxf "${local_filedir}/${local_filename}" -C "${extractdir}")
  203. elif [ "${mime}" == "application/x-xz" ]; then
  204. extractcmd=$(tar -xf "${local_filedir}/${local_filename}" -C "${extractdir}")
  205. elif [ "${mime}" == "application/zip" ]; then
  206. extractcmd=$(unzip -qo -d "${extractdir}" "${local_filedir}/${local_filename}")
  207. fi
  208. local exitcode=$?
  209. if [ "${exitcode}" != 0 ]; then
  210. fn_print_fail_eol_nl
  211. fn_script_log_fatal "Extracting download"
  212. if [ -f "${lgsmlog}" ]; then
  213. echo -e "${extractcmd}" >> "${lgsmlog}"
  214. fi
  215. echo -e "${extractcmd}"
  216. core_exit.sh
  217. else
  218. fn_print_ok_eol_nl
  219. fn_script_log_pass "Extracting download"
  220. fi
  221. }
  222. # Trap to remove file download if canceled before completed.
  223. fn_fetch_trap(){
  224. echo -e ""
  225. echo -en "downloading ${local_filename}..."
  226. fn_print_canceled_eol_nl
  227. fn_script_log_info "Downloading ${local_filename}...CANCELED"
  228. fn_sleep_time
  229. rm -f "${local_filedir:?}/${local_filename}"
  230. echo -en "downloading ${local_filename}..."
  231. fn_print_removed_eol_nl
  232. fn_script_log_info "Downloading ${local_filename}...REMOVED"
  233. core_exit.sh
  234. }
  235. fn_fetch_file(){
  236. remote_fileurl="${1}"
  237. remote_fileurl_backup="${2}"
  238. remote_fileurl_name="${3}"
  239. remote_fileurl_backup_name="${4}"
  240. local_filedir="${5}"
  241. local_filename="${6}"
  242. chmodx="${7:-0}"
  243. run="${8:-0}"
  244. forcedl="${9:-0}"
  245. hash="${10:-0}"
  246. # Download file if missing or download forced.
  247. if [ ! -f "${local_filedir}/${local_filename}" ]||[ "${forcedl}" == "forcedl" ]; then
  248. # If backup fileurl exists include it.
  249. if [ -n "${remote_fileurl_backup}" ]; then
  250. # counter set to 0 to allow second try
  251. counter=0
  252. remote_fileurls_array=( remote_fileurl remote_fileurl_backup )
  253. else
  254. # counter set to 1 to not allow second try
  255. counter=1
  256. remote_fileurls_array=( remote_fileurl )
  257. fi
  258. for remote_fileurl_array in "${remote_fileurls_array[@]}"; do
  259. if [ "${remote_fileurl_array}" == "remote_fileurl" ]; then
  260. fileurl="${remote_fileurl}"
  261. fileurl_name="${remote_fileurl_name}"
  262. elif [ "${remote_fileurl_array}" == "remote_fileurl_backup" ]; then
  263. fileurl="${remote_fileurl_backup}"
  264. fileurl_name="${remote_fileurl_backup_name}"
  265. fi
  266. counter=$((counter+1))
  267. if [ ! -d "${local_filedir}" ]; then
  268. mkdir -p "${local_filedir}"
  269. fi
  270. # Trap will remove part downloaded files if canceled.
  271. trap fn_fetch_trap INT
  272. # Larger files show a progress bar.
  273. if [ "${local_filename##*.}" == "bz2" ]||[ "${local_filename##*.}" == "gz" ]||[ "${local_filename##*.}" == "zip" ]||[ "${local_filename##*.}" == "jar" ]||[ "${local_filename##*.}" == "xz" ]; then
  274. echo -en "downloading ${local_filename}..."
  275. fn_sleep_time
  276. echo -en "\033[1K"
  277. curlcmd=$(curl --connect-timeout 10 --progress-bar --fail -L -o "${local_filedir}/${local_filename}" "${fileurl}")
  278. echo -en "downloading ${local_filename}..."
  279. else
  280. echo -en "fetching ${fileurl_name} ${local_filename}...\c"
  281. curlcmd=$(curl --connect-timeout 10 -s --fail -L -o "${local_filedir}/${local_filename}" "${fileurl}" 2>&1)
  282. fi
  283. local exitcode=$?
  284. # Download will fail if downloads a html file.
  285. if [ -f "${local_filedir}/${local_filename}" ]; then
  286. if [ -n "$(head "${local_filedir}/${local_filename}" | grep "DOCTYPE" )" ]; then
  287. rm "${local_filedir:?}/${local_filename:?}"
  288. local exitcode=2
  289. fi
  290. fi
  291. # On first try will error. On second try will fail.
  292. if [ "${exitcode}" != 0 ]; then
  293. if [ ${counter} -ge 2 ]; then
  294. fn_print_fail_eol_nl
  295. if [ -f "${lgsmlog}" ]; then
  296. fn_script_log_fatal "Downloading ${local_filename}"
  297. fn_script_log_fatal "${fileurl}"
  298. fi
  299. core_exit.sh
  300. else
  301. fn_print_error_eol_nl
  302. if [ -f "${lgsmlog}" ]; then
  303. fn_script_log_error "Downloading ${local_filename}"
  304. fn_script_log_error "${fileurl}"
  305. fi
  306. fi
  307. else
  308. fn_print_ok_eol
  309. sleep 0.3
  310. echo -en "\033[2K\\r"
  311. if [ -f "${lgsmlog}" ]; then
  312. fn_script_log_pass "Downloading ${local_filename}"
  313. fi
  314. # Make file executable if chmodx is set.
  315. if [ "${chmodx}" == "chmodx" ]; then
  316. chmod +x "${local_filedir}/${local_filename}"
  317. fi
  318. # Remove trap.
  319. trap - INT
  320. break
  321. fi
  322. done
  323. fi
  324. if [ -f "${local_filedir}/${local_filename}" ]; then
  325. fn_dl_hash
  326. # Execute file if run is set.
  327. if [ "${run}" == "run" ]; then
  328. # shellcheck source=/dev/null
  329. source "${local_filedir}/${local_filename}"
  330. fi
  331. fi
  332. }
  333. # GitHub file download functions.
  334. # Used to simplify downloading specific files from GitHub.
  335. # github_file_url_dir: the directory of the file in the GitHub: lgsm/functions
  336. # github_file_url_name: the filename of the file to download from GitHub: core_messages.sh
  337. # githuburl: the full GitHub url
  338. # remote_fileurl: The URL of the file: http://example.com/dl/File.tar.bz2
  339. # local_filedir: location the file is to be saved: /home/server/lgsm/tmp
  340. # local_filename: name of file (this can be different from the url name): file.tar.bz2
  341. # chmodx: Optional, set to "chmodx" to make file executable using chmod +x
  342. # run: Optional, set run to execute the file after download
  343. # forcedl: Optional, force re-download of file even if exists
  344. # hash: Optional, set an hash sum and will compare it against the file.
  345. # Fetches files from the Git repo.
  346. fn_fetch_file_github(){
  347. github_file_url_dir="${1}"
  348. github_file_url_name="${2}"
  349. # For legacy versions - code can be removed at a future date
  350. if [ "${legacymode}" == "1" ]; then
  351. remote_fileurl="https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${githubbranch}/${github_file_url_dir}/${github_file_url_name}"
  352. remote_fileurl_backup="https://bitbucket.org/${githubuser}/${githubrepo}/raw/${githubbranch}/${github_file_url_dir}/${github_file_url_name}"
  353. # If master branch will currently running LinuxGSM version to prevent "version mixing". This is ignored if a fork.
  354. elif [ "${githubbranch}" == "master" ]&&[ "${githubuser}" == "GameServerManager" ]&&[ "${commandname}" != "UPDATE-LGSM" ]; then
  355. remote_fileurl="https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${version}/${github_file_url_dir}/${github_file_url_name}"
  356. remote_fileurl_backup="https://bitbucket.org/${githubuser}/${githubrepo}/raw/${version}/${github_file_url_dir}/${github_file_url_name}"
  357. else
  358. remote_fileurl="https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${githubbranch}/${github_file_url_dir}/${github_file_url_name}"
  359. remote_fileurl_backup="https://bitbucket.org/${githubuser}/${githubrepo}/raw/${githubbranch}/${github_file_url_dir}/${github_file_url_name}"
  360. fi
  361. remote_fileurl_name="GitHub"
  362. remote_fileurl_backup_name="Bitbucket"
  363. local_filedir="${3}"
  364. local_filename="${github_file_url_name}"
  365. chmodx="${4:-0}"
  366. run="${5:-0}"
  367. forcedl="${6:-0}"
  368. hash="${7:-0}"
  369. # Passes vars to the file download function.
  370. fn_fetch_file "${remote_fileurl}" "${remote_fileurl_backup}" "${remote_fileurl_name}" "${remote_fileurl_backup_name}" "${local_filedir}" "${local_filename}" "${chmodx}" "${run}" "${forcedl}" "${hash}"
  371. }
  372. # Fetches config files from the Git repo.
  373. fn_fetch_config(){
  374. github_file_url_dir="${1}"
  375. github_file_url_name="${2}"
  376. # If master branch will currently running LinuxGSM version to prevent "version mixing". This is ignored if a fork.
  377. if [ "${githubbranch}" == "master" ]&&[ "${githubuser}" == "GameServerManager" ]&&[ "${commandname}" != "UPDATE-LGSM" ]; then
  378. remote_fileurl="https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${version}/${github_file_url_dir}/${github_file_url_name}"
  379. remote_fileurl_backup="https://bitbucket.org/${githubuser}/${githubrepo}/raw/${version}/${github_file_url_dir}/${github_file_url_name}"
  380. else
  381. remote_fileurl="https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${githubbranch}/${github_file_url_dir}/${github_file_url_name}"
  382. remote_fileurl_backup="https://bitbucket.org/${githubuser}/${githubrepo}/raw/${githubbranch}/${github_file_url_dir}/${github_file_url_name}"
  383. fi
  384. remote_fileurl_name="GitHub"
  385. remote_fileurl_backup_name="Bitbucket"
  386. local_filedir="${3}"
  387. local_filename="${4}"
  388. chmodx="nochmodx"
  389. run="norun"
  390. forcedl="noforce"
  391. hash="nohash"
  392. # Passes vars to the file download function.
  393. fn_fetch_file "${remote_fileurl}" "${remote_fileurl_backup}" "${remote_fileurl_name}" "${remote_fileurl_backup_name}" "${local_filedir}" "${local_filename}" "${chmodx}" "${run}" "${forcedl}" "${hash}"
  394. }
  395. # Fetches modules from the Git repo during first download.
  396. fn_fetch_function(){
  397. github_file_url_dir="lgsm/functions"
  398. github_file_url_name="${functionfile}"
  399. # If master branch will currently running LinuxGSM version to prevent "version mixing". This is ignored if a fork.
  400. if [ "${githubbranch}" == "master" ]&&[ "${githubuser}" == "GameServerManager" ]&&[ "${commandname}" != "UPDATE-LGSM" ]; then
  401. remote_fileurl="https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${version}/${github_file_url_dir}/${github_file_url_name}"
  402. remote_fileurl_backup="https://bitbucket.org/${githubuser}/${githubrepo}/raw/${version}/${github_file_url_dir}/${github_file_url_name}"
  403. else
  404. remote_fileurl="https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${githubbranch}/${github_file_url_dir}/${github_file_url_name}"
  405. remote_fileurl_backup="https://bitbucket.org/${githubuser}/${githubrepo}/raw/${githubbranch}/${github_file_url_dir}/${github_file_url_name}"
  406. fi
  407. remote_fileurl_name="GitHub"
  408. remote_fileurl_backup_name="Bitbucket"
  409. local_filedir="${functionsdir}"
  410. local_filename="${github_file_url_name}"
  411. chmodx="chmodx"
  412. run="run"
  413. forcedl="noforce"
  414. hash="nohash"
  415. # Passes vars to the file download function.
  416. fn_fetch_file "${remote_fileurl}" "${remote_fileurl_backup}" "${remote_fileurl_name}" "${remote_fileurl_backup_name}" "${local_filedir}" "${local_filename}" "${chmodx}" "${run}" "${forcedl}" "${hash}"
  417. }
  418. # Fetches modules from the Git repo during update-lgsm.
  419. fn_update_function(){
  420. github_file_url_dir="lgsm/functions"
  421. github_file_url_name="${functionfile}"
  422. # If master branch will currently running LinuxGSM version to prevent "version mixing". This is ignored if a fork.
  423. if [ "${githubbranch}" == "master" ]&&[ "${githubuser}" == "GameServerManager" ]&&[ "${commandname}" != "UPDATE-LGSM" ]; then
  424. remote_fileurl="https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${version}/${github_file_url_dir}/${github_file_url_name}"
  425. remote_fileurl_backup="https://bitbucket.org/${githubuser}/${githubrepo}/raw/${version}/${github_file_url_dir}/${github_file_url_name}"
  426. else
  427. remote_fileurl="https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${githubbranch}/${github_file_url_dir}/${github_file_url_name}"
  428. remote_fileurl_backup="https://bitbucket.org/${githubuser}/${githubrepo}/raw/${githubbranch}/${github_file_url_dir}/${github_file_url_name}"
  429. fi
  430. remote_fileurl_name="GitHub"
  431. remote_fileurl_backup_name="Bitbucket"
  432. local_filedir="${functionsdir}"
  433. local_filename="${github_file_url_name}"
  434. chmodx="chmodx"
  435. run="norun"
  436. forcedl="noforce"
  437. hash="nohash"
  438. # Passes vars to the file download function.
  439. fn_fetch_file "${remote_fileurl}" "${remote_fileurl_backup}" "${remote_fileurl_name}" "${remote_fileurl_backup_name}" "${local_filedir}" "${local_filename}" "${chmodx}" "${run}" "${forcedl}" "${hash}"
  440. }
  441. # Function to download latest github release.
  442. # $1 GitHub user / organisation.
  443. # $2 Repo name.
  444. # $3 Destination for download.
  445. # $4 Search string in releases (needed if there are more files that can be downloaded from the release pages).
  446. fn_dl_latest_release_github(){
  447. local githubreleaseuser="${1}"
  448. local githubreleaserepo="${2}"
  449. local githubreleasedownloadpath="${3}"
  450. local githubreleasesearch="${4}"
  451. local githublatestreleaseurl="https://api.github.com/repos/${githubreleaseuser}/${githubreleaserepo}/releases/latest"
  452. # Get last github release.
  453. # If no search for the release filename is set, just get the first file from the latest release.
  454. if [ -z "${githubreleasesearch}" ]; then
  455. githubreleaseassets=$(curl -s "${githublatestreleaseurl}" | jq '[ .assets[] ]')
  456. else
  457. githubreleaseassets=$(curl -s "${githublatestreleaseurl}" | jq "[ .assets[]|select(.browser_download_url | contains(\"${githubreleasesearch}\")) ]")
  458. fi
  459. # Check how many releases we got from the api and exit if we have more then one.
  460. if [ "$(echo -e "${githubreleaseassets}" | jq '. | length')" -gt 1 ]; then
  461. fn_print_fatal_nl "Found more than one release to download - Please report this to the LinuxGSM issue tracker"
  462. fn_script_log_fatal "Found more than one release to download - Please report this to the LinuxGSM issue tracker"
  463. else
  464. # Set variables for download via fn_fetch_file.
  465. githubreleasefilename=$(echo -e "${githubreleaseassets}" | jq -r '.[]name')
  466. githubreleasedownloadlink=$(echo -e "${githubreleaseassets}" | jq -r '.[]browser_download_url')
  467. # Error if no version is there.
  468. if [ -z "${githubreleasefilename}" ]; then
  469. fn_print_fail_nl "Cannot get version from GitHub API for ${githubreleaseuser}/${githubreleaserepo}"
  470. fn_script_log_fatal "Cannot get version from GitHub API for ${githubreleaseuser}/${githubreleaserepo}"
  471. else
  472. # Fetch file from the remote location from the existing function to the ${tmpdir} for now.
  473. fn_fetch_file "${githubreleasedownloadlink}" "" "${githubreleasefilename}" "" "${githubreleasedownloadpath}" "${githubreleasefilename}"
  474. fi
  475. fi
  476. }
  477. # Check that curl is installed
  478. if [ ! "$(command -v curl 2>/dev/null)" ]; then
  479. echo -e "[ FAIL ] Curl is not installed"
  480. exit 1
  481. fi