Knowledge Base

Preserving for the future: Shell scripts, AoC, and more

aoe2de-set-username.sh (Source)

#!/bin/sh
# Dependencies:
#    dep-devuan: bgscripts, moreutils
# Env vars: APPLY VERBOSE
USERNAME="${USERNAME:-${1}}"
if test -z "${USERNAME}" ; then echo "Fatal! Need \$1 or USERNAME. Aborted." 1>&2 ; exit 1 ; fi
if test -z "${INFILE}" ;
then
    scriptdir="$( dirname "$( readlink -f "${0}" )" )"
    # find first one
    INFILE="$( find "${scriptdir}" "${scriptdir}/.." "${scriptdir}/../.." -ipath '*/steam_settings/configs.user.ini' -print -quit )"
fi
# This depends on bgscripts.
/usr/libexec/bgscripts/py/modconf.py ${APPLY:+-a} ${VERBOSE:+-v} -s '[user::general]' "${INFILE}" set account_name "${USERNAME}"
# modconf.py has a bug where it adds an additional newline after that updated line, so
# collapse multiple empty lines into a single empty line: ref https://unix.stackexchange.com/a/72749
awk '!NF {if (++n <= 1) print; next}; {n=0;print}' "${INFILE}" | sponge "${INFILE}"
if test -z "${APPLY}" ; then echo "Dry run only! Use APPLY=1" 1>&2 ; fi