ARG PHP_SECURITY_CHECKER_VERSION=1.0.0
ARG JQ_VERSION=1.6

RUN apt-get update && \
    DEBIAN_FRONTEND=noninteractive apt-get install -y \
        wget tar curl ca-certificates \
        openssl bash git unzip \
        php-cli php-dom php-mbstring php-ctype php-xml php-iconv
        
COPY add-letsencrypt-ca.sh /root/add-letsencrypt-ca.sh

RUN bash /root/add-letsencrypt-ca.sh \
    && rm -f /root/add-letsencrypt-ca.sh

RUN wget -O /usr/local/bin/jq https://github.com/stedolan/jq/releases/download/jq-${JQ_VERSION}/jq-linux64 \
    && chmod +x /usr/local/bin/jq

# Install local-php-security-checker
RUN wget -O /usr/local/bin/local-php-security-checker https://github.com/fabpot/local-php-security-checker/releases/download/v${PHP_SECURITY_CHECKER_VERSION}/local-php-security-checker_${PHP_SECURITY_CHECKER_VERSION}_linux_amd64 \
    && chmod +x /usr/local/bin/local-php-security-checker

# Install junit2md
RUN junit2md_download_url=$(curl "https://forge.cadoles.com/api/v1/repos/Cadoles/junit2md/releases" -H "accept:application/json" | jq -r 'sort_by(.published_at) | reverse | .[0] | .assets[] | select(.name == "junit2md-linux-amd64.tar.gz") | .browser_download_url') \
    && wget -O junit2md-linux-amd64.tar.gz "$junit2md_download_url" \
    && tar -xzf junit2md-linux-amd64.tar.gz \
    && cp junit2md-linux-amd64/junit2md /usr/local/bin/junit2md

# Install composer
RUN wget https://raw.githubusercontent.com/composer/getcomposer.org/76a7060ccb93902cd7576b67264ad91c8a2700e2/web/installer -O - -q | php -- --force --install-dir /usr/local/bin --filename composer \
    && chmod +x /usr/local/bin/composer

# Install php-cs-fixer
RUN mkdir --parents /tools/php-cs-fixer \
    && composer require --working-dir=/tools/php-cs-fixer friendsofphp/php-cs-fixer \
    && ln -s /tools/php-cs-fixer/vendor/bin/php-cs-fixer /usr/local/bin/php-cs-fixer

# Install php-stan 
RUN mkdir --parents /tools/phpstan \
    && composer require --working-dir=/tools/phpstan phpstan/phpstan \
    && ln -s /tools/phpstan/vendor/bin/phpstan /usr/local/bin/phpstan \
    && composer require --working-dir=/tools/phpstan phpstan/phpstan-symfony \
    && composer require --working-dir=/tools/phpstan phpstan/phpstan-doctrine

# Install Symfony
RUN curl -1sLf 'https://dl.cloudsmith.io/public/symfony/stable/setup.deb.sh' | bash \
    && apt update \
    && DEBIAN_FRONTEND=noninteractive apt-get install -y symfony-cli