#!/bin/bash

set -ue

source inc/common

##
# This is where things can get a bit hectic. So this long blog is just to
# help preface what's going on here. When this hook fires it will always
# try to re-configure the entire unit. It's like a GOD hook, triggering
# or using data from every other hook with the exception of the install
# and upgrade-charm hooks.
#
# First, get a bunch of configuration values. The idea being this is should
# be the only place that config-get is run as it's "unreliable" anywhere else
# Data should then either be passed to functions or written down - you know
# "YOU SHOULD ALWAYS LEAVE A NOTE!"
#
# From here, the web engine will either be updated or left alone. We always
# assume it's going to be nginx unless you specify apache. So if you put
# "lighttpd" or "i love apache" you're going to get nginx, so don't do that.
# Configuration files are re-written every time. This is to make sure that
# any changes to long running units overtime are included. It may seem
# expensive to keep re-writing the same files but in the end having all
# the units in a sane and identical state is worth it.
#
# Next, we do some small file moving around. Just for debug stuff.
#
# After that tuning levels are decided and executed. This is a bit more
# involved than just running a function, check inc/common for that craziness
#
# After that, it's time to get any user-defined content! do_vcs does that
# and a little more
#
# Caching stuff, basically "DO WE HAVE MEMCACHED, GOOD LETS DO CONFIG"
#
# Then we stop and start everything again and wait for the next round.
##

tuning_level=`config-get tuning`
wp_content_repo=`config-get wp-content`
expose_info=`config-get debug`
engine=`config-get engine`
unit_address=`unit-get private-address`

# Make it lower case
tuning_level=${tuning_level,,}
expose_info=${expose_info,,}
engine=${engine,,}


if [ "$engine" == "apache" ] || [ "$engine" == "apache2" ]; then
	if [ -f .web-engine ]; then
		web_engine=`cat .web-engine`
		service $web_engine stop
	fi
	sed -i -e "s/# deb \(.*\) multiverse/deb \1 multiverse/g" /etc/apt/sources.list #for libapache2-mod-fastcgi
	apt-get update
	apt-get -y purge nginx
	apt-get install -y apache2-mpm-worker libapache2-mod-fastcgi
	service apache2 stop

	rm -f /var/www/index.html

	rm -f /etc/apache2/sites-enabled/*
	a2enmod actions fastcgi alias proxy_balancer proxy_http headers

	install -o root -g root -m 0644 files/charm/apache/etc_apache2_conf-d_php5-fpm.conf /etc/apache2/conf.d/php5-fpm.conf

	juju-log "Installing Apache loadbal config..."
	install -o root -g root -m 0644 files/charm/apache/etc_apache2_sites-enabled_loadbalancer /etc/apache2/sites-available/loadbalancer
	sed -i -e "s/^  ServerName .*/  ServerName ${unit_address}/" /etc/apache2/sites-available/loadbalancer
	a2ensite loadbalancer

	juju-log "Installing Apache wordpress config..."
	install -o root -g root -m 0644 files/charm/apache/etc_apache2_sites-enabled_wordpress /etc/apache2/sites-available/wordpress
	a2ensite wordpress

	echo "apache2" > .web-engine
else
	if [ -f .web-engine ]; then
		web_engine=`cat .web-engine`
		service $web_engine stop
	fi
	apt-get -y purge apache2* libapache2*
	apt-get install -y nginx
	service nginx stop

	juju-log "Cleaning any old or default nginx site configs ..."
	rm -f /etc/nginx/sites-enabled/*
	rm -f /etc/nginx/conf.d/*

	juju-log "Installing nginx common config ..."
	rm -f /etc/nginx/nginx.conf
	install -o root -g root -m 0644 files/charm/nginx/etc_nginx_nginx.conf /etc/nginx/nginx.conf

	juju-log "Installing nginx actual site config ..."
	#rm -f /etc/nginx/sites-available/
	install -o root -g root -m 0644 files/charm/nginx/etc_nginx_sites-enabled_wordpress /etc/nginx/sites-available/wordpress
	ln -sf ../sites-available/wordpress /etc/nginx/sites-enabled/wordpress

	juju-log "Installing nginx loadbal config ..."
	rm -f /etc/nginx/sites-available/loadbalancer
	install -o root -g root -m 0644 files/charm/nginx/etc_nginx_sites-enabled_loadbalancer /etc/nginx/sites-available/loadbalancer
	ln -sf ../sites-available/loadbalancer /etc/nginx/sites-enabled/loadbalancer

	juju-log "Moving nginx var dirs to /mnt storage ..."
	rsync -az /var/lib/nginx /mnt/ && rm -rf /var/lib/nginx && ln -s /mnt/nginx /var/lib/

	echo "nginx" > .web-engine
fi

# http://i.imgur.com/TUF91.gif
hooks/loadbalancer-rebuild

juju-log "Restarting Services ..."
source hooks/restart

if [ ! -f $config_file_path ]; then
	juju-log "Nothing to configure, since nothing is installed"
	exit 0
fi

juju-log "Show details? $expose_info"

if [ "$expose_info" == "yes" ]; then
	rsync -az files/_debug $wp_install_path/
else
	rm -rf $wp_install_path/_debug
fi

juju-log "I will be using this tuning level: $tuning_level"

if [ "$tuning_level" == "optimized" ]; then
	# First and foremost, we need to disable the ability to edit
	# themes and upload/update plugins. This breaks a scale-out
	# environment. It's sad but true. If you want to update a plugin
	# install a theme, etc; take a look at the README.
	make_optimized
elif [ "$tuning_level" == "single" ]; then
	# We need to prepare an NFS mount, because someone is probably
	# going to try to scale out. We also need to vamp up caching.
	make_single
elif [ "$tuning_level" == "bare" ]; then
	# Okay, you know what you're doing. You're probably going to
	# use Gluster to stream-line your files, so you don't need to
	# disable anything. We trust you to do what you need to.
	make_bare
else
	juju-log "Not sure about that tuning level."
	exit 1
fi

do_vcs $wp_content_repo

if [ -z "$wp_content_repo" ]; then
	wp plugin update --path=$wp_install_path --all
fi

do_cache

chown -R www-data.www-data $wp_install_path

. hooks/restart
