# Copyright (c) 2019 ZettaDB inc. All rights reserved.
# This source code is licensed under Apache 2.0 License,
# combined with Common Clause Condition 1.0, as detailed in the NOTICE file.

FROM ubuntu:20.04

# Set noninteractive frontend
ENV DEBIAN_FRONTEND=noninteractive

# Prepare environment
RUN chmod -R 777 /tmp && \
    mkdir -p /kunlun && \
    rm -f /bin/sh && ln -s /bin/bash /bin/sh

COPY . /kunlun/
WORKDIR /kunlun

ARG KUNLUN_VERSION

# Install dependencies and clean up
RUN cp -f sources.list /etc/apt && \
    apt-get update && \
    apt-get install -y --no-install-recommends \
        apt-utils \
        apache2 \
        apache2-utils \
        php \
        php-pgsql \
        php-mysql \
        sudo \
        php-curl \
        php-zip \
        php-xml \
        php-mbstring \
        libapache2-mod-php \
        vim \
        unzip \
        python2 \
        cron \
        curl \
        php-redis \
        php-mongodb && \
    apt-get clean && \
    rm -rf /var/lib/apt/lists/*

RUN echo "export KUNLUN_VERSION=$KUNLUN_VERSION" > /kunlun/VERSION
RUN /bin/bash -x /kunlun/build.sh

# Setup crontab
RUN mkdir -p /etc/cron.d
RUN cp -f /var/www/html/KunlunMonitor/docker_crontab /etc/cron.d/kunlun-cron
RUN chmod 0644 /etc/cron.d/kunlun-cron && \
    touch /var/log/cron.log && \
    chmod 0666 /var/log/cron.log

# Start cron service and apache
CMD cron && apache2ctl -D FOREGROUND

# Expose ports (80 for Apache, 3000 potentially for frontend dev)
EXPOSE 80
EXPOSE 3000
