### CONSTANTS ### START_PORT_RANGE=30000 BASE_DIR="~/7-Days-To-Die" CONFIG_DIRECTORY="${BASE_DIR}/config" ### CONSTANTS ### echo_rgb() { # Echo a colored string to the terminal based on rgb values # # Positional Arguments: # # message # - The message to be printed to stdout # red # - The red value from 0 to 255 # green # - The green value from 0 to 255 # blue # - The blue value from 0 to 255 # # Usage: # echo_rgb "Yep" 10 8 30 # # POSIX Compliant: # N/A # local red local green local blue local input input="${1}" red="${2}" green="${3}" blue="${4}" printf "\e[0;38;2;%s;%s;%sm%s\e[m\n" "${red}" "${green}" "${blue}" "${input}" } log() { # Print a message and send it to stdout or stderr depending upon log level, also configurable with debug etc. # # Arguments: # level # - The log level, defined within a case check in this function # message # - The info message # line_number # - The line number of the calling function (${LINNO}) # # Usage: # log "info" "Could not find that directory" # # POSIX Compliant: # Yes # # Set debug status depending if a global debug variable has been set to either 1 or 0 local debug if [ ${DEBUG} ]; then debug=${DEBUG} else debug=0 fi local FORMAT FORMAT="[$(echo_rgb "$(date +%Y-%m-%dT%H:%M:%S)" 180 140 255)]" # Convert the level to uppercase local level level=$(echo "${1}" | tr '[:lower:]' '[:upper:]') local message message="${2}" case "${level}" in INFO) # Output all info log levels to stdout printf "${FORMAT}[$(echo_rgb "INFO" 0 140 255)] %s\n" "${message}" >&1 return 0 ;; WARN | WARNING) # Output all info log levels to stdout printf "${FORMAT}[$(echo_rgb "WARNING" 255 255 0)] %s\n" "${message}" >&1 return 0 ;; DEBUG) [[ ${debug} == 0 ]] && return printf "${FORMAT}[$(echo_rgb "DEBUG" 0 160 110)] %s\n" "${message}" >&1 return 0 ;; ERROR) # Output all error log levels to stderr printf "${FORMAT}[$(echo_rgb "ERROR" 255 0 0)] %s\n" "${message}" >&2 return 0 ;; # Further log levels can be added by extending this switch statement with more comparisons *) # Default case, no matches # Returns non-zero code as an improper log option was passed, this helps with using `set -e` printf "${FORMAT}[ERROR] %s\n" "Invalid log level passed, received level \"${level}\" with message \"${message}\"" >&2 return 1 ;; esac } important() { echo_rgb "${1}" 135 195 255 } confirmation() { # Receive confirmation from user as y, Y, n, or N # returns 0 when answer is yes and 1 when answer is no # # Arguments: # message # - The confirmation prompt sent to the user, for example: # Would you like to overwrite foobar.txt (y/N)? # # Usage: # confirmation "Some prompt" # - Sends "Some prompt" to the user and gets their input # # POSIX Compliant: # Yes # local message message="${1}" local choice while true; do read -p "${message} " -n 1 -r choice case "$choice" in y | Y) echo "" return 0 ;; n | N) echo "" return 1 ;; *) echo -e "\nInput must be either y, Y, n, or N" ;; esac done } install_configuration() { local conf_file conf_file="${CONFIG_DIRECTORY}/${1}" if [[ ! -f "${conf_file}" ]]; then log "WARNING" "Unable to find the configuration file ${conf_file}" return 1 fi } install() { local server_id server_id="" while :; do case ${1} in -h | -\? | --help) printf "Usage: %s\n" \ "install [options] --server | -s Installs the server to the given id if it doesn't exist Example: --server 3" exit ;; --) # End of all options. break ;; --server | -s) shift server_id="${1}" [[ -z "${server_id}" ]] && log "error" "No server id passed" && exit 1 ;; -?*) printf 'Unknown option: %s\n' "$1" >&2 ;; *) # Default case: No more options, so break out of the loop. break ;; esac shift done [[ -z "${server_id}" ]] && log "error" "No server id passed" && exit 1 local prefix prefix="7D2D" local server_directory local server_config server_directory="${BASE_DIR}/Server-${server_id}" server_config="${server_directory}/Mordhau/Saved/Config/LinuxServer/Game.ini" [[ -d "${server_directory}" ]] && log "error" "A server already exists at ${server_directory}, delete it and try again" && exit 1 kill_server -s "${server_id}" >/dev/null 2>&1 mkdir -p "${server_directory}" && log "info" "Created server directory ${server_directory}" steamcmd +login anonymous +force_install_dir "${server_directory}" +app_update 629800 validate +quit log "info" "Successfully installed Server-${server_id}" log "info" "Starting server to install default configuration files, please wait..." run_and_stop "${server_id}" cat "${server_config}" >"${server_directory}/Mordhau/Saved/Config/LinuxServer/Game-Primary.ini" && log "info" "Created the Game-Primary.ini file" log "info" "Finished setting up Server-${server_id}" } parse_args() { # Parse input arguments # # Arguments: # Consult the `usage` function # # Usage: # parse_args "$@" # - All arguments should be ingested by parse_args first for variable setting # # POSIX Compliant: # Yes # while :; do case ${1} in -h | -\? | --help) usage # Display a usage synopsis. exit ;; --) # End of all options. break ;; start | s) shift start "$@" break ;; kill | k) shift kill_server "$@" break ;; install | i) shift install "$@" break ;; update | u) shift update "$@" break ;; configure | c) shift configure "$@" break ;; -?*) printf 'Unknown option: %s\n' "$1" >&2 usage exit 1 ;; *) # Default case: No more options, so break out of the loop. break ;; esac shift done } parse_args "$@"