FROM tomcat:9-jre17-temurin-jammy
# From: https://hub.docker.com/_/tomcat/
# The default Tomcat environment in the image is:
# CATALINA_BASE:   /usr/local/tomcat
# CATALINA_HOME:   /usr/local/tomcat
# CATALINA_TMPDIR: /usr/local/tomcat/temp
# JRE_HOME:        /usr
# CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
# The configuration files are available in /usr/local/tomcat/conf/.

ENV CATALINA_BASE=/usr/local/tomcat
ENV CATALINA_HOME=/usr/local/tomcat

WORKDIR /root

# pre-configure port 8080 (no TSL)
COPY deps/server.xml deps/server-connector.xml* ${CATALINA_BASE}/conf/
COPY deps/soda.logging.properties ${CATALINA_BASE}/conf/
COPY deps/setenv.sh ${CATALINA_BASE}/bin/
# DB used for authorization
# Tomcat must load DB-driver (postgresql_*.jar), vlkb-soda does not explicitely load DB-drivers
COPY deps/postgresql-*.jar  ${CATALINA_BASE}/lib

# modif permissions to allow run as non-root: need to config TSL and ROOT-CONTEXT
WORKDIR ${CATALINA_BASE}
RUN chmod -R a+rwX conf


RUN apt-get -y update \
 && apt-get -y install apt-utils \
 && apt-get -y install libcfitsio-bin unzip libdavix0v5


ENV WEBAPP_DIR=/webapps/vlkb-soda

WORKDIR /root

COPY deps/ast_9.2.9-1_amd64.deb ./

RUN dpkg -i /root/ast_9.2.9-1_amd64.deb && ldconfig \
 && mkdir -p ${WEBAPP_DIR} \
 && mkdir -p /srv/datasets \
 && mkdir -p /srv/cutouts \
 && mkdir -p /etc/pki/tls

ARG VLKB_VERSION
COPY vlkb-${VLKB_VERSION}.deb ./
COPY vlkb-soda-${VLKB_VERSION}.war ${WEBAPP_DIR}/
RUN dpkg -i vlkb-${VLKB_VERSION}.deb \
 && cd ${WEBAPP_DIR} && unzip vlkb-soda-${VLKB_VERSION}.war


# configure instance

ENV INST_DIR=/usr/local

RUN echo "${INST_DIR}/lib" > /etc/ld.so.conf.d/ast.conf && ldconfig \
 && echo "fits_path_surveys=/srv/datasets" > $WEBAPP_DIR/WEB-INF/classes/cutout.properties

COPY deps/context.xml ${WEBAPP_DIR}/META-INF/context.xml

# enable IA2 token filter update
RUN chmod -R a+rwX ${WEBAPP_DIR}/WEB-INF \
 && touch  ${WEBAPP_DIR}/WEB-INF/classes/auth.properties \
 && touch  ${WEBAPP_DIR}/WEB-INF/classes/iamtoken.properties \
 && chmod a+rw ${WEBAPP_DIR}/WEB-INF/classes/*.properties

# FIXME must use 1002: regardless how /srv/cutouts is created
# in container it is owned by user 1002:1002 with rwxr-xr-x
RUN groupadd --gid 1002 admin \
 && useradd --uid 1002 --gid admin --shell /bin/bash --create-home --no-log-init admin

COPY start-soda.sh /home/admin/start-soda.sh
RUN chown admin:admin /home/admin/start-soda.sh
RUN chown admin:admin /srv/cutouts

USER admin
WORKDIR /home/admin

CMD ["/home/admin/start-soda.sh"]

