core_dl.sh 24 KB

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