Bash_Scripts/CentOS/Squad/Squad-Install-Mod.bash
2021-08-10 04:18:08 -05:00

265 lines
6.7 KiB
Bash
Executable File

#!/bin/bash
echo_rgb() {
# Echo a colored string to the terminal based on rgb values
#
# Positional Arguments:
#
# message <type: string> <position: 1> <required: true>
# - The message to be printed to stdout
# red <type: int> <position: 2> <required: true>
# - The red value from 0 to 255
# green <type: int> <position: 3> <required: true>
# - The green value from 0 to 255
# blue <type: int> <position: 4> <required: true>
# - 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 <type: string> <position: 1> <required: true>
# - The log level, defined within a case check in this function
# message <type: string> <position: 2> <required: true>
# - The info message
# line_number <type: int> <position: 3> <required: false>
# - 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
}
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 <type: string> <position: 1> <required: true>
# - 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
}
usage() {
# Print out usage instructions for the local script
#
# Arguments:
# None
#
# Usage:
# usage
#
# POSIX Compliant:
# Yes
#
printf "Usage: %s\n" \
"$(important "$(basename "${0}") -s <int: server id> -w <int: workshop id>")
-s <int: server id> | --server <int: server id>
Which Squad server to start, see the ~/Squad directory -- each number corresponds to an ID
Example:
--server 0
-w <int: workshop id> | --workshop-id <int: workshop id>
Installs the given workshop ID
Example:
--verify"
}
important() {
echo_rgb "${1}" 145 233 255
}
server_id=""
workshop_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
;;
-s | --server)
shift
server_id="${1}"
[[ -z "${server_id}" ]] &&
log "error" "Server requires an argument, e.g. --server 0" &&
exit 1
[[ ! "${server_id}" =~ [0-9] ]] &&
log "error" "Server ID must be a number, received: ${server_id}" &&
exit 1
;;
-w | --workshop-id)
shift
workshop_id="${1}"
[[ -z "${workshop_id}" ]] &&
log "error" "Workshop ID requires an argument, e.g. --server 0" &&
exit 1
[[ ! "${workshop_id}" =~ [0-9] ]] &&
log "error" "Workshop ID must be a number, received: ${workshop_id}" &&
exit 1
;;
-?*)
printf 'Unknown option: %s\n' "$1" >&2
usage
;;
*) # Default case: No more options, so break out of the loop.
break ;;
esac
shift
done
}
parse_args "$@"
[[ -z "${server_id}" ]] &&
log "error" "A Server ID must be passed via the -s flag" &&
exit 1
[[ -z "${workshop_id}" ]] &&
log "error" "A Workshop ID must be passed via the -w flag" &&
exit 1
[[ ! -d ~/Squad/Server-"${server_id}" ]] &&
log "error" "Unable to find Squad-Server-${server_id}, has it been installed?" &&
exit 1
# Download the mod
log "info" "Downloading mod ${workshop_id}..."
steamcmd +login anonymous +force_install_dir ~/Squad/Server-"${server_id}" +app_update 403240 validate +workshop_download_item 393380 "${workshop_id}" +quit
[[ ! -d ~/Squad/Server-"${server_id}"/steamapps/workshop/content/393380/"${workshop_id}" ]] &&
log "error" "Could not download ${workshop_id}, is that Workshop ID valid?" &&
exit 1
log "info" "Finished downloading ${workshop_id}"
log "info" "Removing old version of mod ${workshop_id} if it exists"
rm -rf ~/Squad/Server-"${server_id}"/SquadGame/Plugins/Mods/"${workshop_id}" >/dev/null 2>&1
[[ "${?}" == "0" ]] &&
log "info" "Removed old mod ${workshop_id}"
log "info" "Installing ${workshop_id} to Squad-Server-${server_id}..."
mv ~/Squad/Server-"${server_id}"/steamapps/workshop/content/393380/"${workshop_id}" \
~/Squad/Server-"${server_id}"/SquadGame/Plugins/Mods/"${workshop_id}" &&
log "info" "Successfully installed mod ${workshop_id} to Squad-Server-${server_id}" ||
log "error" "Unable to install mod ${workshop_id} to Squad-Server-${server_id}"