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}


# Add symlinks to Default Minecraft files
RUN touch ${DATA_PATH}/banned-ips.json && \
    ln -sf ${DATA_PATH}/banned-ips.json ${CONFIG_PATH}
RUN touch ${DATA_PATH}/banned-players.json && \
    ln -sf ${DATA_PATH}/banned-players.json ${CONFIG_PATH}
RUN mkdir ${DATA_PATH}/logs && \
    ln -sf ${DATA_PATH}/logs ${CONFIG_PATH}
RUN touch ${DATA_PATH}/ops.json && \
    ln -sf ${DATA_PATH}/ops.json ${CONFIG_PATH}
RUN touch ${DATA_PATH}/usercache.json && \
    ln -sf ${DATA_PATH}/usercache.json ${CONFIG_PATH}
RUN touch ${DATA_PATH}/whitelist.json && \
    ln -sf ${DATA_PATH}/whitelist.json ${CONFIG_PATH}

# Add symlinks to Plasmo Voice files
RUN touch ${DATA_PATH}/pv-voice_mutes.json && \
    ln -sf ${DATA_PATH}/pv-voice_mutes.json \
        ${CONFIG_PATH}/plugins/PlasmoVoice/voice_mutes.json

# Add symlinks to SuperVanish files
RUN mkdir -p ${DATA_PATH}/SuperVanish && \
    touch ${DATA_PATH}/SuperVanish/data.yml && \
    ln -sf ${DATA_PATH}/SuperVanish/data.yml \
        ${CONFIG_PATH}/plugins/SuperVanish/data.yml

# Add symlinks to CarbonChat files
RUN mkdir -p ${DATA_PATH}/CarbonChat/users && \
    ln -sf ${DATA_PATH}/CarbonChat/users \
        ${CONFIG_PATH}/plugins/CarbonChat/users

# Add symlinks to CustomPlayerModels files
RUN mkdir -p ${DATA_PATH}/CustomizablePlayerModels && \
    ln -sf ${DATA_PATH}/CustomizablePlayerModels/ \
        ${CONFIG_PATH}/plugins/CustomizablePlayerModels

# Add symlinks to FancyHolograms files
RUN mkdir -p ${DATA_PATH}/FancyHolograms/logs && \
    ln -sf ${DATA_PATH}/FancyHolograms/logs/ \
        ${CONFIG_PATH}/plugins/FancyHolograms/logs

# Add symlinks to FancyNpcs files
RUN mkdir -p ${DATA_PATH}/FancyNpcs/logs && \
    ln -sf ${DATA_PATH}/FancyNpcs/logs/ \
        ${CONFIG_PATH}/plugins/FancyNpcs/logs

# 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 && \
    rm -Rf var/lib/apt/lists/*
ENV LANG en_US.UTF-8


VOLUME ${DATA_PATH}


EXPOSE 25565/tcp


ENV GID=988
ENV UID=999

ENV MEMORY=4G
ENV PROXY_SECRET=00000000-0000-0000-0000-000000000000

ENV VOICE_SECRET=00000000-0000-0000-0000-000000000000

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


WORKDIR ${WORKDIR_PATH}/config

CMD \
    # Create and switch to user with desired UID and GID.
    # All processes that create/change files in ${DATA_PATH}
    # must be run under this user.
    groupadd -g ${GID} worker && \
    useradd -M -g ${GID} -u ${UID} worker && \
    chmod -R o-rwx ${WORKDIR_PATH} && \

    # Add proxy secret
    sed -i "s/_PROXY_SECRET_/${PROXY_SECRET}/g" config/paper-global.yml && \

    # Add Plasmo Voice 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 luckperms database secrets
    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 && \

    # Change UID and GID of used files to desired values.
    chown -R worker:worker ${WORKDIR_PATH} && \

    # Launch
    su worker -c "java -Xms${MEMORY} -Xmx${MEMORY} -jar *.jar -nogui"
