#!/bin/bash
# Setup a php:8.2.1-fpm docker image

# We need to install dependencies only for Docker
[[ ! -e /.dockerenv ]] && exit 0

# Quit on error
set -xe

# Install packages
apt-get update
apt-get install -yqq --fix-missing apt-utils gnupg git build-essential openssh-client zip

apt-key adv --keyserver keyserver.ubuntu.com --recv-keys E9C74FEEA2098A6E
echo "deb https://packages.dotdeb.org jessie all" >> /etc/apt/sources.list
echo "deb-src https://packages.dotdeb.org jessie all" >> /etc/apt/sources.list

apt-get update

apt-get install -yqq --fix-missing --no-install-recommends unzip zlib1g-dev libicu-dev g++ \
                               libzip-dev libpng-dev libtidy-dev libssl-dev \
                               libxslt-dev libxrender-dev libxrender1 libxt6 libxtst6 openssh-server \
                               xorg iputils-ping wget gdebi ca-certificates wget \
                               fontconfig acl procps libmagickwand-dev imagemagick

apt-get clean
rm -r /var/lib/apt/lists/*

# Configure docker
#docker-php-ext-configure intl
#docker-php-ext-install intl opcache pdo_mysql exif bcmath calendar gd tidy
#docker-php-ext-enable libxslt-dev
#docker-php-ext-enable intl
#docker-php-ext-enable exif
#docker-php-ext-enable tidy
#docker-php-ext-install zip
#docker-php-ext-install xsl
#pecl install apcu-5.1.21

# Install XDebug (required for coverage)
pecl install xdebug
docker-php-ext-enable xdebug

# Run ssh-agent and add private key (/!\ Attention: la configuration du SSH est indispensable pour pouvoir cloner notre repo foselastica)
eval $(ssh-agent -s)
mkdir -p ~/.ssh
chmod 700 ~/.ssh
echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config
ssh-add <(echo "$SSH_PRIVATE_KEY")
git config --global user.email "exploitation@opentalent.fr"
git config --global user.name "git"

# Install composer
curl -sS https://composer.github.io/installer.sig > installer.sig
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('SHA384', 'composer-setup.php') === file_get_contents('installer.sig')) { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php
php -r "unlink('composer-setup.php'); unlink('installer.sig');"

# Install modules
php composer.phar install --no-interaction --ignore-platform-reqs
