#!/usr/bin/env bash

echo -e "\nif [ -d /vagrant ] ; then\n    cd /vagrant\nfi" >> /home/vagrant/.profile

apt-get -q update
apt-get -qy install apt-transport-https

wget -qO - https://packages.sury.org/php/apt.gpg | apt-key add -
echo "deb https://packages.sury.org/php/ jessie main" > /etc/apt/sources.list.d/php.list

debconf-set-selections <<< "mysql-server mysql-server/root_password password root"
debconf-set-selections <<< "mysql-server mysql-server/root_password_again password root"

apt-get -q update
apt-get -qy install git mysql-server nginx-light php-apcu php-ssh2 php7.1-bcmath php7.1-cli php7.1-curl php7.1-fpm php7.1-intl php7.1-mbstring php7.1-mcrypt php7.1-mysql php7.1-xml php7.1-zip
apt-get -q clean

printf "server {\n\tlisten 80 default_server;\n\tlisten [::]:80 default_server;\n\n\tserver_name _;\n\n\troot /vagrant/web;\n\n\tlocation / {\n\t\ttry_files \$uri @app;\n\t}\n\n\tlocation @app {\n\t\tfastcgi_pass unix:/var/run/php/php7.1-fpm.sock;\n\t\tinclude fastcgi_params;\n\t\tfastcgi_param SCRIPT_FILENAME /vagrant/app/app.php;\n\t\tfastcgi_param SYMFONY_ENV dev;\n\t\tfastcgi_param SYMFONY_DEBUG 1;\n\t}\n}\n" > /etc/nginx/sites-available/default

service nginx restart

sed -e 's/^;date.timezone =.*/date.timezone = "Europe\/Brussels"/' < /usr/lib/php/7.1/php.ini-development > /etc/php/7.1/fpm/php.ini
sed -e 's/^;date.timezone =.*/date.timezone = "Europe\/Brussels"/' -e 's/^disable_functions =.*/;disable_functions =/' -e 's/^memory_limit =.*/memory_limit = -1/' < /usr/lib/php/7.1/php.ini-development > /etc/php/7.1/cli/php.ini

sed -i -e 's/^user = .*/user = vagrant/' -e 's/^group = .*/group = vagrant/' /etc/php/7.1/fpm/pool.d/www.conf

service php7.1-fpm restart

wget -qO - http://getcomposer.org/installer | php && mv composer.phar /usr/local/bin/composer
wget https://phar.phpunit.de/phpunit.phar && mv phpunit.phar /usr/local/bin/phpunit && chmod +x /usr/local/bin/phpunit

su vagrant -c "composer install -d=/vagrant --no-interaction --no-progress"
