#!/bin/bash -e
# reconfigure: yes
# reconfigure-title: Game Server
# reconfigure-description: Change the game account password and software source; start the game server.
# 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
# The first-login fence stays active during interactive setup. Headless
# turnkey-init runs also need defaults and must not open an interactive menu.
if [[ ! -t 0 ]] || { [[ -z "${_TURNKEY_INIT:-}" ]] && { [[ -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

# Nginx may have attempted to start while the shared SSL hook was replacing
# the appliance certificate. Restore it after that hook has completed.
systemctl restart nginx
