#!/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
grep -q "Intel(R) Atom(TM) CPU N2600   @ 1.60GHz" /proc/cpuinfo
if [ $? -eq 0 ]; then
	cpu="N2600"
else
	grep -q "Genuine Intel(R) CPU N270   @ 1.60GHz" /proc/cpuinfo
	if [ $? -eq 0 ]; then
		cpu="N270"
	else
		grep -q "Intel(R) Atom(TM) CPU  330   @ 1.60GHz" /proc/cpuinfo
		if [ $? -eq 0 ]; then
			cpu="330"
		else
			cpu="Generic"
		fi
	fi
fi


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"

	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
}

WebServerInstall() {
	cd
	echo Installing needed packages...
	apt-get -qq install nginx php5-cli php5-mysql php5-intl php5-fpm php-apc curl git
	if [ $? -ne 0 ]; then
		echo "Packages install failed!. Test your internet connection."
		exit 1
	fi
	echo Installing Composer...
	curl -sS https://getcomposer.org/installer | php
	if [ $? -ne 0 ]; then
		echo "Composer install failed!"
		exit 1
	fi
	mv composer.phar /usr/local/bin/composer
	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 2.3.6
	if [ $? -ne 0 ]; then
		echo "Symfony install failed!"
		exit 1
	fi
	cd /var/www/Symfony
	composer require jms/translation-bundle:1.1.* liip/theme-bundle:dev-master
	if [ $? -ne 0 ]; then
		echo "Symfony update failed!"
		exit 1
	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() {
	for file in "/etc/php5/cli/php.ini" "/etc/php5/fpm/php.ini"
	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

