FROM debian:11-slim

# Copy in requirements file
COPY apt-requirements.txt /tmp/apt-requirements.txt

# Set default repos to HTTPS
# RUN sed -i '/URIs: http:\/\/deb\.debian\.org\/debian/c\URIs: https:\/\/deb\.debian\.org\/debian' /etc/apt/sources.list.d/debian.sources

# Install package dependancies
RUN export DEBIAN_FRONTEND=noninteractive && \
    apt-get update -y && \
    xargs -a /tmp/apt-requirements.txt apt-get install -y --no-install-recommends

# Add MongoDB key and repo, install MongoDB
# RUN curl https://pgp.mongodb.com/server-6.0.asc | gpg --dearmor > /usr/share/keyrings/mongodb-archive-keyring.gpg && \
#     echo "deb [ signed-by=/usr/share/keyrings/mongodb-archive-keyring.gpg] https://repo.mongodb.org/apt/debian bullseye/mongodb-org/6.0 main" | tee /etc/apt/sources.list.d/mongodb.list && \
#     apt-get update -y && \
#     apt-get install -y --no-install-recommends mongodb-org

# Add Unifi key and repo, install Unifi
# RUN curl https://dl.ui.com/unifi/unifi-repo.gpg | gpg --dearmor > /usr/share/keyrings/ubiquiti-archive-keyring.gpg && \
#     echo 'deb [signed-by=/usr/share/keyrings/ubiquiti-archive-keyring.gpg] https://www.ui.com/downloads/unifi/debian stable ubiquiti' | tee /etc/apt/sources.list.d/ubiquiti.list && \
#     apt-get update -y && \
#     apt-get install -y --no-install-recommends unifi

# Get latest version of Unifi and download deb file
RUN UNIFI_VERSION=$(curl -sX GET http://dl-origin.ubnt.com/unifi/debian/dists/stable/ubiquiti/binary-amd64/Packages \
        | grep -A 7 -m 1 'Package: unifi' \
        | awk -F ': ' '/Version/{print $2;exit}' \
        | awk -F '-' '{print $1}') && \
    echo "Unifi version: $UNIFI_VERSION" && \
    curl -o /tmp/unifi.deb -L https://dl.ui.com/unifi/$UNIFI_VERSION/unifi_sysvinit_all.deb

# Unpack the unifi deb file, remove mongodb dependancy, then repack and install
RUN mkdir -p /tmp/unpack && \
    dpkg-deb -R /tmp/unifi.deb /tmp/unpack && \
    sed -i '/^ mongodb-server.*),/d' /tmp/unpack/DEBIAN/control && \
    echo "Updated control file:" && \
    cat /tmp/unpack/DEBIAN/control && \
    dpkg-deb -b /tmp/unpack /tmp/unifi-nomongo.deb && \
    dpkg -i /tmp/unifi-nomongo.deb

# Copy in system.properties file
COPY system.properties /var/lib/unifi/system.properties

# Tweak the unifi user, chown files, clean up
RUN usermod -s /bin/bash unifi && \
    chown -R unifi:unifi /var/lib/unifi && \
    chmod 600 /var/lib/unifi/system.properties && \
    apt-get clean && \
    rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

# Add unifi user, create app directores and chown to unifi user
# RUN useradd unifi-svc \
#         --uid 1000 \
#         --user-group \
#         --groups unifi,unifi-svc \
#         --create-home \
#         --shell /bin/bash && \
    # mkdir -p /unifi/data && \
    # mkdir -p /unifi/logs && \
    # chown -R unifi:unifi /unifi

VOLUME /var/lib/unifi
VOLUME /var/log/unifi

# Expose ports
EXPOSE 3478 8080 8443 8843 8880

# Change to unifi user
USER unifi

# Set working directory
WORKDIR /var/lib/unifi

# Set entrypoint
CMD ["/usr/bin/java", "-Dlog4j2.formatMsgNoLookups=true", "-jar", "/usr/lib/unifi/lib/ace.jar", "start"]


