4
0

linuxgsm.sh 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  1. #!/bin/bash
  2. # Project: Linux Game Server Managers - LinuxGSM
  3. # Author: Daniel Gibbs
  4. # License: MIT License, see LICENSE.md
  5. # Purpose: Linux Game Server Management Script
  6. # Contributors: https://linuxgsm.com/contrib
  7. # Documentation: https://docs.linuxgsm.com
  8. # Website: https://linuxgsm.com
  9. # DO NOT EDIT THIS FILE
  10. # LinuxGSM configuration is no longer edited here
  11. # To update your LinuxGSM config go to:
  12. # lgsm/config-lgsm
  13. # https://docs.linuxgsm.com/configuration/linuxgsm-config
  14. # Debugging
  15. if [ -f ".dev-debug" ]; then
  16. if [ -f /.dockerenv ]; then
  17. exec 5> /data/log/dev-debug.log
  18. else
  19. exec 5> dev-debug.log
  20. fi
  21. BASH_XTRACEFD="5"
  22. set -x
  23. fi
  24. version="v26.2.0"
  25. shortname="core"
  26. gameservername="core"
  27. commandname="CORE"
  28. rootdir=$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")
  29. selfname=$(basename "$(readlink -f "${BASH_SOURCE[0]}")")
  30. lgsmdir="${rootdir}/lgsm"
  31. [ -n "${LGSM_LOGDIR}" ] && logdir="${LGSM_LOGDIR}" || logdir="${rootdir}/log"
  32. lgsmlogdir="${logdir}/script"
  33. lgsmlog="${lgsmlogdir}/${selfname}-script.log"
  34. steamcmddir="${HOME}/.steam/steamcmd"
  35. [ -n "${LGSM_SERVERFILES}" ] && serverfiles="${LGSM_SERVERFILES}" || serverfiles="${rootdir}/serverfiles"
  36. modulesdir="${lgsmdir}/modules"
  37. tmpdir="${lgsmdir}/tmp"
  38. [ -n "${LGSM_DATADIR}" ] && datadir="${LGSM_DATADIR}" || datadir="${lgsmdir}/data"
  39. lockdir="${lgsmdir}/lock"
  40. sessionname="${selfname}"
  41. [ -f "${datadir}/${selfname}.uid" ] && socketname="${sessionname}-$(cat "${datadir}/${selfname}.uid")"
  42. serverlist="${datadir}/serverlist.csv"
  43. serverlistmenu="${datadir}/serverlistmenu.csv"
  44. [ -n "${LGSM_CONFIG}" ] && configdir="${LGSM_CONFIG}" || configdir="${lgsmdir}/config-lgsm"
  45. configdirserver="${configdir}/${gameservername}"
  46. configdirdefault="${lgsmdir}/config-default"
  47. userinput="${1}"
  48. userinput2="${2}"
  49. ## GitHub Branch Select
  50. # Allows for the use of different function files
  51. # from a different repo and/or branch.
  52. [ -n "${LGSM_GITHUBUSER}" ] && githubuser="${LGSM_GITHUBUSER}" || githubuser="GameServerManagers"
  53. [ -n "${LGSM_GITHUBREPO}" ] && githubrepo="${LGSM_GITHUBREPO}" || githubrepo="LinuxGSM"
  54. [ -n "${LGSM_GITHUBBRANCH}" ] && githubbranch="${LGSM_GITHUBBRANCH}" || githubbranch="master"
  55. # Check that curl is installed before doing anything
  56. if [ ! "$(command -v curl 2> /dev/null)" ]; then
  57. echo -e "[ FAIL ] Curl is not installed"
  58. exit 1
  59. fi
  60. # Core module that is required first.
  61. core_modules.sh() {
  62. modulefile="${FUNCNAME[0]}"
  63. fn_bootstrap_fetch_file_github "lgsm/modules" "core_modules.sh" "${modulesdir}" "chmodx" "run" "noforcedl" "nomd5"
  64. }
  65. # Bootstrap
  66. # Exits during bootstrap. core_exit.sh is only usable once core_modules.sh (which defines it)
  67. # and core_dl.sh (fn_fetch_module, which it uses) are loaded, so fall back to a plain exit.
  68. fn_bootstrap_exit() {
  69. if type core_exit.sh > /dev/null 2>&1 && type fn_fetch_module > /dev/null 2>&1; then
  70. core_exit.sh
  71. else
  72. exit "${1:-1}"
  73. fi
  74. }
  75. # Validate the configured reference once, without requiring a particular file to exist.
  76. # The contents API also accepts tags and commits, as do the existing raw download URLs.
  77. fn_check_github_branch() {
  78. local branch_check_key="${githubuser}/${githubrepo}/${githubbranch}"
  79. if [ "${github_branch_checked:-}" == "${branch_check_key}" ]; then
  80. return
  81. fi
  82. github_branch_checked="${branch_check_key}"
  83. local branch_http_code
  84. branch_http_code=$(curl --connect-timeout 3 --max-time 10 --silent --location --head --get --data-urlencode "ref=${githubbranch}" --output /dev/null --write-out "%{http_code}" "https://api.github.com/repos/${githubuser}/${githubrepo}/contents/")
  85. # Rate limits, network errors and server failures must leave download fallback intact.
  86. if [ "${branch_http_code}" == "404" ]; then
  87. printf '[ FAIL ] Branch "%s" not found on %s/%s\n' "${githubbranch}" "${githubuser}" "${githubrepo}"
  88. if type fn_script_log_fail > /dev/null 2>&1; then
  89. fn_script_log_fail "Branch \"${githubbranch}\" not found on ${githubuser}/${githubrepo}"
  90. fi
  91. # An exit module could itself require a download from the missing reference.
  92. exit 1
  93. fi
  94. }
  95. # Fetches the core modules required before passed off to core_dl.sh.
  96. fn_bootstrap_fetch_trap() {
  97. echo -e ""
  98. rm -f "${local_filedir:?}/${local_filename}"
  99. # Message and logging functions come from core_messages.sh, which may not be loaded yet.
  100. if type fn_print_canceled_eol_nl > /dev/null 2>&1; then
  101. echo -en "downloading ${local_filename}"
  102. fn_print_canceled_eol_nl
  103. echo -en "downloading ${local_filename}"
  104. fn_print_removed_eol_nl
  105. fn_script_log_info "Downloading ${local_filename}...CANCELED"
  106. fn_script_log_info "Downloading ${local_filename}...REMOVED"
  107. else
  108. echo -e "downloading ${local_filename} ... CANCELED (removed)"
  109. fi
  110. fn_bootstrap_exit 130
  111. }
  112. # Fetches modules from the Git repo during first download.
  113. fn_bootstrap_fetch_module() {
  114. github_file_url_dir="lgsm/modules"
  115. github_file_url_name="${modulefile}"
  116. # If master branch will currently running LinuxGSM version to prevent "version mixing". This is ignored if a fork.
  117. if [ "${githubbranch}" == "master" ] && [ "${githubuser}" == "GameServerManagers" ] && [ "${commandname}" != "UPDATE-LGSM" ]; then
  118. remote_fileurl="https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${version}/${github_file_url_dir}/${github_file_url_name}"
  119. remote_fileurl_backup="https://bitbucket.org/${githubuser}/${githubrepo}/raw/${version}/${github_file_url_dir}/${github_file_url_name}"
  120. else
  121. remote_fileurl="https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${githubbranch}/${github_file_url_dir}/${github_file_url_name}"
  122. remote_fileurl_backup="https://bitbucket.org/${githubuser}/${githubrepo}/raw/${githubbranch}/${github_file_url_dir}/${github_file_url_name}"
  123. fi
  124. remote_fileurl_name="GitHub"
  125. remote_fileurl_backup_name="Bitbucket"
  126. local_filedir="${modulesdir}"
  127. local_filename="${github_file_url_name}"
  128. chmodx="chmodx"
  129. run="run"
  130. forcedl="noforce"
  131. hash="nohash"
  132. # Passes vars to the file download module.
  133. fn_fetch_file "${remote_fileurl}" "${remote_fileurl_backup}" "${remote_fileurl_name}" "${remote_fileurl_backup_name}" "${local_filedir}" "${local_filename}" "${chmodx}" "${run}" "${forcedl}" "${hash}"
  134. }
  135. fn_bootstrap_fetch_file() {
  136. remote_fileurl="${1}"
  137. remote_fileurl_backup="${2}"
  138. remote_fileurl_name="${3}"
  139. remote_fileurl_backup_name="${4}"
  140. local_filedir="${5}"
  141. local_filename="${6}"
  142. chmodx="${7:-0}"
  143. run="${8:-0}"
  144. forcedl="${9:-0}"
  145. hash="${10:-0}"
  146. # Download file if missing or download forced.
  147. if [ ! -f "${local_filedir}/${local_filename}" ] || [ "${forcedl}" == "forcedl" ]; then
  148. fn_check_github_branch
  149. # If backup fileurl exists include it.
  150. if [ -n "${remote_fileurl_backup}" ]; then
  151. # counter set to 0 to allow second try
  152. counter=0
  153. remote_fileurls_array=(remote_fileurl remote_fileurl_backup)
  154. else
  155. # counter set to 1 to not allow second try
  156. counter=1
  157. remote_fileurls_array=(remote_fileurl)
  158. fi
  159. for remote_fileurl_array in "${remote_fileurls_array[@]}"; do
  160. if [ "${remote_fileurl_array}" == "remote_fileurl" ]; then
  161. fileurl="${remote_fileurl}"
  162. fileurl_name="${remote_fileurl_name}"
  163. elif [ "${remote_fileurl_array}" == "remote_fileurl_backup" ]; then
  164. fileurl="${remote_fileurl_backup}"
  165. fileurl_name="${remote_fileurl_backup_name}"
  166. fi
  167. counter=$((counter + 1))
  168. if [ ! -d "${local_filedir}" ]; then
  169. mkdir -p "${local_filedir}"
  170. fi
  171. # Trap will remove part downloaded files if canceled.
  172. trap fn_bootstrap_fetch_trap INT
  173. curlcmd=(curl --connect-timeout 3 --fail -L -o "${local_filedir}/${local_filename}" --retry 2 -A "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.${randomint}.212 Safari/537.36")
  174. # if is large file show progress, else be silent
  175. local exitcode=""
  176. large_files=("bz2" "gz" "zip" "jar" "xz")
  177. if grep -qE "(^|\s)${local_filename##*.}(\s|$)" <<< "${large_files[@]}"; then
  178. echo -e "downloading file [ ${local_filename} ]"
  179. fn_sleep_time
  180. "${curlcmd[@]}" --progress-bar "${fileurl}" 2>&1
  181. exitcode=$?
  182. echo -en "downloading file [ ${local_filename} ]"
  183. else
  184. echo -en "fetching ${fileurl_name} [ ${local_filename} ]\c"
  185. "${curlcmd[@]}" --silent --show-error "${fileurl}" 2>&1
  186. exitcode=$?
  187. fi
  188. # Download will fail if downloads a html file.
  189. if [ -f "${local_filedir}/${local_filename}" ]; then
  190. if head -n 1 "${local_filedir}/${local_filename}" | grep -q "DOCTYPE"; then
  191. rm "${local_filedir:?}/${local_filename:?}"
  192. local exitcode=2
  193. fi
  194. fi
  195. # On first try will error. On second try will fail.
  196. if [ "${exitcode}" -ne 0 ]; then
  197. if [ ${counter} -ge 2 ]; then
  198. echo -e " ... FAIL"
  199. # Logging functions come from core_messages.sh, which may not be loaded yet during bootstrap.
  200. if [ -f "${lgsmlog}" ] && type fn_script_log_fail > /dev/null 2>&1; then
  201. fn_script_log_fail "Downloading ${local_filename}..."
  202. fn_script_log_fail "${fileurl}"
  203. fi
  204. fn_bootstrap_exit 1
  205. else
  206. echo -e " ... ERROR"
  207. if [ -f "${lgsmlog}" ] && type fn_script_log_error > /dev/null 2>&1; then
  208. fn_script_log_error "Downloading ${local_filename}..."
  209. fn_script_log_error "${fileurl}"
  210. fi
  211. fi
  212. else
  213. echo -en " ... OK"
  214. sleep "0.1"
  215. echo -e "\033\\r"
  216. if [ -f "${lgsmlog}" ] && type fn_script_log_pass > /dev/null 2>&1; then
  217. fn_script_log_pass "Downloading ${local_filename}..."
  218. fi
  219. # Make file executable if chmodx is set.
  220. if [ "${chmodx}" == "chmodx" ]; then
  221. chmod +x "${local_filedir}/${local_filename}"
  222. fi
  223. # Remove trap.
  224. trap - INT
  225. break
  226. fi
  227. done
  228. fi
  229. if [ -f "${local_filedir}/${local_filename}" ]; then
  230. # Execute file if run is set.
  231. if [ "${run}" == "run" ]; then
  232. # shellcheck source=/dev/null
  233. source "${local_filedir}/${local_filename}"
  234. fi
  235. fi
  236. }
  237. fn_bootstrap_fetch_file_github() {
  238. github_file_url_dir="${1}"
  239. github_file_url_name="${2}"
  240. # By default modules will be downloaded from the version release to prevent potential version mixing. Only update-lgsm will allow an update.
  241. if [ "${githubbranch}" == "master" ] && [ "${githubuser}" == "GameServerManagers" ] && [ "${commandname}" != "UPDATE-LGSM" ]; then
  242. remote_fileurl="https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${version}/${github_file_url_dir}/${github_file_url_name}"
  243. remote_fileurl_backup="https://bitbucket.org/${githubuser}/${githubrepo}/raw/${version}/${github_file_url_dir}/${github_file_url_name}"
  244. else
  245. remote_fileurl="https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${githubbranch}/${github_file_url_dir}/${github_file_url_name}"
  246. remote_fileurl_backup="https://bitbucket.org/${githubuser}/${githubrepo}/raw/${githubbranch}/${github_file_url_dir}/${github_file_url_name}"
  247. fi
  248. remote_fileurl_name="GitHub"
  249. remote_fileurl_backup_name="Bitbucket"
  250. local_filedir="${3}"
  251. local_filename="${github_file_url_name}"
  252. chmodx="${4:-0}"
  253. run="${5:-0}"
  254. forcedl="${6:-0}"
  255. md5="${7:-0}"
  256. # Passes vars to the file download module.
  257. fn_bootstrap_fetch_file "${remote_fileurl}" "${remote_fileurl_backup}" "${remote_fileurl_name}" "${remote_fileurl_backup_name}" "${local_filedir}" "${local_filename}" "${chmodx}" "${run}" "${forcedl}" "${md5}"
  258. }
  259. # Installer menu.
  260. fn_print_center() {
  261. # Fall back to 80 columns when there is no terminal.
  262. columns="${COLUMNS:-$(tput cols 2> /dev/null)}"
  263. columns="${columns:-80}"
  264. line="$*"
  265. printf "%*s\n" $(((${#line} + columns) / 2)) "${line}"
  266. }
  267. fn_print_horizontal() {
  268. # Fall back to 80 columns when there is no terminal; bash 5.3+ rejects an empty printf width.
  269. local columns="${COLUMNS:-$(tput cols 2> /dev/null)}"
  270. printf '%*s\n' "${columns:-80}" '' | tr ' ' "="
  271. }
  272. # Bash menu.
  273. fn_install_menu_bash() {
  274. local resultvar=$1
  275. title=$2
  276. caption=$3
  277. options=$4
  278. fn_print_horizontal
  279. fn_print_center "${title}"
  280. fn_print_center "${caption}"
  281. fn_print_horizontal
  282. menu_options=()
  283. while read -r line || [[ -n "${line}" ]]; do
  284. var=$(echo -e "${line}" | awk -F "," '{print $2 " - " $3}')
  285. menu_options+=("${var}")
  286. done < "${options}"
  287. menu_options+=("Cancel")
  288. select option in "${menu_options[@]}"; do
  289. if [ "${option}" ] && [ "${option}" != "Cancel" ]; then
  290. eval "$resultvar=\"${option/%\ */}\""
  291. fi
  292. break
  293. done
  294. }
  295. # Whiptail/Dialog menu.
  296. fn_install_menu_whiptail() {
  297. local menucmd=$1
  298. local resultvar=$2
  299. title=$3
  300. caption=$4
  301. options=$5
  302. height=${6:-40}
  303. width=${7:-80}
  304. menuheight=${8:-30}
  305. IFS=","
  306. menu_options=()
  307. while read -r line; do
  308. key=$(echo -e "${line}" | awk -F "," '{print $3}')
  309. val=$(echo -e "${line}" | awk -F "," '{print $2}')
  310. menu_options+=("${val//\"/}" "${key//\"/}")
  311. done < "${options}"
  312. OPTION=$(${menucmd} --title "${title}" --menu "${caption}" "${height}" "${width}" "${menuheight}" "${menu_options[@]}" 3>&1 1>&2 2>&3)
  313. exitcode=$?
  314. if [ "${exitcode}" -eq 0 ]; then
  315. eval "$resultvar=\"${OPTION}\""
  316. else
  317. eval "$resultvar="
  318. fi
  319. }
  320. # Menu selector.
  321. fn_install_menu() {
  322. local resultvar=$1
  323. local selection=""
  324. title=$2
  325. caption=$3
  326. options=$4
  327. # Get menu command.
  328. for menucmd in whiptail dialog bash; do
  329. if [ "$(command -v "${menucmd}")" ]; then
  330. menucmd=$(command -v "${menucmd}")
  331. break
  332. fi
  333. done
  334. case "$(basename "${menucmd}")" in
  335. whiptail | dialog)
  336. fn_install_menu_whiptail "${menucmd}" selection "${title}" "${caption}" "${options}" 40 80 30
  337. ;;
  338. *)
  339. fn_install_menu_bash selection "${title}" "${caption}" "${options}"
  340. ;;
  341. esac
  342. eval "$resultvar=\"${selection}\""
  343. }
  344. # Gets server info from serverlist.csv and puts in to array.
  345. fn_server_info() {
  346. IFS=","
  347. server_info_line="$(grep -aw "${userinput}" "${serverlist}" | head -n 1)"
  348. read -r -a server_info_array <<< "${server_info_line}"
  349. shortname="${server_info_array[0]}" # csgo
  350. gameservername="${server_info_array[1]}" # csgoserver
  351. gamename="${server_info_array[2]}" # Counter Strike: Global Offensive
  352. }
  353. fn_install_getopt() {
  354. userinput="empty"
  355. echo -e "Usage: $0 [option]"
  356. echo -e ""
  357. echo -e "Installer - Linux Game Server Managers - Version ${version}"
  358. echo -e "https://linuxgsm.com"
  359. echo -e ""
  360. echo -e "Commands"
  361. echo -e "install\t\t| Select server to install."
  362. echo -e "servername\t| Enter name of game server to install. e.g $0 csgoserver."
  363. echo -e "list\t\t| List all servers available for install."
  364. exit
  365. }
  366. fn_install_file() {
  367. local_filename="${gameservername}"
  368. if [ -e "${local_filename}" ]; then
  369. i=2
  370. while [ -e "${local_filename}-${i}" ]; do
  371. ((i++))
  372. done
  373. local_filename="${local_filename}-${i}"
  374. fi
  375. cp -R "${selfname}" "${local_filename}"
  376. sed -i -e "s/shortname=\"core\"/shortname=\"${shortname}\"/g" "${local_filename}"
  377. sed -i -e "s/gameservername=\"core\"/gameservername=\"${gameservername}\"/g" "${local_filename}"
  378. echo -e "Installed ${gamename} server as ${local_filename}"
  379. echo -e ""
  380. if [ ! -d "${serverfiles}" ]; then
  381. echo -e "./${local_filename} install"
  382. else
  383. echo -e "Remember to check server ports"
  384. echo -e "./${local_filename} details"
  385. fi
  386. echo -e ""
  387. exit
  388. }
  389. # Prevent LinuxGSM from running as root. Except if doing a dependency install.
  390. if [ "$(whoami)" == "root" ]; then
  391. if [ -f "${modulesdir}/core_modules.sh" ] || [ -f "${modulesdir}/check_root.sh" ] || [ -f "${modulesdir}/core_messages.sh" ]; then
  392. if [ "${userinput}" != "install" ] && [ "${userinput}" != "auto-install" ] && [ "${userinput}" != "i" ] && [ "${userinput}" != "ai" ]; then
  393. core_modules.sh
  394. core_messages.sh
  395. fn_ansi_loader
  396. check_root.sh
  397. fi
  398. else
  399. if [ "${userinput}" != "install" ] && [ "${userinput}" != "auto-install" ] && [ "${userinput}" != "i" ] && [ "${userinput}" != "ai" ]; then
  400. echo -e "[ FAIL ] Do NOT run as root!"
  401. exit 1
  402. fi
  403. fi
  404. fi
  405. # LinuxGSM installer mode.
  406. if [ "${shortname}" == "core" ]; then
  407. # Download the latest serverlist. This is the complete list of all supported servers.
  408. fn_bootstrap_fetch_file_github "lgsm/data" "serverlist.csv" "${datadir}" "nochmodx" "norun" "forcedl" "nomd5"
  409. if [ ! -f "${serverlist}" ]; then
  410. echo -e "[ FAIL ] serverlist.csv could not be loaded."
  411. exit 1
  412. fi
  413. if [ "${userinput}" == "list" ] || [ "${userinput}" == "l" ]; then
  414. {
  415. tail -n +2 "${serverlist}" | awk -F "," '{print $2 "\t" $3}'
  416. } | column -s $'\t' -t | more
  417. exit
  418. elif [ "${userinput}" == "install" ] || [ "${userinput}" == "i" ]; then
  419. tail -n +2 "${serverlist}" | awk -F "," '{print $1 "," $2 "," $3}' > "${serverlistmenu}"
  420. fn_install_menu result "LinuxGSM" "Select game server to install." "${serverlistmenu}"
  421. userinput="${result}"
  422. fn_server_info
  423. if [ "${result}" == "${gameservername}" ]; then
  424. fn_install_file
  425. elif [ "${result}" == "" ]; then
  426. echo -e "Install canceled"
  427. else
  428. echo -e "[ FAIL ] menu result does not match gameservername"
  429. echo -e "result: ${result}"
  430. echo -e "gameservername: ${gameservername}"
  431. fi
  432. elif [ "${userinput}" ]; then
  433. fn_server_info
  434. if [ "${userinput}" == "${gameservername}" ] || [ "${userinput}" == "${gamename}" ] || [ "${userinput}" == "${shortname}" ]; then
  435. fn_install_file
  436. else
  437. echo -e "[ FAIL ] Unknown game server"
  438. exit 1
  439. fi
  440. else
  441. fn_install_getopt
  442. fi
  443. # LinuxGSM server mode.
  444. else
  445. core_modules.sh
  446. if [ "${shortname}" != "core-dep" ]; then
  447. # Load LinuxGSM configs.
  448. # These are required to get all the default variables for the specific server.
  449. # Load the default config. If missing download it. If changed reload it.
  450. if [ ! -f "${configdirdefault}/config-lgsm/${gameservername}/_default.cfg" ]; then
  451. mkdir -p "${configdirdefault}/config-lgsm/${gameservername}"
  452. fn_fetch_config "lgsm/config-default/config-lgsm/${gameservername}" "_default.cfg" "${configdirdefault}/config-lgsm/${gameservername}" "_default.cfg" "nochmodx" "norun" "noforcedl" "nomd5"
  453. fi
  454. if [ ! -f "${configdirserver}/_default.cfg" ]; then
  455. mkdir -p "${configdirserver}"
  456. echo -en "copying _default.cfg\c"
  457. cp -R "${configdirdefault}/config-lgsm/${gameservername}/_default.cfg" "${configdirserver}/_default.cfg"
  458. exitcode=$?
  459. if [ "${exitcode}" -ne 0 ]; then
  460. echo -e " ... FAIL"
  461. exit 1
  462. else
  463. echo -e " ... OK"
  464. fi
  465. else
  466. config_file_diff=$(diff -q "${configdirdefault}/config-lgsm/${gameservername}/_default.cfg" "${configdirserver}/_default.cfg")
  467. if [ "${config_file_diff}" != "" ]; then
  468. fn_print_warn_nl "_default.cfg has altered. reloading config."
  469. echo -en "copying _default.cfg...\c"
  470. cp -R "${configdirdefault}/config-lgsm/${gameservername}/_default.cfg" "${configdirserver}/_default.cfg"
  471. exitcode=$?
  472. if [ "${exitcode}" -ne 0 ]; then
  473. echo -e "FAIL"
  474. exit 1
  475. else
  476. echo -e " ... OK"
  477. fi
  478. fi
  479. fi
  480. fi
  481. # Load the IP details before the first config is loaded.
  482. check_ip.sh
  483. # Configs have to be loaded twice to allow start startparameters to pick up all vars
  484. # shellcheck source=/dev/null
  485. source "${configdirserver}/_default.cfg"
  486. # Load the common.cfg config. If missing download it.
  487. if [ ! -f "${configdirserver}/common.cfg" ]; then
  488. fn_fetch_config "lgsm/config-default/config-lgsm" "common-template.cfg" "${configdirserver}" "common.cfg" "${chmodx}" "nochmodx" "norun" "noforcedl" "nomd5"
  489. # shellcheck source=/dev/null
  490. source "${configdirserver}/common.cfg"
  491. else
  492. # shellcheck source=/dev/null
  493. source "${configdirserver}/common.cfg"
  494. fi
  495. # Load the secrets-common.cfg config. If missing download it.
  496. if [ ! -f "${configdirserver}/secrets-common.cfg" ]; then
  497. fn_fetch_config "lgsm/config-default/config-lgsm" "secrets-common-template.cfg" "${configdirserver}" "secrets-common.cfg" "${chmodx}" "nochmodx" "norun" "noforcedl" "nomd5"
  498. # shellcheck source=/dev/null
  499. source "${configdirserver}/secrets-common.cfg"
  500. else
  501. # shellcheck source=/dev/null
  502. source "${configdirserver}/secrets-common.cfg"
  503. fi
  504. # Load the instance.cfg config. If missing download it.
  505. if [ ! -f "${configdirserver}/${selfname}.cfg" ]; then
  506. fn_fetch_config "lgsm/config-default/config-lgsm" "instance-template.cfg" "${configdirserver}" "${selfname}.cfg" "nochmodx" "norun" "noforcedl" "nomd5"
  507. # shellcheck source=/dev/null
  508. source "${configdirserver}/${selfname}.cfg"
  509. else
  510. # shellcheck source=/dev/null
  511. source "${configdirserver}/${selfname}.cfg"
  512. fi
  513. # Load the secrets-instance.cfg config. If missing download it.
  514. if [ ! -f "${configdirserver}/secrets-${selfname}.cfg" ]; then
  515. fn_fetch_config "lgsm/config-default/config-lgsm" "secrets-instance-template.cfg" "${configdirserver}" "secrets-${selfname}.cfg" "nochmodx" "norun" "noforcedl" "nomd5"
  516. # shellcheck source=/dev/null
  517. source "${configdirserver}/secrets-${selfname}.cfg"
  518. else
  519. # shellcheck source=/dev/null
  520. source "${configdirserver}/secrets-${selfname}.cfg"
  521. fi
  522. # Reloads start parameter to ensure all vars in startparameters are set.
  523. # Will reload the last defined startparameter.
  524. fn_reload_startparameters() {
  525. # reload Wurm config.
  526. if [ "${shortname}" == "wurm" ]; then
  527. # shellcheck source=/dev/null
  528. source "${servercfgfullpath}"
  529. fi
  530. # reload startparameters.
  531. if grep -qE "^[[:blank:]]*startparameters=" "${configdirserver}/secrets-${selfname}.cfg"; then
  532. eval startparameters="$(sed -nr 's/^ *startparameters=(.*)$/\1/p' "${configdirserver}/secrets-${selfname}.cfg")"
  533. elif grep -qE "^[[:blank:]]*startparameters=" "${configdirserver}/${selfname}.cfg"; then
  534. eval startparameters="$(sed -nr 's/^ *startparameters=(.*)$/\1/p' "${configdirserver}/${selfname}.cfg")"
  535. elif grep -qE "^[[:blank:]]*startparameters=" "${configdirserver}/secrets-common.cfg"; then
  536. eval startparameters="$(sed -nr 's/^ *startparameters=(.*)$/\1/p' "${configdirserver}/secrets-common.cfg")"
  537. elif grep -qE "^[[:blank:]]*startparameters=" "${configdirserver}/common.cfg"; then
  538. eval startparameters="$(sed -nr 's/^ *startparameters=(.*)$/\1/p' "${configdirserver}/common.cfg")"
  539. elif grep -qE "^[[:blank:]]*startparameters=" "${configdirserver}/_default.cfg"; then
  540. eval startparameters="$(sed -nr 's/^ *startparameters=(.*)$/\1/p' "${configdirserver}/_default.cfg")"
  541. fi
  542. # reload preexecutable.
  543. if grep -qE "^[[:blank:]]*preexecutable=" "${configdirserver}/secrets-${selfname}.cfg"; then
  544. eval preexecutable="$(sed -nr 's/^ *preexecutable=(.*)$/\1/p' "${configdirserver}/secrets-${selfname}.cfg")"
  545. elif grep -qE "^[[:blank:]]*preexecutable=" "${configdirserver}/${selfname}.cfg"; then
  546. eval preexecutable="$(sed -nr 's/^ *preexecutable=(.*)$/\1/p' "${configdirserver}/${selfname}.cfg")"
  547. elif grep -qE "^[[:blank:]]*preexecutable=" "${configdirserver}/secrets-common.cfg"; then
  548. eval preexecutable="$(sed -nr 's/^ *preexecutable=(.*)$/\1/p' "${configdirserver}/secrets-common.cfg")"
  549. elif grep -qE "^[[:blank:]]*preexecutable=" "${configdirserver}/common.cfg"; then
  550. eval preexecutable="$(sed -nr 's/^ *preexecutable=(.*)$/\1/p' "${configdirserver}/common.cfg")"
  551. elif grep -qE "^[[:blank:]]*preexecutable=" "${configdirserver}/_default.cfg"; then
  552. eval preexecutable="$(sed -nr 's/^ *preexecutable=(.*)$/\1/p' "${configdirserver}/_default.cfg")"
  553. fi
  554. }
  555. # Load the linuxgsm.sh in to tmpdir. If missing download it.
  556. if [ ! -f "${tmpdir}/linuxgsm.sh" ]; then
  557. fn_fetch_file_github "" "linuxgsm.sh" "${tmpdir}" "chmodx" "norun" "noforcedl" "nomd5"
  558. fi
  559. # Enables ANSI colours from core_messages.sh. Can be disabled with ansi=off.
  560. fn_ansi_loader
  561. getopt=$1
  562. core_getopt.sh
  563. fi