# Stage 1: Build
FROM --platform=$TARGETPLATFORM ubuntu:22.04 AS builder

ARG TARGETARCH
ENV DEBIAN_FRONTEND=noninteractive

RUN apt-get update && apt-get install -y --no-install-recommends \
        wget \
        ca-certificates \
        && rm -rf /var/lib/apt/lists/*

# Download and extract appropriate 3proxy package based on architecture.
RUN set -eux; \
        if [ "$TARGETARCH" = "amd64" ]; then \
            pkg_url="https://github.com/3proxy/3proxy/releases/download/0.9.4/3proxy-0.9.4.x86_64.deb"; \
        elif [ "$TARGETARCH" = "arm64" ]; then \
            pkg_url="https://github.com/3proxy/3proxy/releases/download/0.9.4/3proxy-0.9.4.aarch64.deb"; \
        elif [ "$TARGETARCH" = "arm" ]; then \
            pkg_url="https://github.com/3proxy/3proxy/releases/download/0.9.4/3proxy-0.9.4.armv7l.deb"; \
        else \
            echo "Unsupported architecture: $TARGETARCH"; exit 1; \
        fi; \
        wget -O /tmp/3proxy.deb "$pkg_url"; \
        dpkg-deb -x /tmp/3proxy.deb /tmp/3proxy-root; \
        test -x /tmp/3proxy-root/bin/3proxy; \
        install -m 0755 /tmp/3proxy-root/bin/3proxy /usr/local/bin/3proxy; \
        mkdir -p /usr/local/3proxy; \
        cp -a /tmp/3proxy-root/usr/local/3proxy/. /usr/local/3proxy/ || true

# Stage 1b: Build Yggdrasil tooling for target arch
FROM --platform=$BUILDPLATFORM golang:1.22-bookworm AS builder_yggdrasil

ARG TARGETARCH
ARG YGGDRASIL_VERSION=v0.5.12

RUN git clone https://github.com/yggdrasil-network/yggdrasil-go.git /src/yggdrasil-go \
    && cd /src/yggdrasil-go \
    && git checkout ${YGGDRASIL_VERSION}

RUN set -eux; \
    case "${TARGETARCH}" in \
      amd64) goarch=amd64 ;; \
      arm64) goarch=arm64 ;; \
      arm) goarch=arm ;; \
      *) echo "Unsupported architecture: ${TARGETARCH}"; exit 1 ;; \
    esac; \
        cd /src/yggdrasil-go; \
    mkdir -p /out; \
    CGO_ENABLED=0 GOOS=linux GOARCH=${goarch} go build -o /out/yggdrasil ./cmd/yggdrasil; \
    CGO_ENABLED=0 GOOS=linux GOARCH=${goarch} go build -o /out/yggdrasilctl ./cmd/yggdrasilctl; \
    CGO_ENABLED=0 GOOS=linux GOARCH=${goarch} go build -o /out/genkeys ./cmd/genkeys

# Stage 2: Runtime
FROM ubuntu:22.04

RUN apt-get update && apt-get install -y --no-install-recommends \
    ca-certificates \
    iproute2 \
    iputils-ping \
    python3 \
    && rm -rf /var/lib/apt/lists/*

# Copy binaries using the correct absolute paths from Stage 1
COPY --from=builder /usr/local/bin/3proxy /usr/bin/3proxy
COPY --from=builder /usr/local/3proxy /usr/local/3proxy
COPY --from=builder_yggdrasil /out/yggdrasil /usr/bin/yggdrasil
COPY --from=builder_yggdrasil /out/yggdrasilctl /usr/bin/yggdrasilctl
COPY --from=builder_yggdrasil /out/genkeys /usr/bin/genkeys

# Copy local runtime helpers/config
COPY ./entrypoint.sh /entrypoint.sh
COPY ./socks_router.py /usr/local/bin/socks_router.py
COPY ./yggdrasil.conf /etc/yggdrasil/yggdrasil.conf

# Setup config directories
RUN mkdir -p /etc/3proxy /etc/yggdrasil \
    && chmod +x /usr/local/bin/socks_router.py \
    && chmod +x /entrypoint.sh

# Ensure these files exist in your local directory where you run 'docker build'
#COPY first-instanse.cfg /etc/3proxy/
#COPY second-instanse.cfg /etc/3proxy/

EXPOSE 1080 3128 8161

# Start Yggdrasil first, then run plain 3proxy instances from entrypoint.
ENTRYPOINT ["/entrypoint.sh"]
CMD []
