FROM php:7.4-apache

########################################
# APT local cache
# work around because COPY failed if no source file
COPY .dummy .apt-mirror-confi[g] .proxy-confi[g] /
RUN cp /.proxy-config /etc/profile.d/proxy.sh 2> /dev/null || true
RUN if [ -f /.apt-mirror-config ] ; then . /.apt-mirror-config && sed -i \
	-e "s%s\?://deb.debian.org%://${APT_MIRROR_DEBIAN}%g" \
	-e "s%s\?://security.debian.org%://${APT_MIRROR_DEBIAN_SECURITY}%g" \
	-e "s%s\?://archive.ubuntu.com%://${APT_MIRROR_UBUNTU}%g" \
	-e "s%s\?://security.ubuntu.com%://${APT_MIRROR_UBUNTU_SECURITY}%g" \
	/etc/apt/sources.list; fi

########################################
RUN apt-get update --quiet && apt-get install -y \
    libicu-dev libpq-dev zlib1g-dev libicu-dev \
    libzip-dev wget zip patch mailutils sendmail

RUN apt-get install -y emacs php-elisp telnet

RUN sed -i 's/127.0.0.1/smtp/' /etc/mail/submit.mc
RUN echo "define(\`SMART_HOST',\`smtp')" >> /etc/mail/sendmail.mc
RUN m4 /etc/mail/submit.mc > /etc/mail/submit.cf
RUN m4 /etc/mail/sendmail.mc > /etc/mail/sendmail.cf

#install composer setup script
COPY dockers/jirafeau/composer-setup.sh /usr/local/bin/
RUN chmod +x /usr/local/bin/composer-setup.sh

#install internationalization libs
RUN docker-php-ext-configure intl
RUN docker-php-ext-install intl
RUN docker-php-ext-install zip

########################################
#install jirafeau
RUN mkdir /var/jirafeau/ /var/jirafeauData/
WORKDIR /var/jirafeau
COPY --chown=www-data git/Jirafeau/ .
COPY --chown=www-data git/depollueur/src/Jirafeau/[aft].php ./
COPY --chown=www-data dockers/jirafeau/media/kaz media/kaz
RUN sed -i -e '1i\<p>La limite des t&eacute;l&eacute;versements est actuellement de <?php echo ini_get("post_max_size"); ?></p>' lib/template/footer.php
RUN sed -i -e '/<div id="jyraphe">/i\<div id="kaz">' lib/template/footer.php

COPY dockers/jirafeau/config/composer.json .
RUN /usr/local/bin/composer-setup.sh
RUN php composer.phar install

RUN echo '\n\
upload_max_filesize = 1024M\n\
post_max_size = 1024M\n\
[mail function]\n\
SMTP = smtp\n\
smtp_port = 25\n\
sendmail_path=/usr/sbin/sendmail -t -i\n\
sendmail_from = no-reply@kaz.local\n\
' > /usr/local/etc/php/php.ini

RUN chown -R www-data.www-data . /var/jirafeauData/
RUN chmod o=,ug=rwX -R . /var/jirafeauData/
RUN rm -rf .git .gitignore .gitlab-ci.yml CONTRIBUTING.md README.md Dockerfile

VOLUME ["/var/jirafeauData", "/etc/apache2/sites-available"]

RUN echo "#!/bin/sh" >> /entrypoint.sh
RUN echo "chown -R www-data: /var/jirafeauData/" >> /entrypoint.sh
RUN echo "/usr/sbin/apache2ctl -D FOREGROUND" >> /entrypoint.sh
RUN chmod u=+x /entrypoint.sh


EXPOSE 80

ENTRYPOINT ["/entrypoint.sh"]