#!/bin/bash
#
# .---.                  .              .
# |                      |              |
# |--- .--. .-.  .-.  .-.|  .-. .--.--. |.-.  .-. .--.  .-.
# |    |   (.-' (.-' (   | (   )|  |  | |   )(   )|  | (.-'
# '    '     --'  --'  -' -  -' '  '   -' -'   -' '   -  --'
#
#                    Freedom in the Cloud
#
# Wiki application
#
# License
# =======
#
# Copyright (C) 2014-2016 Bob Mottram <bob@robotics.uk.to>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

WIKI_DOMAIN_NAME=
WIKI_ADMIN_PASSWORD=
WIKI_TITLE="${PROJECT_NAME} Wiki"
WIKI_CODE=
WIKI_ONION_PORT=8089

function upgrade_wiki {
    echo ''
}

function backup_local_wiki {
    echo ''
}

function backup_remote_wiki {
    echo ''
}

function remove_wiki {
	if ! grep -Fxq "install_wiki" $COMPLETION_FILE; then
		return
	fi
	function_check remove_onion_service
	remove_onion_service wiki ${WIKI_ONION_PORT}
	nginx_dissite $WIKI_DOMAIN_NAME
	if [ -f /etc/nginx/sites-available/$WIKI_DOMAIN_NAME ]; then
		rm /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
	fi
	apt-get -y remove --purge dokuwiki
	if [ ! -d /var/www/$WIKI_DOMAIN_NAME ]; then
		rm -rf /var/www/$WIKI_DOMAIN_NAME
	fi
	if [ -d /var/lib/dokuwiki ]; then
		rm -rf /var/lib/dokuwiki
	fi
	if [ -d /etc/dokuwiki ]; then
		rm -rf /etc/dokuwiki
	fi
	if [ -d /usr/share/dokuwiki ]; then
		rm -rf /usr/share/dokuwiki
	fi
	sed -i '/install_wiki/d' $COMPLETION_FILE
}

function get_wiki_admin_password {
	if [ -f /home/$MY_USERNAME/README ]; then
		if grep -q "Wiki password" /home/$MY_USERNAME/README; then
			WIKI_ADMIN_PASSWORD=$(cat /home/$MY_USERNAME/README | grep "Wiki password:" | awk -F ':' '{print $2}' | sed 's/^ *//')
		fi
	fi
}

function install_wiki {
	if [[ $SYSTEM_TYPE == "$VARIANT_CLOUD" || $SYSTEM_TYPE == "$VARIANT_MAILBOX" || $SYSTEM_TYPE == "$VARIANT_CHAT" || $SYSTEM_TYPE == "$VARIANT_SOCIAL" || $SYSTEM_TYPE == "$VARIANT_MEDIA" || $SYSTEM_TYPE == "$VARIANT_DEVELOPER" || $SYSTEM_TYPE == "$VARIANT_MASH" ]]; then
		return
	fi
	if grep -Fxq "install_wiki" $COMPLETION_FILE; then
		return
	fi
	if [ ! $WIKI_DOMAIN_NAME ]; then
		return
	fi
	apt-get -y install dokuwiki
	apt-get -y remove --purge apache*
	if [ -d /etc/apache2 ]; then
		rm -rf /etc/apache2
		echo $'Removed Apache installation after Dokuwiki install'
	fi

	if [ ! -d /var/www/$WIKI_DOMAIN_NAME ]; then
		mkdir /var/www/$WIKI_DOMAIN_NAME
	fi
	if [ -d /var/www/$WIKI_DOMAIN_NAME/htdocs ]; then
		rm -rf /var/www/$WIKI_DOMAIN_NAME/htdocs
	fi

	ln -s /usr/share/dokuwiki /var/www/$WIKI_DOMAIN_NAME/htdocs

	mkdir /var/lib/dokuwiki/custom
	cp /etc/dokuwiki/local.php.dist /var/lib/dokuwiki/custom/local.php
	ln -s /var/lib/dokuwiki/custom/local.php /etc/dokuwiki/local.php

	chown www-data /var/lib/dokuwiki/custom
	chown www-data /var/lib/dokuwiki/custom/local.php
	chown -R www-data /etc/dokuwiki
	chown -R www-data /usr/share/dokuwiki/lib/
	chmod 600 /var/lib/dokuwiki/custom/local.php
	chmod -R 755 /usr/share/dokuwiki/lib

	sed -i 's|//$conf|$conf|g' /var/lib/dokuwiki/custom/local.php
	sed -i "s|joe|$MY_USERNAME|g" /var/lib/dokuwiki/custom/local.php

	sed -i "s|Debian DokuWiki|$WIKI_TITLE|g" /etc/dokuwiki/local.php

	# set the admin user
	sed -i "s/@admin/$MY_USERNAME/g" /etc/dokuwiki/local.php

	# disallow registration of new users
	if ! grep -q "disableactions" /etc/dokuwiki/local.php; then
		echo "\$conf['disableactions'] = 'register';" >> /etc/dokuwiki/local.php
	fi
	if ! grep -q "disableactions" /var/lib/dokuwiki/custom/local.php; then
		echo "\$conf['disableactions'] = 'register';" >> /var/lib/dokuwiki/custom/local.php
	fi

	if ! grep -q "authtype" /var/lib/dokuwiki/custom/local.php; then
		echo "\$conf['authtype'] = 'authplain';" >> /var/lib/dokuwiki/custom/local.php
	fi
	if ! grep -q "authtype" /etc/dokuwiki/local.php; then
		echo "\$conf['authtype'] = 'authplain';" >> /etc/dokuwiki/local.php
	fi

	function_check get_wiki_admin_password
	get_wiki_admin_password
	if [ ! $WIKI_ADMIN_PASSWORD ]; then
		if [ -f $IMAGE_PASSWORD_FILE ]; then
			WIKI_ADMIN_PASSWORD="$(printf `cat $IMAGE_PASSWORD_FILE`)"
		else
			WIKI_ADMIN_PASSWORD="$(create_password ${MINIMUM_PASSWORD_LENGTH})"
		fi
	fi
	HASHED_WIKI_PASSWORD=$(echo -n "$WIKI_ADMIN_PASSWORD" | md5sum | awk -F ' ' '{print $1}')
	echo -n "$MY_USERNAME:$HASHED_WIKI_PASSWORD:$MY_NAME:$MY_EMAIL:admin,user,upload" > /var/lib/dokuwiki/acl/users.auth.php
	chmod 640 /var/lib/dokuwiki/acl/users.auth.php

	if ! grep -q "video/ogg" /etc/dokuwiki/mime.conf; then
		echo 'ogv     video/ogg' >> /etc/dokuwiki/mime.conf
	fi
	if ! grep -q "video/mp4" /etc/dokuwiki/mime.conf; then
		echo 'mp4     video/mp4' >> /etc/dokuwiki/mime.conf
	fi
	if ! grep -q "video/webm" /etc/dokuwiki/mime.conf; then
		echo 'webm    video/webm' >> /etc/dokuwiki/mime.conf
	fi

	WIKI_ONION_HOSTNAME=$(add_onion_service wiki 80 ${WIKI_ONION_PORT})

	if [[ $ONION_ONLY == "no" ]]; then
		echo 'server {' > /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '    listen 80;' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo "    root /var/www/$WIKI_DOMAIN_NAME/htdocs;" >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo "    server_name $WIKI_DOMAIN_NAME;" >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '    access_log off;' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo "    error_log /var/log/nginx/${WIKI_DOMAIN_NAME}_error.log $WEBSERVER_LOG_LEVEL;" >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '    index index.php;' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '    charset utf-8;' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '    proxy_read_timeout 86400s;' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		function_check nginx_disable_sniffing
		nginx_disable_sniffing $WIKI_DOMAIN_NAME
		function_check nginx_limits
		nginx_limits $WIKI_DOMAIN_NAME
		echo '    # rewrite to front controller as default rule' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '    location / {' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '        rewrite ^/(.*) /index.php?q=$uri&$args last;' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '    }' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo "    # make sure webfinger and other well known services aren't blocked" >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '    # by denying dot files and rewrite request to the front controller' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '    location ^~ /.well-known/ {' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '        allow all;' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '    }' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '    # statically serve these file types when possible' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '    # otherwise fall back to front controller' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '    # allow browser to cache them' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '    # added .htm for advanced source code editor library' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '    location ~* \.(jpg|jpeg|gif|png|ico|css|js|htm|html|ttf|woff|svg)$ {' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '        expires 30d;' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '        try_files $uri /index.php?q=$uri&$args;' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '    }' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '    # block these file types' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '    location ~* \.(tpl|md|tgz|log|out)$ {' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '        deny all;' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '    }' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '    # or a unix socket' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '    location ~* \.php$ {' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '        # Zero-day exploit defense.' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '        # http://forum.nginx.org/read.php?2,88845,page=3' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo "        # Won't work properly (404 error) if the file is not stored on this" >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo "        # server, which is entirely possible with php-fpm/php-fcgi." >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo "        # Comment the 'try_files' line out if you set up php-fpm/php-fcgi on" >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo "        # another machine. And then cross your fingers that you won't get hacked." >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '        try_files $uri $uri/ /index.php;' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '        # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '        fastcgi_split_path_info ^(.+\.php)(/.+)$;' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '        # With php5-cgi alone:' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '        # fastcgi_pass 127.0.0.1:9000;' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '        # With php5-fpm:' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '        fastcgi_pass unix:/var/run/php5-fpm.sock;' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '        include fastcgi_params;' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '        fastcgi_index index.php;' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '    }' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '    # deny access to all dot files' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '    location ~ /\. {' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '        deny all;' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '    }' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '    #deny access to store' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '    location ~ /store {' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '        deny all;' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '    }' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '    location ~ /(data|conf|bin|inc)/ {' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '      deny all;' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '    }' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '    location ~ /\.ht {' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '      deny  all;' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '    }' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '}' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo 'server {' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '    listen 443 ssl;' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo "    root /var/www/$WIKI_DOMAIN_NAME/htdocs;" >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo "    server_name $WIKI_DOMAIN_NAME;" >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '    access_log off;' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo "    error_log /var/log/nginx/${WIKI_DOMAIN_NAME}_error_ssl.log $WEBSERVER_LOG_LEVEL;" >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '    index index.php;' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '    charset utf-8;' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '    proxy_read_timeout 86400s;' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		function_check nginx_limits
		nginx_limits $WIKI_DOMAIN_NAME
		function_check nginx_ssl
		nginx_ssl $WIKI_DOMAIN_NAME
		function_check nginx_disable_sniffing
		nginx_disable_sniffing $WIKI_DOMAIN_NAME
		echo '    add_header Strict-Transport-Security "max-age=0;";' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '    # webmail' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '    location /webmail {' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '        rewrite ^/(.*) /webmail/index.php last;' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '        rewrite ^/(.*) /webmail/installer/index.php last;' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '    }' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '    # rewrite to front controller as default rule' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '    location / {' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '        rewrite ^/(.*) /index.php?q=$uri&$args last;' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '    }' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo "    # make sure webfinger and other well known services aren't blocked" >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '    # by denying dot files and rewrite request to the front controller' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '    location ^~ /.well-known/ {' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '        allow all;' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '    }' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '    # statically serve these file types when possible' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '    # otherwise fall back to front controller' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '    # allow browser to cache them' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '    # added .htm for advanced source code editor library' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '    location ~* \.(jpg|jpeg|gif|png|ico|css|js|htm|html|ttf|woff|svg)$ {' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '        expires 30d;' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '        try_files $uri /index.php?q=$uri&$args;' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '    }' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '    # block these file types' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '    location ~* \.(tpl|md|tgz|log|out)$ {' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '        deny all;' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '    }' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '    # or a unix socket' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '    location ~* \.php$ {' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '        # Zero-day exploit defense.' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '        # http://forum.nginx.org/read.php?2,88845,page=3' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo "        # Won't work properly (404 error) if the file is not stored on this" >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo "        # server, which is entirely possible with php-fpm/php-fcgi." >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo "        # Comment the 'try_files' line out if you set up php-fpm/php-fcgi on" >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo "        # another machine. And then cross your fingers that you won't get hacked." >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '        try_files $uri $uri/ /index.php;' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '        # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '        fastcgi_split_path_info ^(.+\.php)(/.+)$;' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '        # With php5-cgi alone:' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '        # fastcgi_pass 127.0.0.1:9000;' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '        # With php5-fpm:' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '        fastcgi_pass unix:/var/run/php5-fpm.sock;' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '        include fastcgi_params;' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '        fastcgi_index index.php;' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '    }' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '    # deny access to all dot files' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '    location ~ /\. {' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '        deny all;' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '    }' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '    #deny access to store' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '    location ~ /store {' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '        deny all;' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '    }' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '    location ~ /(data|conf|bin|inc)/ {' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '      deny all;' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '    }' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '    location ~ /\.ht {' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '      deny  all;' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '    }' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '}' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
		echo '' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
	else
		echo -n '' > /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
	fi
	echo 'server {' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
	echo "    listen 127.0.0.1:${WIKI_ONION_PORT} default_server;" >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
	echo "    root /var/www/$WIKI_DOMAIN_NAME/htdocs;" >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
	echo "    server_name $WIKI_ONION_HOSTNAME;" >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
	echo '    access_log off;' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
	echo "    error_log /var/log/nginx/${WIKI_DOMAIN_NAME}_error_ssl.log $WEBSERVER_LOG_LEVEL;" >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
	echo '    index index.php;' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
	echo '    charset utf-8;' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
	echo '    proxy_read_timeout 86400s;' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
	function_check nginx_limits
	nginx_limits $WIKI_DOMAIN_NAME
	function_check nginx_disable_sniffing
	nginx_disable_sniffing $WIKI_DOMAIN_NAME
	echo '    add_header Strict-Transport-Security "max-age=0;";' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
	echo '' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
	echo '    # rewrite to front controller as default rule' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
	echo '    location / {' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
	echo '        rewrite ^/(.*) /index.php?q=$uri&$args last;' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
	echo '    }' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
	echo '' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
	echo "    # make sure webfinger and other well known services aren't blocked" >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
	echo '    # by denying dot files and rewrite request to the front controller' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
	echo '    location ^~ /.well-known/ {' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
	echo '        allow all;' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
	echo '    }' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
	echo '' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
	echo '    # statically serve these file types when possible' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
	echo '    # otherwise fall back to front controller' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
	echo '    # allow browser to cache them' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
	echo '    # added .htm for advanced source code editor library' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
	echo '    location ~* \.(jpg|jpeg|gif|png|ico|css|js|htm|html|ttf|woff|svg)$ {' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
	echo '        expires 30d;' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
	echo '        try_files $uri /index.php?q=$uri&$args;' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
	echo '    }' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
	echo '' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
	echo '    # block these file types' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
	echo '    location ~* \.(tpl|md|tgz|log|out)$ {' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
	echo '        deny all;' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
	echo '    }' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
	echo '' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
	echo '    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
	echo '    # or a unix socket' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
	echo '    location ~* \.php$ {' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
	echo '        # Zero-day exploit defense.' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
	echo '        # http://forum.nginx.org/read.php?2,88845,page=3' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
	echo "        # Won't work properly (404 error) if the file is not stored on this" >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
	echo "        # server, which is entirely possible with php-fpm/php-fcgi." >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
	echo "        # Comment the 'try_files' line out if you set up php-fpm/php-fcgi on" >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
	echo "        # another machine. And then cross your fingers that you won't get hacked." >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
	echo '        try_files $uri $uri/ /index.php;' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
	echo '        # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
	echo '        fastcgi_split_path_info ^(.+\.php)(/.+)$;' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
	echo '        # With php5-cgi alone:' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
	echo '        # fastcgi_pass 127.0.0.1:9000;' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
	echo '        # With php5-fpm:' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
	echo '        fastcgi_pass unix:/var/run/php5-fpm.sock;' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
	echo '        include fastcgi_params;' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
	echo '        fastcgi_index index.php;' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
	echo '        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
	echo '    }' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
	echo '' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
	echo '    # deny access to all dot files' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
	echo '    location ~ /\. {' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
	echo '        deny all;' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
	echo '    }' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
	echo '' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
	echo '    #deny access to store' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
	echo '    location ~ /store {' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
	echo '        deny all;' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
	echo '    }' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
	echo '    location ~ /(data|conf|bin|inc)/ {' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
	echo '      deny all;' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
	echo '    }' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
	echo '    location ~ /\.ht {' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
	echo '      deny  all;' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
	echo '    }' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME
	echo '}' >> /etc/nginx/sites-available/$WIKI_DOMAIN_NAME

	function_check create_site_certificate
	create_site_certificate $WIKI_DOMAIN_NAME

	function_check configure_php
	configure_php

	nginx_ensite $WIKI_DOMAIN_NAME

	systemctl restart php5-fpm
	systemctl restart nginx

	echo "Wiki onion domain:${WIKI_ONION_HOSTNAME}" >> $COMPLETION_FILE

	function_check add_ddns_domain
	add_ddns_domain $WIKI_DOMAIN_NAME

	# add some post-install instructions
	if ! grep -q $"Wiki password" /home/$MY_USERNAME/README; then
		echo '' >> /home/$MY_USERNAME/README
		echo '' >> /home/$MY_USERNAME/README
		echo $'Wiki' >> /home/$MY_USERNAME/README
		echo '====' >> /home/$MY_USERNAME/README
		echo $"Wiki onion domain: ${WIKI_ONION_HOSTNAME}" >> /home/$MY_USERNAME/README
		echo $"Wiki username: $MY_USERNAME" >> /home/$MY_USERNAME/README
		echo $"Wiki password: $WIKI_ADMIN_PASSWORD" >> /home/$MY_USERNAME/README
		echo '' >> /home/$MY_USERNAME/README
		echo $'Once you have set up the wiki then remove the install file:' >> /home/$MY_USERNAME/README
		echo '' >> /home/$MY_USERNAME/README
		echo "  rm /var/www/$WIKI_DOMAIN_NAME/htdocs/install.php" >> /home/$MY_USERNAME/README
		echo '' >> /home/$MY_USERNAME/README
		chown $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME/README
		chmod 600 /home/$MY_USERNAME/README
	fi

	echo "Wiki domain:$WIKI_DOMAIN_NAME" >> $COMPLETION_FILE
	echo 'install_wiki' >> $COMPLETION_FILE
}

# NOTE: deliberately no exit 0
