# Use Debian as the base image
FROM debian:latest

# Set environment variables for non-interactive installation
ENV DEBIAN_FRONTEND=noninteractive

# Update and install required dependencies
RUN apt-get update && \
    apt-get install -y curl && \
    apt-get clean && \
    rm -rf /var/lib/apt/lists/*

# Set the working directory
WORKDIR /usr/local/bin

# Detect architecture and download the correct binary
RUN ARCH=$(uname -m) && \
    if [ "$ARCH" = "x86_64" ]; then \
        curl -o dohzel-proxy.linux-x64-latest.bin https://download.hafnova.com/dohzel-proxy/dohzel-proxy.linux-x64-latest.bin; \
    elif [ "$ARCH" = "aarch64" ]; then \
        curl -o dohzel-proxy.linux-arm64-latest.bin https://download.hafnova.com/dohzel-proxy/dohzel-proxy.linux-arm64-latest.bin; \
    else \
        echo "Unsupported architecture: $ARCH" && exit 1; \
    fi && \
    chmod +x dohzel-proxy.linux-*-latest.bin

# Create a volume for the home directory
VOLUME /root

# Set the home directory as the working directory
WORKDIR /root

# Expose port 53 (DNS)
EXPOSE 53/udp
EXPOSE 53/tcp

# Entry point script handles initialization and enrollment
COPY entrypoint.sh /usr/local/bin/entrypoint.sh
COPY .env /root/.env
RUN chmod +x /usr/local/bin/entrypoint.sh

# Set the entrypoint to the script
ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]
