core_getopt.sh 9.2 KB

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