# 基础镜像
FROM a76yyyy/pycurl:latest

# 维护者信息
LABEL maintainer "a76yyyy <<PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>>"
LABEL org.opencontainers.image.source=https://github.com/qd-today/qd

# 添加 SSH 密钥
ADD ssh/qd_fetch /root/.ssh/id_rsa
ADD ssh/qd_fetch.pub /root/.ssh/id_rsa.pub

# 设置工作目录
WORKDIR /app

# QD && Pip install modules
RUN sed -i 's/mirrors.ustc.edu.cn/dl-cdn.alpinelinux.org/g' /etc/apk/repositories && \
    apk update && apk add --update --no-cache openssh-client && \
    chmod 600 /root/.ssh/id_rsa && \
    ssh-keyscan gitee.com > /root/.ssh/known_hosts && \
    let num=$RANDOM%100+10 && \
    sleep $num && \
    git clone --depth 1 <PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>:qd-today/qd.git /gitclone_tmp && \
    yes | cp -rf /gitclone_tmp/. /usr/src/app && \
    rm -rf /gitclone_tmp && \
    chmod +x /usr/src/app/update.sh && \
    ln -s /usr/src/app/update.sh /bin/update && \
    apk add --update --no-cache python3 py3-six \
    py3-markupsafe py3-pycryptodome py3-tornado py3-wrapt \
    py3-packaging py3-greenlet py3-urllib3 py3-cryptography \
    py3-aiosignal py3-async-timeout py3-attrs py3-frozenlist \
    py3-multidict py3-charset-normalizer py3-aiohttp \
    py3-typing-extensions py3-yarl py3-cffi && \
    [[ $(getconf LONG_BIT) = "32" ]] && \
    echo "Tips: 32-bit systems do not support ddddocr, so there is no need to install numpy and opencv-python" || \
    apk add --update --no-cache py3-numpy-dev py3-opencv py3-pillow && \
    apk add --no-cache --virtual .build_deps cmake make perl \
    autoconf g++ automake py3-pip py3-setuptools py3-wheel python3-dev \
        linux-headers libtool util-linux && \
    if [ -f /usr/src/app/requirements.txt ]; then \
        sed -i '/ddddocr/d' /usr/src/app/requirements.txt && \
        sed -i '/packaging/d' /usr/src/app/requirements.txt && \
        sed -i '/wrapt/d' /usr/src/app/requirements.txt && \
        sed -i '/pycryptodome/d' /usr/src/app/requirements.txt && \
        sed -i '/tornado/d' /usr/src/app/requirements.txt && \
        sed -i '/MarkupSafe/d' /usr/src/app/requirements.txt && \
        sed -i '/pillow/d' /usr/src/app/requirements.txt && \
        sed -i '/opencv/d' /usr/src/app/requirements.txt && \
        sed -i '/numpy/d' /usr/src/app/requirements.txt && \
        sed -i '/greenlet/d' /usr/src/app/requirements.txt && \
        sed -i '/urllib3/d' /usr/src/app/requirements.txt && \
        sed -i '/cryptography/d' /usr/src/app/requirements.txt && \
        sed -i '/aiosignal/d' /usr/src/app/requirements.txt && \
        sed -i '/async-timeout/d' /usr/src/app/requirements.txt && \
        sed -i '/attrs/d' /usr/src/app/requirements.txt && \
        sed -i '/frozenlist/d' /usr/src/app/requirements.txt && \
        sed -i '/multidict/d' /usr/src/app/requirements.txt && \
        sed -i '/charset-normalizer/d' /usr/src/app/requirements.txt && \
        sed -i '/aiohttp/d' /usr/src/app/requirements.txt && \
        sed -i '/typing-extensions/d' /usr/src/app/requirements.txt && \
        sed -i '/yarl/d' /usr/src/app/requirements.txt && \
        sed -i '/cffi/d' /usr/src/app/requirements.txt && \
        pip install --no-cache-dir -r requirements.txt --break-system-packages; \
    else \
        echo "No requirements.txt found, skipping pip install."; \
    fi && \
    apk del .build_deps && \
    sed -i 's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g' /etc/apk/repositories && \
    rm -rf /var/cache/apk/* && \
    rm -rf /usr/share/man/*

# 设置端口和时区
ENV PORT 80
EXPOSE $PORT/tcp
ENV TZ=CST-8

# 添加挂载点
VOLUME ["config"]

# 默认命令
CMD ["sh", "-c", "python .run.py"]