From 236a1fbbb9867fcc39074f2a92205b4a61f94c4f Mon Sep 17 00:00:00 2001 From: jusito <17811819+jusito@users.noreply.github.com> Date: Sat, 29 Jun 2024 14:17:37 +0200 Subject: [PATCH] fix(gmod): initConfig should handle multiple occurence --- gmod/initConfig.sh | 36 ++++++++++++++---------------------- 1 file changed, 14 insertions(+), 22 deletions(-) diff --git a/gmod/initConfig.sh b/gmod/initConfig.sh index 4ca6ccb..4f06f32 100644 --- a/gmod/initConfig.sh +++ b/gmod/initConfig.sh @@ -11,28 +11,20 @@ set -o pipefail CFG_PATH="${SERVER_PATH}/garrysmod/cfg/gmodserver.cfg" function configReplace() { - source="$1" - target="\"$2\"" + local source="$1" + local target="$2" - if ! echo "$2" | grep -qe "^[a-zA-Z0-9_ -]*$"; then - echo "[initConfig.sh]Warning your configured value for $source doesn't match regex ^[a-zA-Z0-9_-]+$. Special symbols could crash this script because they are not sanitized." - fi - - count=$(grep -Poc "($source).+" "${CFG_PATH}" || echo 0) - - echo "[initConfig.sh]Request for replacing $source to $target, source is found $count times" - - if [ "$count" == "1" ]; then - sed -Ei "/${source}.*/d" "${CFG_PATH}" - echo "$source $target" >> "${CFG_PATH}" - - elif [ "$count" == "0" ]; then - echo "" >> "${CFG_PATH}" - echo "$source $target" >> "${CFG_PATH}" - - else - echo "[initConfig.sh]can't set $1 because there are multiple in" - fi + count=$(grep -Ece "^\s*${source}\s" "${CFG_PATH}") + + echo "[initConfig.sh]Request for replacing \"$source\" to \"$target\", source is found $count times" + + sed -E -i "/^\s${source}\s.*/d" "${CFG_PATH}" + + local file_ends_on_newline=$([ "$(tail -c1 f | wc -l)" -eq 1 ] && echo true || echo false) + if ! $file_ends_on_newline; then + echo "" >> "${CFG_PATH}" + fi + echo "$source \"$target\"" >> "${CFG_PATH}" } #create default server.config @@ -56,4 +48,4 @@ if [ -n "${SERVER_RCON_PASSWORD}" ]; then fi if [ -n "${SERVER_VOICE_ENABLE}" ]; then configReplace "sv_voiceenable" "$SERVER_VOICE_ENABLE" -fi \ No newline at end of file +fi