#!/bin/bash
CHK_FILE="/home/container/mpsvrrel64"
if [ -f $CHK_FILE ]; then
    echo "VC:MP executable exists, not downloading. To update, delete mpsvrrel64."
else
    wget -q http://v04.thijn.ovh/server/VCMP04_server_${VCMP_VERSION}_linux64.zip
    unzip VCMP04_server_${VCMP_VERSION}_linux64.zip
    rm -f VCMP04_server_${VCMP_VERSION}_linux64.zip
    echo "Downloaded VC:MP Server version ${VCMP_VERSION}"

    mkdir plugins
    cd plugins/
    wget -q http://v04.thijn.ovh/allplugins_04rel006_${VCMP_PLUGIN_PATCH}.7z
    p7zip -d allplugins_04rel006_${VCMP_PLUGIN_PATCH}.7z
    chmod 700 *
    rm -f allplugins_04rel006_${VCMP_PLUGIN_PATCH}.7z
    rm -f *rel32.*
    rm -f *.dll

    cd /home/container
    chmod +x mpsvrrel64
    find
fi

if [ -f "/home/container/server.cfg" ]; then
    echo "server.cfg exists, not generating file."
else
    echo "
gamemode Default mode
plugins squirrel04rel64 sqlite04rel64 hashing04rel64 sockets04rel64
port ${SERVER_PORT}
sqgamemode main.nut
maxplayers ${MAX_SLOTS}
" > server.cfg
fi

cd /home/container/
MODIFIED_STARTUP=`echo ${STARTUP} | perl -pe 's@\{\{(.*?)\}\}@$ENV{$1}@g'`
echo "Startup: ${MODIFIED_STARTUP}"
eval ${MODIFIED_STARTUP}

exit 0
