linuxgsm.sh 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. #!/bin/bash
  2. # Project: Game Server Managers - LinuxGSM
  3. # Author: Daniel Gibbs
  4. # License: MIT License, Copyright (c) 2017 Daniel Gibbs
  5. # Purpose: Counter-Strike: Global Offensive | Server Management Script
  6. # Contributors: https://github.com/GameServerManagers/LinuxGSM/graphs/contributors
  7. # Documentation: https://github.com/GameServerManagers/LinuxGSM/wiki
  8. # Website: https://gameservermanagers.com
  9. # Debugging
  10. if [ -f ".dev-debug" ]; then
  11. exec 5>dev-debug.log
  12. BASH_XTRACEFD="5"
  13. set -x
  14. fi
  15. version="170305"
  16. rootdir="$(dirname $(readlink -f "${BASH_SOURCE[0]}"))"
  17. selfname="$(basename $(readlink -f "${BASH_SOURCE[0]}"))"
  18. servicename="${selfname}"
  19. shortname="core"
  20. servername="core"
  21. gamename="core"
  22. lockselfname=".${servicename}.lock"
  23. steamcmddir="${rootdir}/steamcmd"
  24. lgsmdir="${rootdir}/lgsm"
  25. functionsdir="${lgsmdir}/functions"
  26. libdir="${lgsmdir}/lib"
  27. tmpdir="${lgsmdir}/tmp"
  28. serverfiles="${rootdir}/serverfiles"
  29. configdir="${lgsmdir}/config-lgsm"
  30. configdirserver="${configdir}/${servername}"
  31. configdirdefault="${lgsmdir}/config-default"
  32. ## GitHub Branch Select
  33. # Allows for the use of different function files
  34. # from a different repo and/or branch.
  35. githubuser="GameServerManagers"
  36. githubrepo="LinuxGSM"
  37. githubbranch="feature/config"
  38. # Core Function that is required first
  39. core_functions.sh(){
  40. functionfile="${FUNCNAME}"
  41. fn_bootstrap_fetch_file "lgsm/functions" "core_functions.sh" "${functionsdir}" "_default.cfg" "noexecutecmd" "norun" "noforce" "nomd5"
  42. }
  43. # Bootstrap
  44. # Fetches the core functions required before passed off to core_dl.sh
  45. # Fetches core functions
  46. fn_bootstrap_fetch_file(){
  47. fileurl="${1}"
  48. filedir="${2}"
  49. filename="${3}"
  50. executecmd="${4:-0}"
  51. run="${5:-0}"
  52. force="${6:-0}"
  53. md5="${7:-0}"
  54. # If the file is missing, then download
  55. if [ ! -f "${filedir}/${filename}" ]; then
  56. if [ ! -d "${filedir}" ]; then
  57. mkdir -p "${filedir}"
  58. fi
  59. # Check curl exists and use available path
  60. curlpaths="$(command -v curl 2>/dev/null) $(which curl >/dev/null 2>&1) /usr/bin/curl /bin/curl /usr/sbin/curl /sbin/curl)"
  61. for curlpath in ${curlpaths}
  62. do
  63. if [ -x "${curlpath}" ]; then
  64. break
  65. fi
  66. done
  67. # If curl exists download file
  68. if [ "$(basename ${curlpath})" == "curl" ]; then
  69. # trap to remove part downloaded files
  70. echo -ne " fetching ${filename}...\c"
  71. curlcmd=$(${curlpath} -s --fail -L -o "${filedir}/${filename}" "${fileurl}" 2>&1)
  72. local exitcode=$?
  73. if [ ${exitcode} -ne 0 ]; then
  74. echo -e "\e[0;31mFAIL\e[0m\n"
  75. echo -e "${fileurl}" | tee -a "${scriptlog}"
  76. echo "${curlcmd}" | tee -a "${scriptlog}"
  77. exit 1
  78. else
  79. echo -e "\e[0;32mOK\e[0m"
  80. fi
  81. else
  82. echo "[ FAIL ] Curl is not installed"
  83. exit 1
  84. fi
  85. # make file executecmd if executecmd is set
  86. if [ "${executecmd}" == "executecmd" ]; then
  87. chmod +x "${filedir}/${filename}"
  88. fi
  89. fi
  90. if [ -f "${filedir}/${filename}" ]; then
  91. # run file if run is set
  92. if [ "${run}" == "run" ]; then
  93. source "${filedir}/${filename}"
  94. fi
  95. fi
  96. }
  97. # Installer menu
  98. fn_print_center() {
  99. columns="$(tput cols)"
  100. line="$@"
  101. printf "%*s\n" $(( (${#line} + columns) / 2)) "${line}"
  102. }
  103. fn_print_horizontal(){
  104. char="${1:-=}"
  105. printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' "${char}"
  106. }
  107. # Bash Menu
  108. fn_install_menu_bash() {
  109. local resultvar=$1
  110. title=$2
  111. caption=$3
  112. options=$4
  113. fn_print_horizontal
  114. fn_print_center $title
  115. fn_print_center $caption
  116. fn_print_horizontal
  117. menu_options=()
  118. while read -r line || [[ -n "${line}" ]]; do
  119. var=$(echo "${line}" | awk -F "," '{print $2 " - " $3}')
  120. menu_options+=( "${var}" )
  121. done < $options
  122. menu_options+=( "Cancel" )
  123. select option in "${menu_options[@]}"; do
  124. if [ -n "${option}" ] && [ "${option}" != "Cancel" ]; then
  125. eval "$resultvar=\"${option/%\ */}\""
  126. fi
  127. break
  128. done
  129. }
  130. # Whiptail/Dialog Menu
  131. fn_install_menu_whiptail() {
  132. local menucmd=$1
  133. local resultvar=$2
  134. title=$3
  135. caption=$4
  136. options=$5
  137. height=${6:-40}
  138. width=${7:-80}
  139. menuheight=${8:-30}
  140. IFS=","
  141. menu_options=()
  142. while read -r line; do
  143. key=$(echo "${line}" | awk -F "," '{print $3}')
  144. val=$(echo "${line}" | awk -F "," '{print $2}')
  145. menu_options+=( ${val//\"} "${key//\"}" )
  146. done < $options
  147. OPTION=$(${menucmd} --title "${title}" --menu "${caption}" ${height} ${width} ${menuheight} "${menu_options[@]}" 3>&1 1>&2 2>&3)
  148. if [ $? = 0 ]; then
  149. eval "$resultvar=\"${OPTION}\""
  150. else
  151. eval "$resultvar="
  152. fi
  153. }
  154. # Menu selector
  155. fn_install_menu() {
  156. local resultvar=$1
  157. local selection=""
  158. title=$2
  159. caption=$3
  160. options=$4
  161. # Get menu command
  162. for menucmd in whiptail dialog bash; do
  163. if [ -x $(which ${menucmd}) ]; then
  164. menucmd=$(which ${menucmd})
  165. break
  166. fi
  167. done
  168. case "$(basename ${menucmd})" in
  169. whiptail|dialog)
  170. fn_install_menu_whiptail "${menucmd}" selection "${title}" "${caption}" "${options}" 40 80 30;;
  171. *)
  172. fn_install_menu_bash selection "${title}" "${caption}" "${options}";;
  173. esac
  174. eval "$resultvar=\"${selection}\""
  175. }
  176. # Gets server info from serverlist.csv and puts in to array
  177. fn_server_info(){
  178. IFS=","
  179. server_info_array=($(grep -a "${userinput}" "${serverlist}"))
  180. shortname="${server_info_array[0]}" # csgo
  181. servername="${server_info_array[1]}" # csgoserver
  182. gamename="${server_info_array[2]}" # Counter Strike: Global Offensive
  183. }
  184. fn_install_getopt(){
  185. userinput="empty"
  186. echo "Usage: $0 [option]"
  187. echo -e ""
  188. echo "Installer - Linux Game Server Managers - Version ${version}"
  189. echo "https://gameservermanagers.com"
  190. echo -e ""
  191. echo -e "Commands"
  192. echo -e "install |Select server to install."
  193. echo -e "servername |e.g $0 csgoserver. Enter the required servername will install it."
  194. echo -e "list |List all servers available for install."
  195. exit
  196. }
  197. fn_install_file(){
  198. filename="${servername}"
  199. if [ -e "${filename}" ]; then
  200. i=2
  201. while [ -e "${filename}-${i}" ] ; do
  202. let i++
  203. done
  204. filename="${filename}-${i}"
  205. fi
  206. cp -R "${selfname}" "${filename}"
  207. sed -i -e "s/shortname=\"core\"/shortname=\"${shortname}\"/g" "${filename}"
  208. sed -i -e "s/servername=\"core\"/servername=\"${servername}\"/g" "${filename}"
  209. sed -i -e "s/gamename=\"core\"/gamename=\"${gamename}\"/g" "${filename}"
  210. echo "Installed ${gamename} server as ${filename}"
  211. echo "./${filename} install"
  212. exit
  213. }
  214. # Prevent from running this script as root.
  215. if [ "$(whoami)" = "root" ]; then
  216. if [ ! -f "${functionsdir}/core_functions.sh" ]||[ ! -f "${functionsdir}/check_root.sh" ]||[ ! -f "${functionsdir}/core_messages.sh" ]; then
  217. echo "[ FAIL ] Do NOT run this script as root!"
  218. exit 1
  219. else
  220. core_functions.sh
  221. check_root.sh
  222. fi
  223. fi
  224. # LinuxGSM installer mode
  225. if [ "${shortname}" == "core" ]; then
  226. userinput=$1
  227. datadir="${lgsmdir}/data"
  228. serverlist="${datadir}/serverlist.csv"
  229. serverlist_tmp="${tmpdir}/data/serverlist.csv"
  230. # Download the serverlist. This is the complete list of all supported servers.
  231. # Download to tmp dir
  232. fn_bootstrap_fetch_file "lgsm/data" "serverlist.csv" "${tmpdir}/data" "serverlist.csv" "noexecutecmd" "norun" "noforce" "nomd5"
  233. # if missing in lgsm dir copy it accross
  234. if [ ! -f "${serverlist}" ]; then
  235. mkdir -p "${datadir}"
  236. cp -R "${serverlist_tmp}" "${serverlist}"
  237. # check if the files are different.
  238. else
  239. file_diff=$(diff -q "${serverlist_tmp}" "${serverlist}")
  240. if [ "${file_diff}" != "" ]; then
  241. cp -Rf "${serverlist_tmp}" "${serverlist}"
  242. fi
  243. fi
  244. if [ ! -f "${serverlist}" ];then
  245. echo "[ FAIL ] serverlist.csv could not be loaded."
  246. exit 1
  247. fi
  248. if [ "${userinput}" == "list" ]; then
  249. {
  250. awk -F "," '{print $2 "\t" $3}' "${serverlist}"
  251. } | column -s $'\t' -t | more
  252. exit
  253. elif [ "${userinput}" == "install" ]; then
  254. fn_install_menu result "LinuxGSM" "Select game to install" "lgsm/data/serverlist.csv"
  255. userinput="${result}"
  256. fn_server_info
  257. if [ "${result}" == "${servername}" ]; then
  258. fn_install_file
  259. else
  260. echo "[ FAIL ] menu result does not match servername"
  261. fi
  262. elif [ -n "${userinput}" ]; then
  263. fn_server_info
  264. if [ "${userinput}" == "${servername}" ]; then
  265. fn_install_file
  266. fi
  267. else
  268. fn_install_getopt
  269. fi
  270. # LinuxGSM Server Mode
  271. else
  272. core_functions.sh
  273. # Load LinuxGSM configs
  274. # These are required to get all the default variables for the specific server.
  275. # Load the default config. If missing download it. If changed reload it.
  276. if [ ! -f "${configdirdefault}/config-lgsm/${servername}/_default.cfg" ];then
  277. mkdir -p "${configdirdefault}/config-lgsm/${servername}"
  278. fn_fetch_config "lgsm/config-default/config-lgsm/${servername}" "_default.cfg" "${configdirdefault}/config-lgsm/${servername}" "_default.cfg" "noexecutecmd" "norun" "noforce" "nomd5"
  279. fi
  280. if [ ! -f "${configdirserver}/_default.cfg" ];then
  281. mkdir -p "${configdirserver}"
  282. cp -R "${configdirdefault}/config-lgsm/${servername}/_default.cfg" "${configdirserver}/_default.cfg"
  283. else
  284. function_file_diff=$(diff -q ${configdirdefault}/config-lgsm/${servername}/_default.cfg ${configdirserver}/_default.cfg)
  285. if [ "${function_file_diff}" != "" ]; then
  286. echo "_default.cfg has been altered. Reloading config."
  287. cp -R "${configdirdefault}/config-lgsm/${servername}/_default.cfg" "${configdirserver}/_default.cfg"
  288. fi
  289. fi
  290. source "${configdirserver}/_default.cfg"
  291. # Load the common.cfg config. If missing download it
  292. if [ ! -f "${configdirserver}/common.cfg" ];then
  293. fn_fetch_config "lgsm/config-default/config-lgsm" "common-template.cfg" "${configdirserver}" "common.cfg" "${executecmd}" "noexecutecmd" "norun" "noforce" "nomd5"
  294. source "${configdirserver}/common.cfg"
  295. else
  296. source "${configdirserver}/common.cfg"
  297. fi
  298. # Load the instance.cfg config. If missing download it
  299. if [ ! -f "${configdirserver}/${servicename}.cfg" ];then
  300. fn_fetch_config "lgsm/config-default/config-lgsm" "instance-template.cfg" "${configdirserver}" "${servicename}.cfg" "noexecutecmd" "norun" "noforce" "nomd5"
  301. source "${configdirserver}/${servicename}.cfg"
  302. else
  303. source "${configdirserver}/${servicename}.cfg"
  304. fi
  305. fi
  306. getopt=$1
  307. core_getopt.sh