FROM debian:stretch

MAINTAINER Robin Thoni <robin@rthoni.com>

RUN apt-get update && apt-get -y install\
        wget &&\
        apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

ARG ZABBIX_VERSION=4.0

RUN sed -i 's/main/main non-free/' /etc/apt/sources.list

RUN if [ "$(uname -m)" = "x86_64" ]; then echo -n "debian"; else echo -n "raspbian"; fi > /tmp/distrib &&\
    wget http://repo.zabbix.com/zabbix/${ZABBIX_VERSION}/$(cat /tmp/distrib)/pool/main/z/zabbix-release/zabbix-release_${ZABBIX_VERSION}-2+stretch_all.deb -O /tmp/zabbix.deb &&\
    rm /tmp/distrib &&\
    dpkg -i /tmp/zabbix.deb

RUN apt-get update && apt-get -y install\
        zabbix-server-pgsql zabbix-get snmp snmp-mibs-downloader snmptrapd\
        snmp &&\
        apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

RUN chown -R zabbix:zabbix /etc/zabbix &&\
    mkdir -p /var/run/zabbix &&\
    chown zabbix:zabbix /var/run/zabbix/

RUN rm -rf /var/log/* &&\
    mkdir -p /var/log/zabbix/ &&\
    chown zabbix:zabbix /var/log/zabbix/ &&\
    ln -s /dev/stdout /var/log/zabbix/zabbix_server.log

RUN chmod u+s /usr/bin/fping &&\
    chmod u+s /usr/bin/fping6

COPY ./zabbix_server.conf /etc/zabbix/zabbix_server.conf

COPY ./vars-vars /etc/vars-vars

COPY ./vars-files /etc/vars-files

COPY ./run.sh /run.sh

COPY ./certs/* /etc/zabbix/certs/

RUN chown zabbix:zabbix /etc/zabbix/certs/* &&\
    chmod 400 /etc/zabbix/certs/*

EXPOSE 80

USER zabbix

CMD ["/run.sh"]
