FROM bugzilla/bugzilla-perl-slim:20240410.1 AS builder

RUN apt-get update \
 && apt-get dist-upgrade -y \
 && apt-get install -y \
    apt-file \
    build-essential \
    cmake \
    curl \
    git \
    libcairo-dev \
    libexpat-dev \
    libgd-dev \
    libssl-dev \
    openssl \
    zlib1g-dev \
    unzip \
    wget

# The Perl image is based on Debian, which doesn't have MySQL 8, and the
# current DBD::mysql requires MySQL 8 libraries to build, so we have
# to get the client libraries from mysql.com
RUN gpg --homedir /tmp --no-default-keyring --keyring /usr/share/keyrings/mysql-8.0.gpg --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 5072E1F5 \
 && gpg --homedir /tmp --no-default-keyring --keyring /usr/share/keyrings/mysql-8.0.gpg --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 3A79BD29 \
 && gpg --homedir /tmp --no-default-keyring --keyring /usr/share/keyrings/mysql-8.0.gpg --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys B7B3B788A8D3785C \
 && echo 'deb [arch=amd64 signed-by=/usr/share/keyrings/mysql-8.0.gpg] http://repo.mysql.com/apt/debian/ bookworm mysql-8.0' > /etc/apt/sources.list.d/mysql-8.0.list
RUN apt-get update \
 && apt-get install -y libmysqlclient-dev

WORKDIR /app

COPY Makefile.PL Bugzilla.pm gen-cpanfile.pl /app/
COPY extensions/ /app/extensions/

RUN cpanm --notest --quiet App::cpm Module::CPANfile Carton::Snapshot

RUN perl Makefile.PL
RUN make cpanfile GEN_CPANFILE_ARGS="-D mysql"

RUN carton install

RUN apt-file update
RUN find local -name '*.so' -exec ldd {} \; \
    | egrep -v 'not.found|not.a.dynamic.executable' \
    | awk '$3 {print $3}' \
    | sort -u \
    | xargs -IFILE apt-file search -l FILE \
    | sort -u > PACKAGES

FROM bugzilla/bugzilla-perl-slim:20240410.1

ENV DEBIAN_FRONTEND noninteractive

COPY --from=builder /app/local /app/local
COPY --from=builder /app/PACKAGES /app/PACKAGES

RUN apt-get update \
    && apt-get install -y \
       gnupg

# The Perl image is based on Debian, which doesn't have MySQL 8, and the
# current DBD::mysql requires MySQL 8 libraries to build, so we have
# to get the client libraries from mysql.com
RUN gpg --homedir /tmp --no-default-keyring --keyring /usr/share/keyrings/mysql-8.0.gpg --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 5072E1F5 \
 && gpg --homedir /tmp --no-default-keyring --keyring /usr/share/keyrings/mysql-8.0.gpg --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 3A79BD29 \
 && gpg --homedir /tmp --no-default-keyring --keyring /usr/share/keyrings/mysql-8.0.gpg --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys B7B3B788A8D3785C \
 && echo 'deb [arch=amd64 signed-by=/usr/share/keyrings/mysql-8.0.gpg] http://repo.mysql.com/apt/debian/ bookworm mysql-8.0' > /etc/apt/sources.list.d/mysql-8.0.list

RUN apt-get update \
    && apt-get install -y \
       libmysqlclient21 \
       $(cat /app/PACKAGES) \
    && rm -rf /var/cache/apt/* /var/lib/apt/lists/*

