<commit_before>#!/bin/bash
currentUbuntuVersionSupported="14.04"

############################################################
# Functions
############################################################

# installer nickName, actualName
# example: installer best-text-editor nano
function installer
{
	if [ -z "`which "$1" 2>/dev/null`" ]
	then
		executable=$1
		shift
		while [ -n "$1" ]
		do
			DEBIAN_FRONTEND=noninteractive apt-get -q -y install "$1"
			apt-get clean
			printInfo "$executable installed"
			shift
		done
	else
		printWarn "$2 already installed"
	fi
}

# uninstaller nickName, actualName
# example: uninstaller /usr/sbin/apache2 'apache2*'
function uninstaller
{
	if [ -n "`which "$1" 2>/dev/null`" ]
	then
		DEBIAN_FRONTEND=noninteractive apt-get -q -y remove --purge "$2"
		apt-get clean
		printInfo "$2 uninstalled"
	else
		printWarn "$2 is not installed"
	fi
}

# exits script if something goes wrong
function die
{
	echo "ERROR: $1" > /dev/null 1>&2
	exit 1
}

# Green Text
function printInfo {
	echo -n -e '\e[32m'
	echo -n $1
	echo -e '\e[0m'
}

# Yellow Text
function printWarn
{
	echo -n -e '\e[93m'
	echo -n $1
	echo -e '\e[0m'
}

# Red Text
function printError
{
	echo -n -e '\e[91m'
	echo -n $1
	echo -e '\e[0m'
}

# Do some sanity checking (root and Ubuntu version)
function checkSanity
{
	if [ $(/usr/bin/id -u) != "0" ]
	then
		die 'Must be run by root user'
	fi
	. /etc/lsb-release
	version=$DISTRIB_RELEASE
	if [ "$version" != "$currentUbuntuVersionSupported" ]
	then
		die "Distribution is not supported"
	fi
}

############################################################
# Initial Setup
############################################################

function baseInstaller
{
	installer nano nano # text editor
	installer vim vim # text editor
	installer iftop iftop # show network usage
	installer nload nload # visualize network usage
	installer htop htop # task manager
	installer mc mc # file explorer
	installer unzip unzip
	installer zip zip
	installer curl curl
	installer screen screen
	installer gt5 gt5 # visual disk usage
	installer nslookup dnsutils # dns tools
	ppaGit # verison control
}

function removeUneededPackages
{
	# Apache
	service apache2 stop
	apt-get remove apache2* -y
	apt-get autoremove -y
}

function setTimezone
{
	dpkg-reconfigure tzdata
}

function ppaSupport
{
	installer python-software-properties python-software-properties
	installer software-properties-common software-properties-common
}

function hardenSysctl
{
	source ~/www-ubuntu/www-ubuntu.conf
	if [ "$hasHardenSysctlRun" = false ]
	then
		cat sysctl-append.conf >> /etc/sysctl.conf
		sysctl -p > /dev/null
		sed -i 's/hasHardenSysctlRun.*/hasHardenSysctlRun=true/' www-ubuntu.conf
	else
		printWarn "hardenSysctl has already been run on this system, function skipped."
	fi
}

function ppaGit
{
	add-apt-repository ppa:git-core/ppa -y
	apt-get update
	apt-get upgrade -y # git was installed to pull in www-ubuntu
	printInfo "git was upgraded to the ppa verison"
}

function scriptAlias
{
	source ~/www-ubuntu/www-ubuntu.conf
	if [ "$hasAnAliasBeenAdded" = false ]
	then
		echo "alias www-ubuntu='/root/www-ubuntu/www-ubuntu.sh'" >> /root/.bashrc
		cd ~/www-ubuntu
		sed -i 's/hasAnAliasBeenAdded.*/hasAnAliasBeenAdded=true/' www-ubuntu.conf
	fi
}

function baseSetup
{
	setTimezone
	removeUneededPackages
	runUpdater
	hardenSysctl
	ppaSupport
	baseInstaller
	runCleaner
	scriptAlias
}

############################################################
# Main options
############################################################

# Nginx/PHP
function installWWW
{
	source ~/www-ubuntu/www-ubuntu.conf
	if [ "$hasInstallWWW" = true ]
	then
		die "installWWW has already been run, if run again conflicts will be created"
	fi
	NGINX="1.6.2"
	PAGESPEED="192.168.3.11"
	PSOL="192.168.3.11"
	WWWUSER="deploy"
	# Create user
	adduser deploy
	# PHP
	# https://www.digitalocean.com/community/tutorials/how-to-install-linux-nginx-mysql-php-lemp-stack-on-ubuntu-14-04
	installer php5-fpm php5-fpm
	installer php5-mysql php5-mysql
	installer php-apc php-apc
	sed -i 's/user = www-data/user = $WWWUSER/' /etc/php5/fpm/pool.d/www.conf
	sed -i 's/group = www-data/group = $WWWUSER/' /etc/php5/fpm/pool.d/www.conf
	sed -i 's/listen.owner = www-data/listen.owner = $WWWUSER/' /etc/php5/fpm/pool.d/www.conf
	sed -i 's/listen.group = www-data/listen.group = $WWWUSER/' /etc/php5/fpm/pool.d/www.conf
	chown $WWWUSER:$WWWUSER /var/run/php5-fpm.sock
	service php5-fpm restart

	# Nginx/Pagespeed from source
	installer build-essential build-essential
	installer zlib1g-dev zlib1g-dev
	installer libpcre3 libpcre3
	installer libpcre3-dev libpcre3-dev
	installer libssl-dev libssl-dev
	cd ~
	wget https://github.com/pagespeed/ngx_pagespeed/archive/release-${PAGESPEED}-beta.zip
	unzip release-${PAGESPEED}-beta.zip
	cd ngx_pagespeed-release-${PAGESPEED}-beta/
	wget https://dl.google.com/dl/page-speed/psol/${PSOL}.tar.gz
	tar -xzvf ${PSOL}.tar.gz  # extracts to psol/
	cd ~
	wget http://nginx.org/download/nginx-$NGINX.tar.gz # download nginx
	tar -xvzf nginx-$NGINX.tar.gz # uncompress nginx
	cd nginx-$NGINX/<commit_msg>double quotes required for vars<commit_after>#!/bin/bash
currentUbuntuVersionSupported="14.04"

############################################################
# Functions
############################################################

# installer nickName, actualName
# example: installer best-text-editor nano
function installer
{
	if [ -z "`which "$1" 2>/dev/null`" ]
	then
		executable=$1
		shift
		while [ -n "$1" ]
		do
			DEBIAN_FRONTEND=noninteractive apt-get -q -y install "$1"
			apt-get clean
			printInfo "$executable installed"
			shift
		done
	else
		printWarn "$2 already installed"
	fi
}

# uninstaller nickName, actualName
# example: uninstaller /usr/sbin/apache2 'apache2*'
function uninstaller
{
	if [ -n "`which "$1" 2>/dev/null`" ]
	then
		DEBIAN_FRONTEND=noninteractive apt-get -q -y remove --purge "$2"
		apt-get clean
		printInfo "$2 uninstalled"
	else
		printWarn "$2 is not installed"
	fi
}

# exits script if something goes wrong
function die
{
	echo "ERROR: $1" > /dev/null 1>&2
	exit 1
}

# Green Text
function printInfo {
	echo -n -e '\e[32m'
	echo -n $1
	echo -e '\e[0m'
}

# Yellow Text
function printWarn
{
	echo -n -e '\e[93m'
	echo -n $1
	echo -e '\e[0m'
}

# Red Text
function printError
{
	echo -n -e '\e[91m'
	echo -n $1
	echo -e '\e[0m'
}

# Do some sanity checking (root and Ubuntu version)
function checkSanity
{
	if [ $(/usr/bin/id -u) != "0" ]
	then
		die 'Must be run by root user'
	fi
	. /etc/lsb-release
	version=$DISTRIB_RELEASE
	if [ "$version" != "$currentUbuntuVersionSupported" ]
	then
		die "Distribution is not supported"
	fi
}

############################################################
# Initial Setup
############################################################

function baseInstaller
{
	installer nano nano # text editor
	installer vim vim # text editor
	installer iftop iftop # show network usage
	installer nload nload # visualize network usage
	installer htop htop # task manager
	installer mc mc # file explorer
	installer unzip unzip
	installer zip zip
	installer curl curl
	installer screen screen
	installer gt5 gt5 # visual disk usage
	installer nslookup dnsutils # dns tools
	ppaGit # verison control
}

function removeUneededPackages
{
	# Apache
	service apache2 stop
	apt-get remove apache2* -y
	apt-get autoremove -y
}

function setTimezone
{
	dpkg-reconfigure tzdata
}

function ppaSupport
{
	installer python-software-properties python-software-properties
	installer software-properties-common software-properties-common
}

function hardenSysctl
{
	source ~/www-ubuntu/www-ubuntu.conf
	if [ "$hasHardenSysctlRun" = false ]
	then
		cat sysctl-append.conf >> /etc/sysctl.conf
		sysctl -p > /dev/null
		sed -i 's/hasHardenSysctlRun.*/hasHardenSysctlRun=true/' www-ubuntu.conf
	else
		printWarn "hardenSysctl has already been run on this system, function skipped."
	fi
}

function ppaGit
{
	add-apt-repository ppa:git-core/ppa -y
	apt-get update
	apt-get upgrade -y # git was installed to pull in www-ubuntu
	printInfo "git was upgraded to the ppa verison"
}

function scriptAlias
{
	source ~/www-ubuntu/www-ubuntu.conf
	if [ "$hasAnAliasBeenAdded" = false ]
	then
		echo "alias www-ubuntu='/root/www-ubuntu/www-ubuntu.sh'" >> /root/.bashrc
		cd ~/www-ubuntu
		sed -i 's/hasAnAliasBeenAdded.*/hasAnAliasBeenAdded=true/' www-ubuntu.conf
	fi
}

function baseSetup
{
	setTimezone
	removeUneededPackages
	runUpdater
	hardenSysctl
	ppaSupport
	baseInstaller
	runCleaner
	scriptAlias
}

############################################################
# Main options
############################################################

# Nginx/PHP
function installWWW
{
	source ~/www-ubuntu/www-ubuntu.conf
	if [ "$hasInstallWWW" = true ]
	then
		die "installWWW has already been run, if run again conflicts will be created"
	fi
	NGINX="1.6.2"
	PAGESPEED="192.168.3.11"
	PSOL="192.168.3.11"
	WWWUSER="deploy"
	# Create user
	adduser deploy
	# PHP
	# https://www.digitalocean.com/community/tutorials/how-to-install-linux-nginx-mysql-php-lemp-stack-on-ubuntu-14-04
	installer php5-fpm php5-fpm
	installer php5-mysql php5-mysql
	installer php-apc php-apc
	sed -i "s/user = www-data/user = $WWWUSER/" /etc/php5/fpm/pool.d/www.conf
	sed -i "s/group = www-data/group = $WWWUSER/" /etc/php5/fpm/pool.d/www.conf
	sed -i "s/listen.owner = www-data/listen.owner = $WWWUSER/" /etc/php5/fpm/pool.d/www.conf
	sed -i "s/listen.group = www-data/listen.group = $WWWUSER/" /etc/php5/fpm/pool.d/www.conf
	chown $WWWUSER:$WWWUSER /var/run/php5-fpm.sock
	service php5-fpm restart

	# Nginx/Pagespeed from source
	installer build-essential build-essential
	installer zlib1g-dev zlib1g-dev
	installer libpcre3 libpcre3
	installer libpcre3-dev libpcre3-dev
	installer libssl-dev libssl-dev
	cd ~
	wget https://github.com/pagespeed/ngx_pagespeed/archive/release-${PAGESPEED}-beta.zip
	unzip release-${PAGESPEED}-beta.zip
	cd ngx_pagespeed-release-${PAGESPEED}-beta/
	wget https://dl.google.com/dl/page-speed/psol/${PSOL}.tar.gz
	tar -xzvf ${PSOL}.tar.gz  # extracts to psol/
	cd ~
	wget http://nginx.org/download/nginx-$NGINX.tar.gz # download nginx
	tar -xvzf nginx-$NGINX.tar.gz # uncompress nginx
	cd nginx-$NGINX/
{'fraction_non_alphanumeric': 0.17926734216679657, 'fraction_numerical': 0.01539360872954014, 'mean_word_length': 4.0541605120630235, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 21, 'https://': 6, 'lorem ipsum': 0, 'www.': 44, 'xml': 0}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1593301', 'n_tokens_mistral': 3991, 'n_tokens_neox': 3706, 'n_words': 923}