core_getopt.sh 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. # Console servers only.
  25. cmd_console=( "c;console" "command_console.sh" "Access server console." )
  26. cmd_debug=( "d;debug" "command_debug.sh" "Start server directly in your terminal." )
  27. # Update servers only.
  28. cmd_update=( "u;update" "command_update.sh" "Check and apply any server updates." )
  29. cmd_check_update=( "cu;check-update" "command_check_update.sh" "Check if a gameserver update is available" )
  30. cmd_force_update=( "fu;force-update;update-restart;ur" "forceupdate=1; command_update.sh" "Apply server updates bypassing check." )
  31. # SteamCMD servers only.
  32. cmd_validate=( "v;validate" "command_validate.sh" "Validate server files with SteamCMD." )
  33. # Server with mods-install.
  34. cmd_mods_install=( "mi;mods-install" "command_mods_install.sh" "View and install available mods/addons." )
  35. cmd_mods_remove=( "mr;mods-remove" "command_mods_remove.sh" "View and remove an installed mod/addon." )
  36. cmd_mods_update=( "mu;mods-update" "command_mods_update.sh" "Update installed mods/addons." )
  37. # Server specific.
  38. cmd_change_password=( "pw;change-password" "command_ts3_server_pass.sh" "Change TS3 serveradmin password." )
  39. cmd_install_default_resources=( "ir;install-default-resources" "command_install_resources_mta.sh" "Install the MTA default resources." )
  40. cmd_wipe=( "w;wipe;wi" "command_wipe.sh" "Map assets are wiped and blueprints are kept." )
  41. cmd_full_wipe=( "fw;full-wipe;wa;wipeall" "fullwipe=1; command_wipe.sh" "Map assets and blueprints are wiped." )
  42. cmd_map_compressor_u99=( "mc;map-compressor" "compress_ut99_maps.sh" "Compresses all ${gamename} server maps." )
  43. cmd_map_compressor_u2=( "mc;map-compressor" "compress_unreal2_maps.sh" "Compresses all ${gamename} server maps." )
  44. cmd_install_cdkey=( "cd;server-cd-key" "install_ut2k4_key.sh" "Add your server cd key." )
  45. cmd_install_dst_token=( "ct;cluster-token" "install_dst_token.sh" "Configure cluster token." )
  46. cmd_install_squad_license=( "li;license" "install_squad_license.sh" "Add your Squad server license." )
  47. cmd_fastdl=( "fd;fastdl" "command_fastdl.sh" "Build a FastDL directory." )
  48. # Dev commands.
  49. cmd_dev_debug=( "dev;developer" "command_dev_debug.sh" "Enable developer Mode." )
  50. cmd_dev_detect_deps=( "dd;detect-deps" "command_dev_detect_deps.sh" "Detect required dependencies." )
  51. cmd_dev_detect_glibc=( "dg;detect-glibc" "command_dev_detect_glibc.sh" "Detect required glibc." )
  52. cmd_dev_detect_ldd=( "dl;detect-ldd" "command_dev_detect_ldd.sh" "Detect required dynamic dependencies." )
  53. cmd_dev_query_raw=( "qr;query-raw" "command_dev_query_raw.sh" "The raw output of gamedig and gsquery." )
  54. cmd_dev_clear_functions=( "cf;clear-functions" "command_dev_clear_functions.sh" "Delete the contents of the functions dir." )
  55. ### Set specific opt here.
  56. currentopt=( "${cmd_start[@]}" "${cmd_stop[@]}" "${cmd_restart[@]}" "${cmd_monitor[@]}" "${cmd_test_alert[@]}" "${cmd_details[@]}" "${cmd_postdetails[@]}" "${cmd_skeleton[@]}" )
  57. # Update LinuxGSM.
  58. currentopt+=( "${cmd_update_linuxgsm[@]}" )
  59. # Exclude noupdate games here.
  60. if [ "${shortname}" == "jk2" ]||[ "${engine}" != "idtech3" ];then
  61. if [ "${shortname}" != "bf1942" ]&&[ "${shortname}" != "bfv" ]&&[ "${engine}" != "idtech2" ]&&[ "${engine}" != "iw2.0" ]&&[ "${engine}" != "iw3.0" ]&&[ "${engine}" != "quake" ]&&[ "${shortname}" != "samp" ]&&[ "${shortname}" != "ut2k4" ]&&[ "${shortname}" != "ut99" ]; then
  62. currentopt+=( "${cmd_update[@]}" )
  63. # force update for SteamCMD or Multi Theft Auto only.
  64. if [ "${appid}" ]||[ "${shortname}" == "mta" ]; then
  65. currentopt+=( "${cmd_force_update[@]}" )
  66. fi
  67. fi
  68. fi
  69. # Validate and check-update command.
  70. if [ "${appid}" ]; then
  71. currentopt+=( "${cmd_validate[@]}" "${cmd_check_update[@]}" )
  72. fi
  73. # Backup.
  74. currentopt+=( "${cmd_backup[@]}" )
  75. # Console & Debug
  76. currentopt+=( "${cmd_console[@]}" "${cmd_debug[@]}" )
  77. ## Game server exclusive commands.
  78. # FastDL command.
  79. if [ "${engine}" == "source" ]; then
  80. currentopt+=( "${cmd_fastdl[@]}" )
  81. fi
  82. # TeamSpeak exclusive.
  83. if [ "${shortname}" == "ts3" ]; then
  84. currentopt+=( "${cmd_change_password[@]}" )
  85. fi
  86. # Unreal exclusive.
  87. if [ "${shortname}" == "rust" ]; then
  88. currentopt+=( "${cmd_wipe[@]}" "${cmd_full_wipe[@]}" )
  89. fi
  90. if [ "${engine}" == "unreal2" ]; then
  91. if [ "${shortname}" == "ut2k4" ]; then
  92. currentopt+=( "${cmd_install_cdkey[@]}" "${cmd_map_compressor_u2[@]}" )
  93. else
  94. currentopt+=( "${cmd_map_compressor_u2[@]}" )
  95. fi
  96. fi
  97. if [ "${engine}" == "unreal" ]; then
  98. currentopt+=( "${cmd_map_compressor_u99[@]}" )
  99. fi
  100. # DST exclusive.
  101. if [ "${shortname}" == "dst" ]; then
  102. currentopt+=( "${cmd_install_dst_token[@]}" )
  103. fi
  104. # MTA exclusive.
  105. if [ "${shortname}" == "mta" ]; then
  106. currentopt+=( "${cmd_install_default_resources[@]}" )
  107. fi
  108. # Squad license exclusive.
  109. if [ "${shortname}" == "squad" ]; then
  110. currentopt+=( "${cmd_install_squad_license[@]}" )
  111. fi
  112. ## Mods commands.
  113. if [ "${engine}" == "source" ]||[ "${shortname}" == "rust" ]||[ "${shortname}" == "hq" ]||[ "${shortname}" == "sdtd" ]||[ "${shortname}" == "cs" ]||[ "${shortname}" == "dod" ]||[ "${shortname}" == "tfc" ]||[ "${shortname}" == "ns" ]||[ "${shortname}" == "ts" ]||[ "${shortname}" == "hldm" ]; then
  114. currentopt+=( "${cmd_mods_install[@]}" "${cmd_mods_remove[@]}" "${cmd_mods_update[@]}" )
  115. fi
  116. ## Installer.
  117. currentopt+=( "${cmd_install[@]}" "${cmd_auto_install[@]}" )
  118. ## Developer commands.
  119. currentopt+=( "${cmd_dev_debug[@]}" )
  120. if [ -f ".dev-debug" ]; then
  121. currentopt+=( "${cmd_dev_detect_deps[@]}" "${cmd_dev_detect_glibc[@]}" "${cmd_dev_detect_ldd[@]}" "${cmd_dev_query_raw[@]}" "${cmd_dev_clear_functions[@]}" )
  122. fi
  123. ## Donate.
  124. currentopt+=( "${cmd_donate[@]}" )
  125. ### Build list of available commands.
  126. optcommands=()
  127. index="0"
  128. for ((index="0"; index < ${#currentopt[@]}; index+=3)); do
  129. cmdamount=$(echo -e "${currentopt[index]}" | awk -F ';' '{ print NF }')
  130. for ((cmdindex=1; cmdindex <= cmdamount; cmdindex++)); do
  131. optcommands+=( "$(echo -e "${currentopt[index]}" | awk -F ';' -v x=${cmdindex} '{ print $x }')" )
  132. done
  133. done
  134. # Shows LinuxGSM usage.
  135. fn_opt_usage(){
  136. echo -e "Usage: $0 [option]"
  137. echo -e ""
  138. echo -e "LinuxGSM - ${gamename} - Version ${version}"
  139. echo -e "https://linuxgsm.com/${gameservername}"
  140. echo -e ""
  141. echo -e "${lightyellow}Commands${default}"
  142. # Display available commands.
  143. index="0"
  144. {
  145. for ((index="0"; index < ${#currentopt[@]}; index+=3)); do
  146. # Hide developer commands.
  147. if [ "${currentopt[index+2]}" != "DEVCOMMAND" ]; then
  148. 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]}"
  149. fi
  150. done
  151. } | column -s $'\t' -t
  152. fn_script_log_pass "Display commands"
  153. core_exit.sh
  154. }
  155. # Check if command existw and run corresponding scripts, or display script usage.
  156. if [ -z "${getopt}" ]; then
  157. fn_opt_usage
  158. fi
  159. # If command exists.
  160. for i in "${optcommands[@]}"; do
  161. if [ "${i}" == "${getopt}" ] ; then
  162. # Seek and run command.
  163. index="0"
  164. for ((index="0"; index < ${#currentopt[@]}; index+=3)); do
  165. currcmdamount=$(echo -e "${currentopt[index]}" | awk -F ';' '{ print NF }')
  166. for ((currcmdindex=1; currcmdindex <= currcmdamount; currcmdindex++)); do
  167. if [ "$(echo -e "${currentopt[index]}" | awk -F ';' -v x=${currcmdindex} '{ print $x }')" == "${getopt}" ]; then
  168. # Run command.
  169. eval "${currentopt[index+1]}"
  170. # Exit should occur in modules. Should this not happen print an error
  171. fn_print_error2_nl "Command did not exit correctly: ${getopt}"
  172. fn_script_log_error "Command did not exit correctly: ${getopt}"
  173. core_exit.sh
  174. fi
  175. done
  176. done
  177. fi
  178. done
  179. # If we're executing this, it means command was not found.
  180. fn_print_error2_nl "Unknown command: $0 ${getopt}"
  181. fn_script_log_error "Unknown command: $0 ${getopt}"
  182. fn_opt_usage
  183. core_exit.sh