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
ENV HOME /root


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


ENV WEBAPP_DIR=/webapps/vlkb-soda


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 /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

# 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/

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

# 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

# enable SKA IAM token filter update
RUN chmod a+rw ${WEBAPP_DIR}/WEB-INF/web.xml \
 && touch  ${WEBAPP_DIR}/WEB-INF/classes/ia2token.properties \
 && touch  ${WEBAPP_DIR}/WEB-INF/classes/iamtoken.properties \
 && chmod a+rw ${WEBAPP_DIR}/WEB-INF/classes/*.properties

env ACCESS_CONTEXT_ROOT=datasets
# configure during start-up
COPY start-soda.sh.soda /root/start-soda.sh


RUN chmod +rx /root && chmod +rx /root/start-soda.sh
USER 1000:1000
CMD ["sh", "-c", "/root/start-soda.sh"]

