FROM ubuntu:noble AS build

# set version label
ARG BUILD_DATE
ARG VERSION
ARG CALIBREWEB_RELEASE
ARG CALIBRE_INSTALLER_SOURCE_CODE_URL=https://raw.githubusercontent.com/kovidgoyal/calibre/master/setup/linux-installer.py
ARG NETIFACES_PLUS_RELEASE
ARG KEPUBIFY_RELEASE
# Configurer debconf pour utiliser un frontend non interactif
ARG DEBIAN_FRONTEND=noninteractive
ARG DEBCONF_NONINTERACTIVE_SEEN=true


LABEL build_version="petite-maison-orange.fr version:- ${VERSION} Build-date:- ${BUILD_DATE}"
LABEL maintainer="Eric Coissac <eric@petite-maison-orange.fr>"

# RUN cp /etc/apt/sources.list.d/debian.sources \
#        /etc/apt/sources.list.d/debian.sources.save \
#     && sed -e "s/contrib main/contrib main non-free-firmware/g" \
#               /etc/apt/sources.list.d/debian.sources.save > /etc/apt/sources.list.d/debian.sources

RUN \
  echo "**** Installing required packages ****" 1>&2 \
  && apt-get update \
  && apt-get install -y --no-install-recommends \
    build-essential \
    ca-certificates \
    libldap2-dev \
    libsasl2-dev \
    libxml2-dev \
    libxslt1-dev \
    libzmq3-dev \
    libmagic-dev \
    curl \
    file \
    git \
    google-mock \
    imagemagick \
    jq \
    calibre \
    python3-dev \
    python3-pip \
    python3-setuptools \
    python3-venv \
    tzdata \
    unrar \
    wget \
    xz-utils \
    bash
  RUN echo "**** create tmp dir ****" 1>&2 \
  && mkdir -p /tmp

# Télécharger et installer Go
# Définir la version de Go à installer
ENV GO_VERSION=1.24.1
ENV GO_TARBALL=go${GO_VERSION}.linux-amd64.tar.gz

RUN echo "***Download and install ${GO_TARBALL}"
RUN curl -OL https://go.dev/dl/${GO_TARBALL} && \
    tar -C /usr/local -xzf ${GO_TARBALL} && \
    rm ${GO_TARBALL}

# Définir le PATH pour Go
ENV PATH="/usr/local/go/bin:${PATH}"

RUN echo "***install kepubify from source" 1>&2 \
    && go install github.com/pgaskin/kepubify@latest 


RUN echo "**** install python3 ****" 1>&2
ENV PYTHONUNBUFFERED=1
RUN python3 -m venv /app \
     && . /app/bin/activate \
     && pip3 install --no-cache --upgrade pip \
     && pip3 install --no-cache --upgrade setuptools \
     && pip3 install --no-cache --upgrade poetry \
     && pip3 install --no-cache cryptography \
     && pip3 install --no-cache ldap3 \
     && pip3 install --no-cache flask-ldap3-login \
     && pip3 install --no-cache --upgrade tzdata \
     && pip3 install --no-cache --upgrade netifaces-plus \
     && pip3 install --no-cache python-magic 

RUN echo "**** download calibre-web ****" 1>&2 \
     && if [ -z ${CALIBREWEB_RELEASE} ]; then \
        CALIBREWEB_RELEASE=$(curl -sX GET "https://api.github.com/repos/janeczku/calibre-web/releases/latest" \
        | awk '/tag_name/{print $4;exit}' FS='[""]'); \
      fi \
     && echo "    ---> install version ${CALIBREWEB_RELEASE}"  1>&2 \
     && git clone --depth 1 --branch ${CALIBREWEB_RELEASE} https://github.com/janeczku/calibre-web.git /app/calibreweb 

COPY patches /tmp/patches
RUN echo "**** apply calibre-web patches ****" 1>&2 \
    && cd /app/calibreweb \
    && for p in /tmp/patches/calibre-web/*.patch ; do \
           echo Applying patch : $p ; \
           patch --verbose -p0 < ${p} || \
           cat optional-requirements.txt.rej; \
       done

RUN echo "**** build & install calibre-web  ****" 1>&2 \
     && . /app/bin/activate \
     && cd /app/calibreweb \
     && pip3 install --no-cache simpleldap \
     && cat optional-requirements.txt \
     && pip3 install --only-binary :all: greenlet \
     && pip3 install --only-binary :all: Flask-SQLAlchemy \
     && pip3 install --no-cache \
                     -r requirements.txt \
                     -r optional-requirements.txt 

RUN echo "**** copy kepubify to app folder  ****" 1>&2 \
    && mv /root/go/bin/kepubify /app/bin



RUN yes | /app/bin/pip3 uninstall google-api-python-client
RUN yes | /app/bin/pip3 uninstall httplib2
RUN yes | /app/bin/pip3 uninstall oauth2client
RUN yes | /app/bin/pip3 uninstall uritemplate
RUN yes | /app/bin/pip3 uninstall PyDrive2
RUN yes | /app/bin/pip3 uninstall PyYAML
RUN yes | /app/bin/pip3 uninstall rsa
    
ENV TZ=Europe/Paris
RUN cp /usr/share/zoneinfo/$TZ /etc/localtime 

FROM ubuntu:noble AS production


# ports and volumes
EXPOSE 8083
VOLUME /config
VOLUME /books

ENV PUID=1000
ENV PGID=1000
ENV TZ=Europe/Paris

RUN echo "**** install production packages ****"  1>&2 
RUN apt-get update 
RUN apt-get install --no-install-recommends -y \
    bash \
    libldap2 \
    libsasl2-2 \
    libxml2 \
    libxslt1.1 \
    libmagic1 \
    ca-certificates \
    imagemagick \
    calibre \
    procps \
    python3 \
    xdg-utils \
    tzdata \
    vim 

COPY --from=build /app /app

RUN apt-get clean
RUN rm -rf /var/lib/apt/lists/* \
           /tmp/* \
           /var/tmp/* \
           /usr/share/doc/* 

RUN   echo "**** Setting timezone ****" 2>&1 \
      && echo $TZ > /etc/timezone
COPY --from=build /etc/timezone /etc/timezone 
          
           
RUN mkdir -p /config \ 
    && chown -R ${PUID}:${PGID} /config
           
WORKDIR /app
USER ${PUID}:${PGID}

CMD ["/app/bin/python3", "/app/calibreweb/cps.py", "-p", "/config/app.db", "-g", "/config/gdrive.db", "-o", "/config/calibre-web.log"]
