#!/bin/bash -e
# select and start game server

. /etc/default/inithooks

[ -e $INITHOOKS_CONF ] && . $INITHOOKS_CONF

GAMEUSER=${GAMEUSER:-gameuser}
$INITHOOKS_PATH/bin/setpass.py $GAMEUSER --pass="$APP_PASS"

unset skip_init
# if sec updates set (or turnkey-init-fence is active), then we must be running
# non-interactively so we should set default values
if [[ -n "$SEC_UPDATES" ]] || systemctl -q is-active turnkey-init-fence ; then
    APP_GAMESERVER_REPO="${APP_GAMESERVER_REPO:-https://github.com/jesinmat/linux-gameservers.git}"
    APP_GAMESERVER_BRANCH="${APP_GAMESERVER_BRANCH:-master}"
    [[ -n "$GAME" ]] || skip_init=true
fi
$INITHOOKS_PATH/bin/gameserver.py --gameserver-repo="$APP_GAMESERVER_REPO" --gameserver-branch="$APP_GAMESERVER_BRANCH"

[[ -n "$skip_init" ]] || /usr/local/bin/gameserver-init
