FROM debian:bullseye-slim
LABEL Description="vlkb tomcat9"

WORKDIR /root
ENV HOME /root


RUN apt -y update \
 && apt -y install sudo procps psmisc tree wget curl vim make build-essential checkinstall git \
                   libcfitsio-dev libpqxx-dev librabbitmq-dev libcsv-dev gfortran \
                   openjdk-17-jre-headless unzip \
                   rabbitmq-server openjdk-17-jre openjdk-17-jdk tomcat9 tomcat9-admin \
                   postgresql-client \
                   time montage

ENV JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64
ENV CATALINA_BASE=/var/lib/tomcat9
ENV CATALINA_HOME=/usr/share/tomcat9
ENV CATALINA_TMPDIR=/tmp

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 vlkb-obscore-${VLKB_VERSION}.deb vlkbd-${VLKB_VERSION}.deb ./
COPY vlkb-soda-${VLKB_VERSION}.war ${WEBAPP_DIR}/
RUN dpkg -i vlkb-${VLKB_VERSION}.deb vlkb-obscore-${VLKB_VERSION}.deb vlkbd-${VLKB_VERSION}.deb \
 && cd ${WEBAPP_DIR} && jar -xf vlkb-soda-${VLKB_VERSION}.war
 
# Tomcat must load DB-driver (postgresql_*.jar), vlkb-soda does not explicitely load DB-drivers
COPY deps/postgresql-*.jar  /var/lib/tomcat9/lib



# configure instance

ENV INST_DIR=/usr/local

COPY deps/vlkbd_exec.sh ${INST_DIR}/bin

RUN mkdir -p ${INST_DIR}/etc/vlkb-obscore \
 && mkdir -p ${INST_DIR}/etc/vlkbd \
 && echo "${INST_DIR}/lib" > /etc/ld.so.conf.d/ast.conf \
 && ldconfig

# configure during docker build-time

COPY deps/vlkb-obscore.datasets.conf ${INST_DIR}/etc/vlkb-obscore/datasets.conf
COPY deps/vlkbd.datasets.conf ${INST_DIR}/etc/vlkbd/datasets.conf

# precofigure port 8080 (no SSL)
COPY deps/server.xml deps/server-connector.xml /etc/tomcat9/

# configure during docker run-time

COPY entrypoint.sh /root

RUN echo "alias log-catalina='ls -t /var/log/tomcat9/catalina*.log | head -n 1 | xargs tail -200 '" >> /root/.bashrc$
# run

RUN pwd && chmod +x /root/entrypoint.sh
CMD ["sh", "-c", "/root/entrypoint.sh"]

