## syntax=docker/dockerfile:3

FROM bitnami/nginx:1.22-debian-11

SHELL ["/bin/bash", "-eux", "-c"]

LABEL name="BitDeals WebUI"
EXPOSE 80/tcp

## Debian update; install dependencies
ARG DEBIAN_FRONTEND=noninteractive \
        DEBCONF_NOWARNINGS="yes"

USER root

RUN apt-get update -y ; \
        apt-get upgrade -y ; \
        apt-get -y install --no-install-recommends locales tzdata moreutils ; \
        apt-get clean ; \
        rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

COPY --chown=www-data:www-data ./build /app
COPY ./docker/nginx.conf /opt/bitnami/nginx/conf/server_blocks/nginx.conf

ENV WEB_CONF_FILE="/app/build/config.js"
ENV BD_FORWEB="http://127.0.0.1:80"
ENV BD_WEB_LANG="en"

SHELL ["/bin/bash", "-eu", "-c"]

# Change default variables in config file
RUN sed -i -e "/creditsText:/ s/20../$(date +%Y)/" \
	-e 's/confAuthorize:.*/confAuthorize: true,/' \
	-e 's/dmHost:.*/dmHost: "http:\/\/0.0.0.0:4999",/' \
	-e 's/confPrivateDash:.*/confPrivateDash: true,/' \
	-e 's/confDealModule:.*/confDealModule: false,/' \
	-e 's/confAdmin:.*/confAdmin: true,/' $WEB_CONF_FILE

RUN chmod 666 $WEB_CONF_FILE
USER 1001

# Confugure BitDeals WebUI
CMD cat $WEB_CONF_FILE | \
		sed -e "s|defaultLanguage:.*|defaultLanguage: \"$BD_WEB_LANG\",|" \
			-e "s|apiTokenUrl:.*|apiTokenUrl: \"$BD_FORWEB/oauth2/token\",|" \
			-e "s|apiDomain:.*|apiDomain: \"$BD_FORWEB\",|" \
			-e "s|wsDomain:.*|wsDomain: \"wss://$(basename $BD_FORWEB)\",|" | \
		sponge $WEB_CONF_FILE ;\
	/opt/bitnami/scripts/nginx/run.sh

