core_getopt.sh 8.8 KB

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