#!/bin/bash
#
# This is the TouchScale MariaDB installer script.
#

# Detect OS version 
dist=$(lsb_release -si)
ver=$(lsb_release -sr)
nam=$(lsb_release -sc)
tmpfile="/tmp/tmpfile"

# Detect hardware
cpu=$(pcs-cpu)

packages_precise="nginx php5-cli php5-curl php5-mysql php5-intl php5-fpm php5-xmlrpc php5-imagick php-apc curl git"
packages_bionic="composer nginx php-cli php-curl php-mysql php-fpm php-xml php-imagick git"

php_ini_precise="/etc/php5/cli/php.ini /etc/php5/fpm/php.ini"
php_ini_bionic="/etc/php/7.2/cli/php.ini /etc/php/7.2/fpm/php.ini"


CheckPermission() {
	sf_path="/var/www/touchscale"
	if [ -d $sf_path ]; then
	        echo " Web server already installed."
	        echo " You can launch webserver-update."
	        exit 1
	fi
	account=`whoami`
	if [ ${account} != "root" ]; then
		echo " ${account}, you are NOT the supervisor."
		echo " The root permission is required to run this installer."
		echo " You must execute this script with sudo"
		exit 1
	fi
	echo ' '
	echo This procedure requires a system update via an internet connection.
	read -r -p "Are you sure you want to continue? <y/N> " response
	if [ "$response" != "y" ] && [ "$response" != "Y" ]; then
		echo Exiting...
		echo ' '
		exit 0;
	fi
}


TestLinuxVersion() {

	echo "Detected $dist $nam $ver on $cpu CPU machine"
	
}

ConfigDefaultNginx() {
	def="/etc/nginx/sites-available/default"
	if [ ! -f $def.old ]; then
		cp -f $def $def.old
	fi
	echo '# TouchScale default site' > $def
	echo ' ' >> $def
	if [ "$nam" = "precise" ]; then
		echo 'upstream phpfcgi {' >> $def
		echo '    server 127.0.0.1:9000;' >> $def
		echo '}' >> $def
		echo 'server {' >> $def
		echo ' ' >> $def
		echo '    listen 80;' >> $def
		echo '    server_name touchscale;' >> $def
		echo '    root /var/www/touchscale/web;' >> $def
		echo ' ' >> $def
		echo '    error_log /var/log/nginx/touchscale-error.log;' >> $def
		echo '    access_log /var/log/nginx/touchscale-access.log;' >> $def
		echo ' ' >> $def
		echo '    location / {' >> $def
		echo '        try_files $uri @rewriteapp;' >> $def
		echo '    }' >> $def
		echo ' ' >> $def
		echo '   location @rewriteapp {' >> $def
		echo '        rewrite ^(.*)$ /app.php/$1 last;' >> $def
		echo '    }' >> $def
		echo ' ' >> $def
		echo '    location ~ ^/(app|app_dev|config)\.php(/|$) {' >> $def
		echo '        fastcgi_pass phpfcgi;' >> $def
		echo '        fastcgi_split_path_info ^(.+\.php)(/.*)$;' >> $def
		echo '        include fastcgi_params;' >> $def
		echo '        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name; ' >> $def
		echo '        fastcgi_param  HTTPS off;' >> $def
		echo '    }' >> $def
		echo '}' >> $def
	else
		echo 'server {' >> $def
		echo '    listen 80 default_server;' >> $def
		echo '    listen [::]:80 default_server;' >> $def
		echo '    root /var/www/touchscale/web;' >> $def
		echo '    index app.php;' >> $def
		echo '    server_name touchscale;' >> $def
		echo '    location / {' >> $def
		echo '        try_files $uri /app.php$is_args$args;' >> $def
		echo '    }' >> $def
		echo '    location ~ \.php$ {' >> $def
		echo '        include snippets/fastcgi-php.conf;' >> $def
		echo '        fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;' >> $def
		echo '    }' >> $def
		echo '}' >> $def
	fi
}

WebServerInstall() {
	cd
	echo Installing needed packages...
	packages=$packages_precise
#	symfony_version="2.3.6"
	symfony_version="2.3.*"
	symfony_packages="jms/translation-bundle:1.1.* liip/theme-bundle:1.3.* mike42/escpos-php"
	if [ "$nam" = "bionic" ]; then
		packages=$packages_bionic
		symfony_version="2.3.*"
		symfony_packages="jms/translation-bundle:1.1.* liip/theme-bundle:1.3.* mike42/escpos-php"
	fi
	apt-get -qq install $packages
	if [ $? -ne 0 ]; then
		echo "Packages install failed!. Test your internet connection."
		exit 1
	fi
	if [ "$nam" = "precise" ]; then
		echo Installing Composer...
		curl -sS https://getcomposer.org/installer | php
		if [ $? -ne 0 ]; then
			echo "Composer install failed!"
			exit 1
		fi
#		php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
#		php composer-setup.php --install-dir=/usr/local/bin --filename=composer --version=1.0.0
#		php -r "unlink('composer-setup.php');"
		mv composer.phar /usr/local/bin/composer
	fi
	if [ ! -d /var/www ]; then
		mkdir /var/www
	fi
	cd /var/www
	echo Installing Symfony Project...
	# optional: --no-progress
	composer create-project --prefer-dist symfony/framework-standard-edition /var/www/Symfony $symfony_version
	if [ $? -ne 0 ]; then
		echo "Symfony install failed!"
		exit 1
	fi
	# Only update on bionic. Out of memory with php 5.3.10 (Precise)  -- 10-01-2019 -- 
	if [ "$nam" = "bionic" ]; then
		cd /var/www/Symfony
		composer require $symfony_packages
		if [ $? -ne 0 ]; then
			echo "Symfony update failed!"
			exit 1
		fi
	fi
	cd /var/www
	ln -sf Symfony touchscale
	chown -R www-data:www-data Symfony touchscale
	cd /var/www/touchscale
	chmod -R 777 app/cache app/logs
	#setfacl -R -m u:www-data:rwX -m u:pcscale:rwX app/cache app/logs
	#setfacl -dR -m u:www-data:rwX -m u:pcscale:rwX app/cache app/logs
}

ConfigPhpIni() {
	php_inis=$php_ini_precise
	if [ "$nam" = "bionic" ]; then
		fpmconf="/etc/php/7.2/fpm/pool.d/www.conf"
		grep -q "listen = 127.0.0.1:9000" $fpmconf
		if [ $? -ne 0 ]; then
			sed -i '/listen = /run/php/php7.2-fpm.sock/c\\listen = 127.0.0.1:9000' $fpmconf
		fi		
		php_inis=$php_ini_bionic
	fi
	for file in $php_inis
	do
		modified="0"
		# test timezone...
		grep -q ";date.timezone" $file
		if [ $? -eq 0 ]; then
			sed 's/;date.timezone =/date.timezone = Europe\/Madrid/' $file > $tmpfile
			mv -f $tmpfile $file
			modified="1"
		fi
		# test short_open_tag...
		grep -q "short_open_tag = On" $file
		if [ $? -eq 0 ]; then
			sed 's/short_open_tag = On/short_open_tag = Off/' $file > $tmpfile
			mv -f $tmpfile $file
			modified="1"
		fi
		if [ "$modified" -ne "0" ]; then
			echo File $file correctly updated.
		fi
	done
}

UpdateSymfonyFiles() {
	webserver-update
}


CheckPermission
WebServerInstall
ConfigDefaultNginx
ConfigPhpIni
UpdateSymfonyFiles

