# This is the Stage 2 Dockerfile, which handles the WeBWorK part, building
# on top of the base OS image (webwork-base) created by the Stage 1 Dockerfile.

# ==================================================================
# Phase 1 - Download webwork and pg git repositories.

FROM alpine/git AS base

# Build args specifying the branches for webwork2 and pg used to build the image.
ARG WEBWORK2_GIT_URL
ARG WEBWORK2_BRANCH
ARG PG_GIT_URL
ARG PG_BRANCH

WORKDIR /opt/base

RUN echo Cloning branch $WEBWORK2_BRANCH from $WEBWORK2_GIT_URL \
	&& echo git clone --single-branch --branch ${WEBWORK2_BRANCH} --depth 1 $WEBWORK2_GIT_URL \
	&& git clone --single-branch --branch ${WEBWORK2_BRANCH} --depth 1 $WEBWORK2_GIT_URL \
	&& rm -rf webwork2/.git webwork2/{*ignore,Dockerfile,docker-compose.yml,docker-config}

RUN echo Cloning branch $PG_BRANCH branch from $PG_GIT_URL \
	&& echo git clone --single-branch --branch ${PG_BRANCH} --depth 1 $PG_GIT_URL \
	&& git clone --single-branch --branch ${PG_BRANCH} --depth 1 $PG_GIT_URL \
	&& rm -rf  pg/.git

# Optional - include OPL (also need to uncomment further below when an included OPL is desired):
#RUN git clone --single-branch --branch main --depth 1 https://github.com/openwebwork/webwork-open-problem-library.git \
#  && rm -rf  webwork-open-problem-library/.git

# ==================================================================
# Phase 2 - Set ENV variables

# We need to change FROM before setting the ENV variables.

FROM webwork-base:forWW220

ENV WEBWORK_URL=/webwork2 \
	WEBWORK_ROOT_URL=http://localhost:8080 \
	WEBWORK_SMTP_SERVER=localhost \
	WEBWORK_SMTP_SENDER=webwork@example.com \
	WEBWORK_TIMEZONE=America/New_York \
	APP_ROOT=/opt/webwork \
	DEBIAN_FRONTEND=noninteractive \
	DEBCONF_NONINTERACTIVE_SEEN=true

# Environment variables which depend on a prior environment variable must be set
# in an ENV call after the dependencies were defined.
ENV WEBWORK_ROOT=$APP_ROOT/webwork2 \
	PG_ROOT=$APP_ROOT/pg \
	PATH=$PATH:$APP_ROOT/webwork2/bin

# ==================================================================
# Phase 3 - Install webwork2 and pg which were downloaded to /opt/base/ in phase 1
# Option: Install the OPL in the image also (about 850 MB)

RUN mkdir -p $APP_ROOT/courses $APP_ROOT/libraries $APP_ROOT/libraries/webwork-open-problem-library $APP_ROOT/webwork2 /www/www/html

COPY --from=base /opt/base/webwork2 $APP_ROOT/webwork2
COPY --from=base /opt/base/pg $APP_ROOT/pg

# Optional - include OPL (also need to uncomment above to clone from GitHub when needed):
#COPY --from=base /opt/base/webwork-open-problem-library $APP_ROOT/libraries/webwork-open-problem-library

# ==================================================================
# Phase 4 - System configuration

# 1. Setup PATH.
# 2. Create the webwork2 PID directory and the /etc/ssl/local directory in case it is needed.
# 3. Perform initial permissions setup for material INSIDE the image.
# 4. Build standard locales.
# 5. Set the default system timezone to be UTC.
# 6. Install third party javascript files.
# 7. Apply patches

# Patch files that are applied below
COPY docker-config/pgfsys-dvisvmg-bbox-fix.patch /tmp

RUN echo "PATH=$PATH:$APP_ROOT/webwork2/bin" >> /root/.bashrc \
	&& mkdir /run/webwork2 /etc/ssl/local \
	&& cd $APP_ROOT/webwork2/ \
		&& chown www-data DATA ../courses logs tmp /etc/ssl/local /run/webwork2 \
		&& chmod -R u+w DATA ../courses logs tmp /run/webwork2 /etc/ssl/local \
	&& echo "en_US ISO-8859-1\nen_US.UTF-8 UTF-8" > /etc/locale.gen \
		&& /usr/sbin/locale-gen \
		&& echo "locales locales/default_environment_locale select en_US.UTF-8\ndebconf debconf/frontend select Noninteractive" > /tmp/preseed.txt \
		&& debconf-set-selections /tmp/preseed.txt \
	&& rm /etc/localtime /etc/timezone && echo "Etc/UTC" > /etc/timezone \
		&& dpkg-reconfigure -f noninteractive tzdata \
	&& cd $WEBWORK_ROOT/htdocs \
		&& npm install \
	&& cd $PG_ROOT/htdocs \
		&& npm install \
	&& patch -p1 -d / < /tmp/pgfsys-dvisvmg-bbox-fix.patch \
	&& rm /tmp/pgfsys-dvisvmg-bbox-fix.patch

# ==================================================================
# Phase 5 - Final setup and prepare docker-entrypoint.sh
# Done near the end, so that an update to docker-entrypoint.sh can be
# done without rebuilding the earlier layers of the Docker image.

EXPOSE 8080
WORKDIR $WEBWORK_ROOT

COPY docker-config/docker-entrypoint.sh /usr/local/bin/

ENTRYPOINT ["docker-entrypoint.sh"]

# Add enviroment variables to control some things during container startup
ENV SSL=0 \
	PAPERSIZE=letter \
	SYSTEM_TIMEZONE=UTC \
	ADD_LOCALES=0 \
	ADD_APT_PACKAGES=0

# ================================================

CMD ["sudo", "-E", "-u", "www-data", "hypnotoad", "-f", "bin/webwork2"]
