Bash_Scripts/CentOS/Arma/Mods/Arma-Install-Mod.bash
2021-07-28 09:29:40 -05:00

125 lines
3.6 KiB
Bash
Executable File

#!/bin/bash --posix
usage() {
printf "%s\n" \
"Usage: $(basename "${0}") -u <steam username> -s <server number> -w <workshop id> -m <mod name>
--user <string> | -u <string>
Example:
--user Sbinalla
--server <int> | -s <int>
Example:
--server 0
--workshop-id <int> | -w <int>
Example:
--workshop-id 450814997
--mod-name <string> | -m <string>
Example:
--mod-name @cba3
Note:
All mod names are converted to lowercase, Arma 3 requires lowercase mod names for linux."
}
error() {
printf "\n%s\n" "$1" >&2
exit 1
}
confirmation() {
local choice
while true; do
read -p "${1}" -n 1 -r choice
case "${choice}" in
y | Y)
echo ""
return 1
;;
n | N)
echo ""
return 0
;;
*) echo -e "\nInput must be either y, Y, n, or N" ;;
esac
done
}
## Variable setup
SERVERNUM=""
STEAMUSER=""
WORKSHOPID=""
MODNAME=""
# Arg parsing is done here
while :; do
case $1 in
-h | -\? | --help)
usage # Display a usage synopsis.
exit
;;
--) # End of all options.
shift
break
;;
-s | --server)
shift
SERVERNUM="${1}"
;;
-u | --user)
shift
STEAMUSER="${1}"
;;
-w | --workshop-id)
shift
WORKSHOPID="${1}"
;;
-m | --mod-name)
shift
MODNAME="${1}"
;;
-?*)
printf 'Unknown option: %s\n' "$1" >&2
usage
error
;;
*) # Default case: No more options, so break out of the loop.
break ;;
esac
shift
done
## Next chunk is checks to see that variables are passed and match correct arguments etc.
[[ "${SERVERNUM}" == "" ]] && error "Error: A server number must be provided"
[[ "${SERVERNUM}" =~ [^0-9]+. ]] && error "Error: The argument for server must be a number"
[[ "${STEAMUSER}" == "" ]] && error "Error: A steam user must be provided"
[[ "${WORKSHOPID}" == "" ]] && error "Error: An argument for workshop-id must be provided"
[[ "${WORKSHOPID}" =~ [^0-9]+. ]] && error "Error: The argument for workshop-id must be a number"
[[ "${MODNAME}" == "" ]] && error "Error: An argument for mod-name must be provided"
[[ -d ~/Arma/Server-"${SERVERNUM}" ]] || error "Error: Arma Server ${SERVERNUM} does not exist!"
# Converts the modname to lowercase as Arma 3 on linux requires lowercase mod directory names
MODNAME=$(echo "${MODNAME}" | tr '[:upper:]' '[:lower:]')
# Checks if the modname has an @ symbol as the first character, if not we add it
[[ "${MODNAME}" =~ ^@ ]] || MODNAME="@${MODNAME}"
# Create the mods directory if it doesn't exist
[[ -d ~/Arma/Server-"${SERVERNUM}"/mods ]] \
|| "$(mkdir ~/Arma/Server-"${SERVERNUM}"/mods && echo "Info: Created mods directory as it did not exist")"
echo "Info: Downloading mod ${WORKSHOPID} as ${MODNAME}"
# Download the mod
steamcmd +login "${STEAMUSER}" +force_install_dir ~/Arma/Server-"${SERVERNUM}" +workshop_download_item 107410 "${WORKSHOPID}" +quit \
&& echo && echo "Info: Finished Downloading mod"
# Finally install the mod into the mods directory
echo "Info: Installing the mod ${MODNAME}"
[[ -d "${HOME}/Arma/Server-${SERVERNUM}/mods/${MODNAME}" ]] && rm -rf "${HOME}/Arma/Server-${SERVERNUM}/mods/${MODNAME}"
[[ -d ~/Arma/Server-"${SERVERNUM}"/mods/"${MODNAME}" ]] \
|| mkdir -p ~/Arma/Server-"${SERVERNUM}"/mods/"${MODNAME}" \
&& echo "Info: Created the ${MODNAME} directory within mods"
mv "${HOME}/Arma/Server-${SERVERNUM}/steamapps/workshop/content/107410/${WORKSHOPID}"/* "${HOME}/Arma/Server-${SERVERNUM}/mods/${MODNAME}"
echo "Info: Successfully installed the mod ${MODNAME} (${WORKSHOPID}) to Server-${SERVERNUM}"