#!/bin/bash

# PHP {{ php_ver }} (via PPA)
sudo add-apt-repository ppa:ondrej/php -y
sudo apt update
sudo apt install -y php{{ php_ver }}-bz2 php{{ php_ver }}-cli php{{ php_ver }}-common php{{ php_ver }}-curl php{{ php_ver }}-fpm \
    php{{ php_ver }}-gd php{{ php_ver }}-intl php{{ php_ver }}-mbstring php{{ php_ver }}-mysql php{{ php_ver }}-opcache \
    php{{ php_ver }}-readline php{{ php_ver }}-xml php{{ php_ver }}-xmlrpc php{{ php_ver }}-xsl php{{ php_ver }}-zip php{{ php_ver }}-redis
sudo sed -i "s/;date.timezone =.*/date.timezone = {{ timezone | replace("/", "\/") }}/" /etc/php/{{ php_ver }}/cli/php.ini
sudo sed -i "s/;date.timezone =.*/date.timezone = {{ timezone | replace("/", "\/") }}/" /etc/php/{{ php_ver }}/fpm/php.ini
sudo sed -i "s/; max_input_vars =.*/max_input_vars = 10000/" /etc/php/{{ php_ver }}/fpm/php.ini
sudo sed -i "s/upload_max_filesize = .*/upload_max_filesize = 10M/" /etc/php/{{ php_ver }}/fpm/php.ini
sudo sed -i "s/post_max_size = .*/post_max_size = 12M/" /etc/php/{{ php_ver }}/fpm/php.ini
sudo sed -i "s/max_execution_time = .*/max_execution_time = 60/" /etc/php/{{ php_ver }}/fpm/php.ini
sudo sed -i "s/;cgi.fix_pathinfo=1/cgi.fix_pathinfo=0/" /etc/php/{{ php_ver }}/fpm/php.ini
sudo sed -i "s/;opcache.enable=.*/opcache.enable=1/" /etc/php/{{ php_ver }}/fpm/php.ini
sudo sed -i "s/;opcache.use_cwd=.*/opcache.use_cwd=1/" /etc/php/{{ php_ver }}/fpm/php.ini
sudo sed -i "s/;opcache.validate_timestamps=.*/opcache.validate_timestamps=1/" /etc/php/{{ php_ver }}/fpm/php.ini
sudo sed -i "s/;opcache.revalidate_freq=.*/;opcache.revalidate_freq=20/" /etc/php/{{ php_ver }}/fpm/php.ini
sudo systemctl enable php{{ php_ver }}-fpm.service
sudo systemctl restart php{{ php_ver }}-fpm.service

curl https://getcomposer.org/installer > composer-setup.php && php composer-setup.php && sudo mv composer.phar /usr/local/bin/composer && sudo chmod +x /usr/local/bin/composer && rm composer-setup.php
