FROM python:3.12-slim

ARG ANI_CLI=https://github.com/pystardust/ani-cli.git
ARG ANI_CLI_BRANCH=master
ARG ANI_CLI_WEB=https://gitea.coreplay.eu/Dymas/ani-cli-web.git

ENV DEBIAN_FRONTEND=noninteractive \
    ANI_CLI_REPO=${ANI_CLI} \
    ANI_CLI_BRANCH=${ANI_CLI_BRANCH} \
    ANI_CLI_WEB_REPO=${ANI_CLI_WEB} \
    ANI_CLI_BIN=ani-cli \
    ANI_CLI_DOWNLOAD_DIR=/downloads \
    ANI_CLI_WEB_HOST=0.0.0.0 \
    ANI_CLI_WEB_PORT=8421 \
    UPDATE_ON_START=false \
    USER_UID= \
    USER_GID=

RUN apt-get update \
    && apt-get install -y --no-install-recommends \
        aria2 \
        ca-certificates \
        curl \
        ffmpeg \
        fzf \
        git \
        grep \
        openssl \
        patch \
        sed \
        sudo \
        util-linux \
    && rm -rf /var/lib/apt/lists/*

RUN git clone --depth 1 --branch "${ANI_CLI_BRANCH}" "${ANI_CLI}" /tmp/ani-cli-src \
    && install -m 0755 /tmp/ani-cli-src/ani-cli /usr/local/bin/ani-cli \
    && ani_cli_raw_repo="$(printf '%s' "${ANI_CLI}" | sed -E 's#^https://github.com/##; s#\\.git$##')" \
    && case "${ANI_CLI}" in \
        https://github.com/*) \
            sed -i "s|https://raw.githubusercontent.com/pystardust/ani-cli/master/ani-cli|https://raw.githubusercontent.com/${ani_cli_raw_repo}/${ANI_CLI_BRANCH}/ani-cli|" /usr/local/bin/ani-cli ;; \
    esac \
    && rm -rf /tmp/ani-cli-src

RUN git clone --depth 1 "${ANI_CLI_WEB}" /app \
    && rm -rf /app/.git

COPY docker-entrypoint.sh /usr/local/bin/docker-entrypoint.sh

RUN chmod 0755 /usr/local/bin/docker-entrypoint.sh \
    && mkdir -p /downloads /app/.ani-cli-web

WORKDIR /app

EXPOSE 8421

ENTRYPOINT ["/usr/local/bin/docker-entrypoint.sh"]
