FROM ubuntu:22.04 AS build

ARG VERSION=26.1.0
ARG TARGETPLATFORM

RUN apt update && apt install -y wget unzip &&\
    apt-get clean &&\
    rm -rf /var/lib/apt/lists/* &&\
    rm -rf /var/tmp/* &&\
    rm -rf /tmp/* &&\
    ARCH=$(case "${TARGETPLATFORM}" in \
        "linux/amd64") echo "x86_64" ;; \
        "linux/arm64") echo "aarch64" ;; \
        *) echo "Unsupported TARGETPLATFORM: ${TARGETPLATFORM}" && exit 1 ;; \
    esac) &&\
    wget https://gitcode.com/ascend/mind-cluster/releases/download/v${VERSION}/Ascend-mindxdl-ascend-operator_${VERSION}_linux-${ARCH}.zip &&\
    unzip Ascend-mindxdl-ascend-operator_${VERSION}_linux-${ARCH}.zip -d /build

FROM ubuntu:22.04

RUN useradd -d /home/hwMindX -u 9000 -m -s /usr/sbin/nologin hwMindX &&\
    usermod root -s /usr/sbin/nologin

COPY --from=build /build/ascend-operator /usr/local/bin/ascend-operator
COPY --from=build /build/agreement.txt /usr/local/

RUN chown -R hwMindX:hwMindX /usr/local/bin/ascend-operator /usr/local/agreement.txt &&\
    chmod 500 /usr/local/bin/ascend-operator &&\
    chmod 440 /usr/local/agreement.txt &&\
    chmod 750 /home/hwMindX &&\
    echo 'umask 027' >> /etc/profile &&\
    echo 'source /etc/profile' >> /home/hwMindX/.bashrc

USER hwMindX

ENTRYPOINT ["/bin/bash", "-c", "cat /usr/local/agreement.txt; exec /bin/bash"]