FROM debian:bullseye-slim

WORKDIR /root
ENV HOME /root

RUN apt-get -y update \
 && apt-get -y --no-install-recommends install libcfitsio9 \
                  unzip openjdk-17-jre-headless tomcat9 libtcnative-1 ca-certificates

ENV CATALINA_BASE=/var/lib/tomcat9
ENV CATALINA_HOME=/usr/share/tomcat9

RUN rm -rf $CATALINA_BASE/webapps/examples/ \
           $CATALINA_BASE/webapps/docs/ \
           $CATALINA_BASE/webapps/host-manager

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/surveys \
 && 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 \
 && apt-get autoremove && apt-get clean



# configure build instance

ENV INST_DIR=/usr/local

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

# change webapps-dir and preconfigure port 8080 (no SSL)
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/

# configure at start-up

COPY start-soda.sh.soda /root/start-soda.sh

# modif permissions to allow run as non-root
WORKDIR ${CATALINA_HOME}
# orig was: chmod 1777 logs temp work;
# logs --> /var/log/tomcat9 work --> /var/cache/tomcat9 temp (missing)
RUN chmod -R +rX .; chmod 1777 /var/log/tomcat9 /var/cache/tomcat9
WORKDIR ${CATALINA_BASE}
RUN chmod -R a+rwX conf
RUN chmod +rx /root && chmod +rx /root/start-soda.sh
CMD ["sh", "-c", "/root/start-soda.sh"]

