core_getopt.sh 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. #!/bin/bash
  2. # LinuxGSM core_getopt.sh function
  3. # Author: Daniel Gibbs
  4. # Website: https://linuxgsm.com
  5. # Description: getopt arguments.
  6. local function_selfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")"
  7. ### Define all commands here ###
  8. ## User commands | Trigger commands | Description
  9. # Standard commands
  10. cmd_install=( "i;install" "command_install.sh" "Install the server." )
  11. cmd_auto_install=( "ai;auto-install" "fn_autoinstall" "Install the server without prompts." )
  12. cmd_start=( "st;start" "command_start.sh" "Start the server." )
  13. cmd_stop=( "sp;stop" "command_stop.sh" "Stop the server." )
  14. cmd_restart=( "r;restart" "command_restart.sh" "Restart the server." )
  15. cmd_details=( "dt;details" "command_details.sh" "Display server information." )
  16. cmd_postdetails=( "pd;postdetails" "command_postdetails.sh" "Post details to hastebin (removing passwords)." )
  17. cmd_backup=( "b;backup" "command_backup.sh" "Create backup archives of the server." )
  18. cmd_update_linuxgsm=( "ul;update-lgsm;uf;update-functions" "command_update_linuxgsm.sh" "Check and apply any LinuxGSM updates." )
  19. cmd_test_alert=( "ta;test-alert" "command_test_alert.sh" "Send a test alert." )
  20. cmd_monitor=( "m;monitor" "command_monitor.sh" "Check server status and restart if crashed." )
  21. cmd_donate=( "do;donate" "command_donate.sh" "Donation options." )
  22. # Console servers only
  23. cmd_console=( "c;console" "command_console.sh" "Access server console." )
  24. cmd_debug=( "d;debug" "command_debug.sh" "Start server directly in your terminal." )
  25. # Update servers only
  26. cmd_update=( "u;update" "command_update.sh" "Check and apply any server updates." )
  27. cmd_force_update=( "fu;force-update;update-restart;ur" "forceupdate=1; command_update.sh" "Apply server updates bypassing check." )
  28. # SteamCMD servers only
  29. cmd_validate=( "v;validate" "command_validate.sh" "Validate server files with SteamCMD." )
  30. # Server with mods-install
  31. cmd_mods_install=( "mi;mods-install" "command_mods_install.sh" "View and install available mods/addons." )
  32. cmd_mods_remove=( "mr;mods-remove" "command_mods_remove.sh" "View and remove an installed mod/addon." )
  33. cmd_mods_update=( "mu;mods-update" "command_mods_update.sh" "Update installed mods/addons." )
  34. # Server specific
  35. cmd_change_password=( "pw;change-password" "command_ts3_server_pass.sh" "Change TS3 serveradmin password." )
  36. cmd_install_default_resources=( "ir;install-default-resources" "command_install_resources_mta.sh" "Install the MTA default resources." )
  37. cmd_wipe=( "wi;wipe" "command_wipe.sh" "Wipe your main game server data." )
  38. cmd_wipeall=( "wa;wipeall" "wipeall=1; command_wipe.sh" "Wipe your game server data and blueprints." )
  39. cmd_map_compressor_u99=( "mc;map-compressor" "compress_ut99_maps.sh" "Compresses all ${gamename} server maps." )
  40. cmd_map_compressor_u2=( "mc;map-compressor" "compress_unreal2_maps.sh" "Compresses all ${gamename} server maps." )
  41. cmd_install_cdkey=( "cd;server-cd-key" "install_ut2k4_key.sh" "Add your server cd key." )
  42. cmd_install_dst_token=( "ct;cluster-token" "install_dst_token.sh" "Configure cluster token." )
  43. cmd_install_squad_license=( "li;license" "install_squad_license.sh" "Add your Squad server license." )
  44. cmd_fastdl=( "fd;fastdl" "command_fastdl.sh" "Build a FastDL directory." )
  45. # Dev commands
  46. cmd_dev_debug=( "dev;developer" "command_dev_debug.sh" "Enable developer Mode." )
  47. cmd_dev_detect_deps=( "dd;detect-deps" "command_dev_detect_deps.sh" "Detect required dependencies." )
  48. cmd_dev_detect_glibc=( "dg;detect-glibc" "command_dev_detect_glibc.sh" "Detect required glibc." )
  49. cmd_dev_detect_ldd=( "dl;detect-ldd" "command_dev_detect_ldd.sh" "Detect required dynamic dependencies." )
  50. cmd_dev_query_raw=( "qr;query-raw" "command_dev_query_raw.sh" "The raw output of gamedig and gsquery." )
  51. cmd_dev_clear_functions=( "cf;clear-functions" "command_dev_clear_functions.sh" "Delete the contents of the functions dir." )
  52. ### Set specific opt here ###
  53. currentopt=( "${cmd_start[@]}" "${cmd_stop[@]}" "${cmd_restart[@]}" "${cmd_monitor[@]}" "${cmd_test_alert[@]}" "${cmd_details[@]}" "${cmd_postdetails[@]}" )
  54. # Update LGSM
  55. currentopt+=( "${cmd_update_linuxgsm[@]}" )
  56. # Exclude noupdate games here
  57. if [ "${engine}" != "quake" ]&&[ "${engine}" != "idtech2" ]&&[ "${engine}" != "idtech3" ]&&[ "${engine}" != "iw2.0" ]&&[ "${engine}" != "iw3.0" ]&&[ "${shortname}" != "bf1942" ]&&[ "${shortname}" != "samp" ]; then
  58. currentopt+=( "${cmd_update[@]}" )
  59. # force update for SteamCMD only or MTA
  60. if [ -n "${appid}" ]||[ "${shortname}" == "mta" ]; then
  61. currentopt+=( "${cmd_force_update[@]}" )
  62. fi
  63. fi
  64. # Validate command
  65. if [ -n "${appid}" ]; then
  66. currentopt+=( "${cmd_validate[@]}" )
  67. fi
  68. #Backup
  69. currentopt+=( "${cmd_backup[@]}" )
  70. # Exclude games without a console
  71. if [ "${shortname}" != "ts3" ]; then
  72. currentopt+=( "${cmd_console[@]}" "${cmd_debug[@]}" )
  73. fi
  74. ## Game server exclusive commands
  75. # FastDL command
  76. if [ "${engine}" == "source" ]; then
  77. currentopt+=( "${cmd_fastdl[@]}" )
  78. fi
  79. # TeamSpeak exclusive
  80. if [ "${shortname}" != "ts3" ]; then
  81. currentopt+=( "${cmd_change_password[@]}" )
  82. fi
  83. # Unreal exclusive
  84. if [ "${shortname}" == "rust" ]; then
  85. currentopt+=( "${cmd_wipe[@]}" "${cmd_wipeall[@]}" )
  86. fi
  87. if [ "${engine}" == "unreal2" ]; then
  88. if [ "${shortname}" == "ut2k4" ]; then
  89. currentopt+=( "${cmd_install_cdkey[@]}" "${cmd_map_compressor_u2[@]}" )
  90. else
  91. currentopt+=( "${cmd_map_compressor_u2[@]}" )
  92. fi
  93. fi
  94. if [ "${engine}" == "unreal" ]; then
  95. currentopt+=( "${cmd_map_compressor_u99[@]}" )
  96. fi
  97. # DST exclusive
  98. if [ "${shortname}" == "dst" ]; then
  99. currentopt+=( "${cmd_install_dst_token[@]}" )
  100. fi
  101. # MTA exclusive
  102. if [ "${shortname}" == "mta" ]; then
  103. currentopt+=( "${cmd_install_default_resources[@]}" )
  104. fi
  105. # Squad license exclusive
  106. if [ "${shortname}" == "squad" ]; then
  107. currentopt+=( "${cmd_install_squad_license[@]}" )
  108. fi
  109. ## Mods commands
  110. if [ "${engine}" == "source" ]||[ "${shortname}" == "rust" ]||[ "${shortname}" == "hq" ]||[ "${shortname}" == "sdtd" ]; then
  111. currentopt+=( "${cmd_mods_install[@]}" "${cmd_mods_remove[@]}" "${cmd_mods_update[@]}" )
  112. fi
  113. ## Installer
  114. currentopt+=( "${cmd_install[@]}" "${cmd_auto_install[@]}" )
  115. ## Developer commands
  116. currentopt+=( "${cmd_dev_debug[@]}" )
  117. if [ -f ".dev-debug" ]; then
  118. currentopt+=( "${cmd_dev_detect_deps[@]}" "${cmd_dev_detect_glibc[@]}" "${cmd_dev_detect_ldd[@]}" "${cmd_dev_query_raw[@]}" "${cmd_dev_clear_functions[@]}" )
  119. fi
  120. ## Donate
  121. currentopt+=( "${cmd_donate[@]}" )
  122. ### Build list of available commands
  123. optcommands=()
  124. index="0"
  125. for ((index="0"; index < ${#currentopt[@]}; index+=3)); do
  126. cmdamount="$(echo "${currentopt[index]}" | awk -F ';' '{ print NF }')"
  127. for ((cmdindex=1; cmdindex <= ${cmdamount}; cmdindex++)); do
  128. optcommands+=( "$(echo "${currentopt[index]}" | awk -F ';' -v x=${cmdindex} '{ print $x }')" )
  129. done
  130. done
  131. # Shows LinuxGSM usage
  132. fn_opt_usage(){
  133. echo "Usage: $0 [option]"
  134. echo -e ""
  135. echo "LinuxGSM - ${gamename} - Version ${version}"
  136. echo "https://linuxgsm.com/${gameservername}"
  137. echo -e ""
  138. echo -e "${lightyellow}Commands${default}"
  139. # Display available commands
  140. index="0"
  141. {
  142. for ((index="0"; index < ${#currentopt[@]}; index+=3)); do
  143. # Hide developer commands
  144. if [ "${currentopt[index+2]}" != "DEVCOMMAND" ]; then
  145. echo -e "${cyan}$(echo "${currentopt[index]}" | awk -F ';' '{ print $2 }')\t${default}$(echo "${currentopt[index]}" | awk -F ';' '{ print $1 }')\t| ${currentopt[index+2]}"
  146. fi
  147. done
  148. } | column -s $'\t' -t
  149. core_exit.sh
  150. }
  151. ### Check if user commands exist and run corresponding scripts, or display script usage
  152. if [ -z "${getopt}" ]; then
  153. fn_opt_usage
  154. fi
  155. # Command exists
  156. for i in "${optcommands[@]}"; do
  157. if [ "${i}" == "${getopt}" ] ; then
  158. # Seek and run command
  159. index="0"
  160. for ((index="0"; index < ${#currentopt[@]}; index+=3)); do
  161. currcmdamount="$(echo "${currentopt[index]}" | awk -F ';' '{ print NF }')"
  162. for ((currcmdindex=1; currcmdindex <= ${currcmdamount}; currcmdindex++)); do
  163. if [ "$(echo "${currentopt[index]}" | awk -F ';' -v x=${currcmdindex} '{ print $x }')" == "${getopt}" ]; then
  164. # Run command
  165. eval "${currentopt[index+1]}"
  166. core_exit.sh
  167. break
  168. fi
  169. done
  170. done
  171. fi
  172. done
  173. # If we're executing this, it means command was not found
  174. echo -e "${red}Unknown command${default}: $0 ${getopt}"
  175. exitcode=2
  176. fn_opt_usage
  177. core_exit.sh