#   Copyright 2022-2023 Nosial - All Rights Reserved.
#
#   Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
#   documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
#   rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
#   permit persons to whom the Software is furnished to do so, subject to the following conditions:
#
#   The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
#   Software.
#
#   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
#   WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
#   COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
#   OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
#   ncc in Docker
#
#   This image is intended to be used as a base for projects using ncc.
#

# Build-time args
ARG PHP_VERSION=8.2

# Builder stage: downloads necessary files and serves them on a silver platter.
FROM php:${PHP_VERSION}-fpm AS builder
ENV GENERIC_BUILD_PATH=/tmp/ncc_build
WORKDIR /tmp

# Install some stuff the default image doesn't come with
RUN apt-get update -yqq && \
    apt-get install git libpq-dev libzip-dev zip make wget gnupg -yqq

# Download phive and install phab
RUN wget -O phive.phar https://phar.io/releases/phive.phar && \
    wget -O phive.phar.asc https://phar.io/releases/phive.phar.asc && \
    gpg --keyserver hkps://keys.openpgp.org --recv-keys 0x9D8A98B29B2D5D79 && \
    gpg --verify phive.phar.asc phive.phar && \
    rm phive.phar.asc && chmod +x phive.phar && \
    ./phive.phar install phpab --global --trust-gpg-keys 0x2A8299CE842DD38C

# Copy the local repository to the image
COPY . /tmp/ncc

# Build ncc
RUN cd /tmp/ncc && make redist


# Main stage: Copies build files and installs all dependencies
FROM php:${PHP_VERSION}-fpm AS production

# OSI labels
LABEL maintainer="Netkas <netkas@nosial.net>"
LABEL description="ncc's official Docker image"

ADD https://github.com/mlocati/docker-php-extension-installer/releases/latest/download/install-php-extensions /usr/local/bin/

# Copy downloaded files
COPY --from=builder /tmp/ncc_build/. .

# Install some stuff the default image doesn't come with
RUN apt-get update -yqq && \
    apt-get install -yqq git libpq-dev libzip-dev zip make wget gnupg gcc -yqq

RUN chmod +x /usr/local/bin/install-php-extensions && sync && \
	install-php-extensions zip xsl

# Install ncc
RUN php INSTALL --auto

# Finalize image
RUN mkdir /app
WORKDIR /app