FROM sapmachine:21-jre-headless-ubuntu


ENV WORKDIR_PATH=/workspace
ENV CONFIG_PATH=${WORKDIR_PATH}/config
ENV DATA_PATH=${WORKDIR_PATH}/data

ADD ./src ${CONFIG_PATH}
RUN  mkdir ${DATA_PATH}


RUN mkdir ${DATA_PATH}/logs && \
    ln -sf ${DATA_PATH}/logs ${CONFIG_PATH}
RUN touch ${DATA_PATH}/pv-groups.json && \
    ln -sf ${DATA_PATH}/pv-groups.json \
        ${CONFIG_PATH}/plugins/pv-addon-groups/groups.json

# Generate unicode locale so that cyrillic characters display properly
RUN apt-get update -y && apt-get install -y locales && \
    echo en_US.UTF-8 UTF-8 > /etc/locale.gen && \
    dpkg-reconfigure --frontend=noninteractive locales
ENV LANG en_US.UTF-8


VOLUME ${DATA_PATH}


ENV MEMORY=4G

ENV PROXY_SECRET=00000000-0000-0000-0000-000000000000
ENV SERVERS=lobby="lobby:25565";survival="survival:25565"
ENV TRY=lobby;survival

ENV LIBRELOGIN_LIMBOS=limbo
ENV LIBRELOGIN_LOBBIES=lobby
ENV LIBRELOGIN_DB_HOST=127.0.0.1
ENV LIBRELOGIN_DB_PORT=3306
ENV LIBRELOGIN_DB_NAME=coreprotect_db
ENV LIBRELOGIN_DB_USERNAME=coreprotect
ENV LIBRELOGIN_DB_PASSWORD=0000

ENV LUCKPERMS_DB_HOST=127.0.0.1
ENV LUCKPERMS_DB_PORT=3306
ENV LUCKPERMS_DB_NAME=luckperms
ENV LUCKPERMS_DB_USERNAME=luckperms
ENV LUCKPERMS_DB_PASSWORD=0000

ENV SKINSRESTORER_DB_HOST=127.0.0.1
ENV SKINSRESTORER_DB_PORT=3306
ENV SKINSRESTORER_DB_NAME=skinsrestorer_db
ENV SKINSRESTORER_DB_USERNAME=skinsrestorer
ENV SKINSRESTORER_DB_PASSWORD=0000

ENV VOICE_SECRET=00000000-0000-0000-0000-000000000000
ENV VOICE_SERVERS=lobby="lobby:25565";survival="survival:25565"

ENV SONAR_DB_HOST=127.0.0.1
ENV SONAR_DB_PORT=3306
ENV SONAR_DB_NAME=sonar_db
ENV SONAR_DB_USERNAME=sonar
ENV SONAR_DB_PASSWORD=0000

WORKDIR ${WORKDIR_PATH}/config

EXPOSE 25565/tcp

# TODO: Make awk commands sapn multiple lines
# TODO: Use one call to awk to perform substitutions
CMD \
    # Add proxy secret
    sed -i "s/_PROXY_SECRET_/${PROXY_SECRET}/g" forwarding.secret && \

    # Substitute librelogin database path
    sed -i "s~../../librelogin.db~../../../..${DATA_PATH}/librelogin.db~g" plugins/librelogin/config.conf && \

    # Add server list to velocity.toml
    awk -v servers_str="${SERVERS}" '{ count=split(servers_str,servers_arr,";"); servers_fmt_str = ""; for (i = 1; i <= count; i++) servers_fmt_str = servers_fmt_str servers_arr[i] "\n"; sub(/_SERVERS_/,servers_fmt_str); print }' velocity.toml > velocity.toml.tmp && mv -T -f velocity.toml.tmp velocity.toml && \
    # Add try servers to velocity.toml
    awk -v try_str="${TRY}" '{ count=split(try_str,try_arr,";"); try_fmt_str = ""; for (i = 1; i <= count; i++) try_fmt_str = try_fmt_str "\"" try_arr[i] "\"" ",\n    "; sub(/_TRY_/,try_fmt_str); print }' velocity.toml > velocity.toml.tmp && mv -T -f velocity.toml.tmp velocity.toml && \

    # Add limbo servers to librelogin config
    awk -v limbos_str="${LIBRELOGIN_LIMBOS}" '{ count=split(limbos_str,limbos_arr,";"); limbos_fmt_str = ""; for (i = 1; i <= count; i++) limbos_fmt_str = limbos_fmt_str limbos_arr[i] ",\n    "; sub(/_LIBRELOGIN_LIMBOS_/,limbos_fmt_str); print }' plugins/librelogin/config.conf > plugins/librelogin/config.conf.tmp && mv -T -f plugins/librelogin/config.conf.tmp plugins/librelogin/config.conf && \
    # Add lobby servers to librelogin config
    awk -v lobbies_str="${LIBRELOGIN_LOBBIES}" '{ count=split(lobbies_str,lobbies_arr,";"); lobbies_fmt_str = ""; for (i = 1; i <= count; i++) lobbies_fmt_str = lobbies_fmt_str lobbies_arr[i] ",\n        "; sub(/_LIBRELOGIN_LOBBIES_/,lobbies_fmt_str); print }' plugins/librelogin/config.conf > plugins/librelogin/config.conf.tmp && mv -T -f plugins/librelogin/config.conf.tmp plugins/librelogin/config.conf && \
    # Add database info to librelogin config
    sed -i "s/_LIBRELOGIN_DB_HOST_/${LIBRELOGIN_DB_HOST}/g" plugins/librelogin/config.conf && \
    sed -i "s/_LIBRELOGIN_DB_PORT_/${LIBRELOGIN_DB_PORT}/g" plugins/librelogin/config.conf && \
    sed -i "s/_LIBRELOGIN_DB_NAME_/${LIBRELOGIN_DB_NAME}/g" plugins/librelogin/config.conf && \
    sed -i "s/_LIBRELOGIN_DB_USERNAME_/${LIBRELOGIN_DB_USERNAME}/g" plugins/librelogin/config.conf && \
    sed -i "s/_LIBRELOGIN_DB_PASSWORD_/${LIBRELOGIN_DB_PASSWORD}/g" plugins/librelogin/config.conf && \

    # Add database info to luckperms config
    sed -i "s/_LUCKPERMS_DB_HOST_/${LUCKPERMS_DB_HOST}/g" plugins/luckperms/config.yml && \
    sed -i "s/_LUCKPERMS_DB_PORT_/${LUCKPERMS_DB_PORT}/g" plugins/luckperms/config.yml && \
    sed -i "s/_LUCKPERMS_DB_NAME_/${LUCKPERMS_DB_NAME}/g" plugins/luckperms/config.yml && \
    sed -i "s/_LUCKPERMS_DB_USERNAME_/${LUCKPERMS_DB_USERNAME}/g" plugins/luckperms/config.yml && \
    sed -i "s/_LUCKPERMS_DB_PASSWORD_/${LUCKPERMS_DB_PASSWORD}/g" plugins/luckperms/config.yml && \

    # Add database info to skinsrestorer config
    sed -i "s/_SKINSRESTORER_DB_HOST_/${SKINSRESTORER_DB_HOST}/g" plugins/skinsrestorer/config.yml && \
    sed -i "s/_SKINSRESTORER_DB_PORT_/${SKINSRESTORER_DB_PORT}/g" plugins/skinsrestorer/config.yml && \
    sed -i "s/_SKINSRESTORER_DB_NAME_/${SKINSRESTORER_DB_NAME}/g" plugins/skinsrestorer/config.yml && \
    sed -i "s/_SKINSRESTORER_DB_USERNAME_/${SKINSRESTORER_DB_USERNAME}/g" plugins/skinsrestorer/config.yml && \
    sed -i "s/_SKINSRESTORER_DB_PASSWORD_/${SKINSRESTORER_DB_PASSWORD}/g" plugins/skinsrestorer/config.yml && \

    # Add Plasmo Voice proxy secret
    cat plugins/plasmovoice/forwarding-secret | sed "s/00000000-0000-0000-0000-000000000000/${VOICE_SECRET}/g" | tr -d '\n' > plugins/plasmovoice/forwarding-secret.tmp && mv -T -f plugins/plasmovoice/forwarding-secret.tmp plugins/plasmovoice/forwarding-secret && \
    # Add server list to Plasmo Voice config
    awk -v servers_str="${VOICE_SERVERS}" '{ count=split(servers_str,servers_arr,";"); servers_fmt_str = ""; for (i = 1; i <= count; i++) servers_fmt_str = servers_fmt_str servers_arr[i] "\n"; sub(/_SERVERS_/,servers_fmt_str); print }' plugins/plasmovoice/config.toml > plugins/plasmovoice/config.toml.tmp && mv -T -f plugins/plasmovoice/config.toml.tmp plugins/plasmovoice/config.toml && \

    # Add database info to sonar config
    sed -i "s/_SONAR_DB_HOST_/${SONAR_DB_HOST}/g" plugins/sonar/config.yml && \
    sed -i "s/_SONAR_DB_PORT_/${SONAR_DB_PORT}/g" plugins/sonar/config.yml && \
    sed -i "s/_SONAR_DB_NAME_/${SONAR_DB_NAME}/g" plugins/sonar/config.yml && \
    sed -i "s/_SONAR_DB_USERNAME_/${SONAR_DB_USERNAME}/g" plugins/sonar/config.yml && \
    sed -i "s/_SONAR_DB_PASSWORD_/${SONAR_DB_PASSWORD}/g" plugins/sonar/config.yml && \

    java -Xms${MEMORY} -Xmx${MEMORY} -XX:+UseG1GC -XX:G1HeapRegionSize=4M -XX:+UnlockExperimentalVMOptions -XX:+ParallelRefProcEnabled -XX:+AlwaysPreTouch -XX:MaxInlineLevel=15 -jar *.jar
