FROM ubuntu:22.04

LABEL maintainer="bot-lastwar"
LABEL description="Bot LastWar - Discord/Telegram Bot with Web Interface"

ARG DEBIAN_FRONTEND=noninteractive

RUN apt-get update && apt-get install -y \
    apache2 \
    php \
    php-cli \
    php-mysql \
    php-mbstring \
    php-xml \
    php-curl \
    php-zip \
    php-json \
    libapache2-mod-php \
    composer \
    nodejs \
    npm \
    supervisor \
    curl \
    git \
    unzip \
    nano \
    libzip-dev \
    bash \
    bash-completion \
    readline-common \
    less \
    && rm -rf /var/lib/apt/lists/*

RUN a2enmod rewrite headers env

RUN npm install -g yarn

RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

RUN mkdir -p /var/www/html/bot/logs \
    && mkdir -p /var/www/html/bot/galeria \
    && mkdir -p /var/run/supervisor \
    && mkdir -p /etc/supervisor/conf.d

COPY docker/apache/*.conf /etc/apache2/sites-available/
COPY docker/supervisor/*.conf /etc/supervisor/conf.d/

RUN a2ensite reod-dragon.ddns.net.conf || true
RUN a2dissite 000-default.conf || true

WORKDIR /var/www/html/bot

EXPOSE 80

COPY docker/entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh

COPY docker/zimaos.env.template /var/www/html/bot/docker/

COPY . . /var/www/html/bot/

# Eliminar archivos .env de desarrollo que vienen del código fuente
# En Docker usaremos el archivo .env generado desde variables de entorno
RUN rm -f /var/www/html/bot/.env.reod /var/www/html/bot/.env.pruebas /var/www/html/bot/.env.example 2>/dev/null || true

RUN rm -rf /var/www/html/bot/docker/Dockerfile \
    /var/www/html/bot/docker-compose*.yml 2>/dev/null || true

RUN chmod -R 777 /var/www/html/bot/logs \
    /var/www/html/bot/galeria \
    /var/run/supervisor

RUN touch /var/log/apache2/error.log \
    /var/log/apache2/access.log \
    && chmod 666 /var/log/apache2/*.log

# Set index.php as first DirectoryIndex
RUN cat > /etc/apache2/mods-enabled/dir.conf << 'EOF'
<IfModule mod_dir.c>
    DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm
</IfModule>
EOF

CMD ["/entrypoint.sh"]
