#!/bin/bash
#
# .---.                  .              .
# |                      |              |
# |--- .--. .-.  .-.  .-.|  .-. .--.--. |.-.  .-. .--.  .-.
# |    |   (.-' (.-' (   | (   )|  |  | |   )(   )|  | (.-'
# '    '     --'  --'  -' -  -' '  '   -' -'   -' '   -  --'
#
#                    Freedom in the Cloud
#
# Dokuwiki application
#
# License
# =======
#
# Copyright (C) 2014-2016 Bob Mottram <bob@freedombone.net>
#
# 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/>.

VARIANTS='full full-vim writer'

IN_DEFAULT_INSTALL=0

SHOW_ON_ABOUT=1

DOKUWIKI_DOMAIN_NAME=
DOKUWIKI_ADMIN_PASSWORD=
DOKUWIKI_TITLE="${PROJECT_NAME} Dokuwiki"
DOKUWIKI_CODE=
DOKUWIKI_ONION_PORT=8089

dokuwiki_variables=(ONION_ONLY
                MY_USERNAME
                DOKUWIKI_TITLE
                DOKUWIKI_DOMAIN_NAME
                DOKUWIKI_CODE
                DDNS_PROVIDER)

function install_interactive_dokuwiki {
    if [[ $ONION_ONLY != "no" ]]; then
        DOKUWIKI_TITLE=$'My Dokuwiki'
        DOKUWIKI_DOMAIN_NAME='dokuwiki.local'
        write_config_param "DOKUWIKI_TITLE" "$DOKUWIKI_TITLE"
        write_config_param "DOKUWIKI_DOMAIN_NAME" "$DOKUWIKI_DOMAIN_NAME"
    else
        function_check interactive_site_details_with_title
        interactive_site_details_with_title "dokuwiki" "DOKUWIKI_TITLE" "DOKUWIKI_DOMAIN_NAME" "DOKUWIKI_CODE"
    fi
    APP_INSTALLED=1
}

function change_password_dokuwiki {
    ${PROJECT_NAME}-pass -u $1 -a dokuwiki -p "$2"
}

function reconfigure_dokuwiki {
    echo -n ''
}

function upgrade_dokuwiki {
    echo -n ''
}

function backup_local_dokuwiki {
    source_directory=/var/lib/dokuwiki
    if [ -d $source_directory ]; then
        dest_directory=dokuwiki
        function_check backup_directory_to_usb
        backup_directory_to_usb $source_directory $dest_directory
        backup_directory_to_usb /etc/dokuwiki dokuwiki2
    fi
}

function restore_local_dokuwiki {
    if [ -d /var/lib/dokuwiki ]; then
        echo $"Restoring Dokuwiki installation"
        function_check get_completion_param
        DOKUWIKI_DOMAIN_NAME=$(get_completion_param "dokuwiki domain")
        temp_restore_dir=/root/tempdokuwiki
        function_check restore_directory_from_usb
        if [ -d ${USB_MOUNT}/backup/dokuwiki ]; then
            restore_directory_from_usb ${temp_restore_dir} dokuwiki
        else
            restore_directory_from_usb ${temp_restore_dir} wiki
        fi
        cp -r ${temp_restore_dir}/var/lib/dokuwiki/* /var/lib/dokuwiki/
        if [ ! "$?" = "0" ]; then
            function_check restore_directory_from_usb
            set_user_permissions
            function_check backup_unmount_drive
            backup_unmount_drive
            exit 868
        fi
        if [ -d ${USB_MOUNT}/backup/wiki2 ]; then
            restore_directory_from_usb ${temp_restore_dir}2 wiki2
        else
            restore_directory_from_usb ${temp_restore_dir}2 dokuwiki2
        fi
        cp -r ${temp_restore_dir}2/etc/dokuwiki/* /etc/dokuwiki/
        if [ ! "$?" = "0" ]; then
            function_check set_user_permissions
            set_user_permissions
            function_check backup_unmount_drive
            backup_unmount_drive
            exit 869
        fi
        rm -rf ${temp_restore_dir}
        rm -rf ${temp_restore_dir}2
        #rm -rf /var/lib/dokuwiki/data/cache/*
        #rm -rf /var/lib/dokuwiki/data/meta/*
        chmod -R 755 /var/lib/dokuwiki/data
        chown -R www-data:www-data /var/lib/dokuwiki/data
        chown -R www-data:www-data /var/lib/dokuwiki/*
        # Ensure that the bundled SSL cert is being used
        if [ -f /etc/ssl/certs/${DOKUWIKI_DOMAIN_NAME}.bundle.crt ]; then
            sed -i "s|${DOKUWIKI_DOMAIN_NAME}.crt|${DOKUWIKI_DOMAIN_NAME}.bundle.crt|g" /etc/nginx/sites-available/${DOKUWIKI_DOMAIN_NAME}
        fi
        if [ -d /etc/letsencrypt/live/${DOKUWIKI_DOMAIN_NAME} ]; then
            ln -s /etc/letsencrypt/live/${DOKUWIKI_DOMAIN_NAME}/privkey.pem /etc/ssl/private/${DOKUWIKI_DOMAIN_NAME}.key
            ln -s /etc/letsencrypt/live/${DOKUWIKI_DOMAIN_NAME}/fullchain.pem /etc/ssl/certs/${DOKUWIKI_DOMAIN_NAME}.pem
        fi
        echo $"Restore of Dokuwiki complete"
    fi
}

function backup_remote_dokuwiki {
    if [ -d /etc/dokuwiki ]; then
        echo $"Backing up dokuwiki"
        backup_directory_to_friend /var/lib/dokuwiki dokuwiki
        backup_directory_to_friend /etc/dokuwiki dokuwiki2
    fi
}

function restore_remote_dokuwiki {
    if [ -d $SERVER_DIRECTORY/backup/dokuwiki ]; then
        function_check get_completion_param
        DOKUWIKI_DOMAIN_NAME=$(get_completion_param "dokuwiki domain")
        echo $"Restoring Dokuwiki installation $DOKUWIKI_DOMAIN_NAME"
        function_check restore_directory_from_friend
        restore_directory_from_friend /root/tempdokuwiki dokuwiki
        cp -r /root/tempdokuwiki/var/lib/dokuwiki/* /var/lib/dokuwiki/
        if [ ! "$?" = "0" ]; then
            exit 868
        fi
        restore_directory_from_friend /root/tempdokuwiki2 dokuwiki2
        cp -r /root/tempdokuwiki2/etc/dokuwiki/* /etc/dokuwiki/
        if [ ! "$?" = "0" ]; then
            exit 869
        fi
        rm -rf /root/tempdokuwiki
        rm -rf /root/tempdokuwiki2
        #rm -rf /var/lib/dokuwiki/data/cache/*
        #rm -rf /var/lib/dokuwiki/data/meta/*
        chmod -R 755 /var/lib/dokuwiki/data
        chown -R www-data:www-data /var/lib/dokuwiki/data
        chown -R www-data:www-data /var/lib/dokuwiki/*
        # Ensure that the bundled SSL cert is being used
        if [ -f /etc/ssl/certs/${DOKUWIKI_DOMAIN_NAME}.bundle.crt ]; then
            sed -i "s|${DOKUWIKI_DOMAIN_NAME}.crt|${DOKUWIKI_DOMAIN_NAME}.bundle.crt|g" /etc/nginx/sites-available/${DOKUWIKI_DOMAIN_NAME}
        fi
        if [ -d /etc/letsencrypt/live/${DOKUWIKI_DOMAIN_NAME} ]; then
            ln -s /etc/letsencrypt/live/${DOKUWIKI_DOMAIN_NAME}/privkey.pem /etc/ssl/private/${DOKUWIKI_DOMAIN_NAME}.key
            ln -s /etc/letsencrypt/live/${DOKUWIKI_DOMAIN_NAME}/fullchain.pem /etc/ssl/certs/${DOKUWIKI_DOMAIN_NAME}.pem
        fi
        echo $"Restore of Dokuwiki complete"
    fi
}

function remove_dokuwiki {
    if [ ${#DOKUWIKI_DOMAIN_NAME} -eq 0 ]; then
        return
    fi
    function_check remove_onion_service
    remove_onion_service dokuwiki ${DOKUWIKI_ONION_PORT}
    nginx_dissite $DOKUWIKI_DOMAIN_NAME
    remove_certs $DOKUWIKI_DOMAIN_NAME
    if [ -f /etc/nginx/sites-available/$DOKUWIKI_DOMAIN_NAME ]; then
        rm /etc/nginx/sites-available/$DOKUWIKI_DOMAIN_NAME
    fi
    apt-get -yq remove --purge dokuwiki
    if [ ! -d /var/www/$DOKUWIKI_DOMAIN_NAME ]; then
        rm -rf /var/www/$DOKUWIKI_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
    remove_completion_param "install_dokuwiki"
    remove_completion_param "dokuwiki domain"
    sed -i '/dokuwiki/d' $COMPLETION_FILE

    function_check remove_ddns_domain
    remove_ddns_domain $DOKUWIKI_DOMAIN_NAME
}

function install_dokuwiki {
    if [ ! $DOKUWIKI_DOMAIN_NAME ]; then
        return
    fi
    apt-get -yq install dokuwiki
    apt-get -yq remove --purge apache*
    if [ -d /etc/apache2 ]; then
        rm -rf /etc/apache2
        echo $'Removed Apache installation after Dokuwiki install'
    fi

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

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

    if [ ! -d /var/lib/dokuwiki/custom ]; then
        mkdir /var/lib/dokuwiki/custom
    fi
    cp /etc/dokuwiki/local.php.dist /var/lib/dokuwiki/custom/local.php
    if [ ! -f /etc/dokuwiki/local.php ]; then
        ln -s /var/lib/dokuwiki/custom/local.php /etc/dokuwiki/local.php
    fi

    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|$DOKUWIKI_TITLE|g" /etc/dokuwiki/local.php
    sed -i "s|Debian DokuWiki|$DOKUWIKI_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

    if [ -f $IMAGE_PASSWORD_FILE ]; then
        DOKUWIKI_ADMIN_PASSWORD="$(printf `cat $IMAGE_PASSWORD_FILE`)"
    else
        if [ ! $DOKUWIKI_ADMIN_PASSWORD ]; then
            DOKUWIKI_ADMIN_PASSWORD="$(create_password ${MINIMUM_PASSWORD_LENGTH})"
        fi
    fi
    HASHED_DOKUWIKI_PASSWORD=$(echo -n "$DOKUWIKI_ADMIN_PASSWORD" | md5sum | awk -F ' ' '{print $1}')
    echo -n "$MY_USERNAME:$HASHED_DOKUWIKI_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

    DOKUWIKI_ONION_HOSTNAME=$(add_onion_service dokuwiki 80 ${DOKUWIKI_ONION_PORT})
    set_completion_param "dokuwiki domain" "${DOKUWIKI_DOMAIN_NAME}"

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

    function_check create_site_certificate
    create_site_certificate $DOKUWIKI_DOMAIN_NAME

    function_check configure_php
    configure_php

    nginx_ensite $DOKUWIKI_DOMAIN_NAME

    systemctl restart php5-fpm
    systemctl restart nginx

    function_check add_ddns_domain
    add_ddns_domain $DOKUWIKI_DOMAIN_NAME

    ${PROJECT_NAME}-pass -u $MY_USERNAME -a dokuwiki -p "$DOKUWIKI_ADMIN_PASSWORD"

    APP_INSTALLED=1
}

# NOTE: deliberately no exit 0
