#!/bin/bash
#
# .---.                  .              .
# |                      |              |
# |--- .--. .-.  .-.  .-.|  .-. .--.--. |.-.  .-. .--.  .-.
# |    |   (.-' (.-' (   | (   )|  |  | |   )(   )|  | (.-'
# '    '     --'  --'  -' -  -' '  '   -' -'   -' '   -  --'
#
#                    Freedom in the Cloud
#
# Etherpad app
#
# License
# =======
#
# Copyright (C) 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

ETHERPAD_DOMAIN_NAME=
ETHERPAD_CODE=
ETHERPAD_ONION_PORT=8101
ETHERPAD_PORT=9001
ETHERPAD_REPO="https://github.com/ether/etherpad-lite"
ETHERPAD_COMMIT='223127bf39d2ba431d9c1965a7f2aadadc73d77a'
ETHERPAD_ADMIN_PASSWORD=
ETHERPAD_TITLE=$'Freedombone Docs'
ETHERPAD_WELCOME_MESSAGE=$"Welcome to ${ETHERPAD_TITLE}!\n\nThis pad text is synchronized as you type, so that everyone viewing this page sees the same text. This allows you to collaborate seamlessly on documents!"

etherpad_variables=(ONION_ONLY
                    DEFAULT_DOMAIN_NAME
                    ETHERPAD_DOMAIN_NAME
                    ETHERPAD_CODE
                    ETHERPAD_TITLE
                    ETHERPAD_WELCOME_MESSAGE
                    DDNS_PROVIDER
                    MY_USERNAME)

function etherpad_password_hash {
    echo $(python -c "from passlib.hash import bcrypt;print(bcrypt.encrypt(\"$1\", rounds=10))")
}

function change_password_etherpad {
    change_username="$1"
    new_user_password=$(etherpad_password_hash "$2")

    read_config_param ETHERPAD_DOMAIN_NAME

    if grep "\"$change_username\": {" /var/www/${ETHERPAD_DOMAIN_NAME}/htdocs/settings.json; then
        user_line=$(cat /var/www/${ETHERPAD_DOMAIN_NAME}/htdocs/settings.json | grep "\"$change_username\": {")
        if [[ "$user_line" == *"\"is_admin\": true"* ]]; then
            sed -i "s|\"$change_username\": {.*|\"$change_username\": { \"hash\": \"$new_user_password\", \"is_admin\": true }|g" /var/www/${ETHERPAD_DOMAIN_NAME}/htdocs/settings.json
        else
            sed -i "s|\"$change_username\": {.*|\"$change_username\": { \"hash\": \"$new_user_password\", \"is_admin\": false },|g" /var/www/${ETHERPAD_DOMAIN_NAME}/htdocs/settings.json
        fi
        ${PROJECT_NAME}-pass -u $change_username -a etherpad -p "$2"
        systemctl restart etherpad
    fi
}

function etherpad_create_database {
    if [ -f $IMAGE_PASSWORD_FILE ]; then
        ETHERPAD_ADMIN_PASSWORD="$(printf `cat $IMAGE_PASSWORD_FILE`)"
    else
        if [ ! $ETHERPAD_ADMIN_PASSWORD ]; then
            ETHERPAD_ADMIN_PASSWORD="$(create_password ${MINIMUM_PASSWORD_LENGTH})"
        fi
    fi
    if [ ! $ETHERPAD_ADMIN_PASSWORD ]; then
        return
    fi

    function_check create_database
    create_database etherpad "$ETHERPAD_ADMIN_PASSWORD" $MY_USERNAME
}

function create_etherpad_settings {
    settings_file=/var/www/${ETHERPAD_DOMAIN_NAME}/htdocs/settings.json
    echo '{' > $settings_file
    echo "  \"title\": \"${ETHERPAD_TITLE}\"," >> $settings_file
    echo '  "favicon": "favicon.ico",' >> $settings_file

    echo '  "ip": "0.0.0.0",' >> $settings_file
    echo "  \"port\" : ${ETHERPAD_PORT}," >> $settings_file

    echo '  "showSettingsInAdminPage" : true,' >> $settings_file

    if [[ $ONION_ONLY == 'no' ]]; then
        echo '  "ssl" : {' >> $settings_file
        echo "            \"key\"  : \"/etc/ssl/private/${ETHERPAD_DOMAIN_NAME}.key\"," >> $settings_file
        echo "            \"cert\" : \"/etc/ssl/certs/${ETHERPAD_DOMAIN_NAME}.pem\"," >> $settings_file
        echo '          },' >> $settings_file
    fi

    echo '   "dbType" : "mysql",' >> $settings_file
    echo '   "dbSettings" : {' >> $settings_file
    echo '                    "user"    : "root",' >> $settings_file
    echo '                    "host"    : "localhost",' >> $settings_file
    echo "                    \"password\": \"${MARIADB_PASSWORD}\"," >> $settings_file
    echo '                    "database": "etherpad",' >> $settings_file
    echo '                    "charset" : "utf8mb4"' >> $settings_file
    echo '                  },' >> $settings_file

    echo "  \"defaultPadText\" : \"${ETHERPAD_WELCOME_MESSAGE}\"," >> $settings_file

    echo '  "padOptions": {' >> $settings_file
    echo '    "noColors": false,' >> $settings_file
    echo '    "showControls": true,' >> $settings_file
    echo '    "showChat": true,' >> $settings_file
    echo '    "showLineNumbers": false,' >> $settings_file
    echo '    "useMonospaceFont": false,' >> $settings_file
    echo '    "userName": false,' >> $settings_file
    echo '    "userColor": true,' >> $settings_file
    echo '    "rtl": false,' >> $settings_file
    echo '    "alwaysShowChat": true,' >> $settings_file
    echo '    "chatAndUsers": true,' >> $settings_file
    echo '    "lang": "en-gb"' >> $settings_file
    echo '  },' >> $settings_file

    echo '  "suppressErrorsInPadText" : true,' >> $settings_file
    echo '  "requireSession" : false,' >> $settings_file
    echo '  "editOnly" : false,' >> $settings_file
    echo '  "sessionNoPassword" : false,' >> $settings_file
    echo '  "minify" : true,' >> $settings_file
    echo '  "maxAge" : 21600, // 60 * 60 * 6 = 6 hours' >> $settings_file
    echo '  "abiword" : null,' >> $settings_file
    echo '  "soffice" : null,' >> $settings_file
    echo '  "tidyHtml" : null,' >> $settings_file
    echo '  "allowUnknownFileEnds" : false,' >> $settings_file
    echo '  "requireAuthentication" : true,' >> $settings_file
    echo '  "requireAuthorization" : true,' >> $settings_file
    echo '  "trustProxy" : false,' >> $settings_file
    echo '  "disableIPlogging" : true,' >> $settings_file

    echo '  "users": {' >> $settings_file
    echo "    \"${MY_USERNAME}\": { \"hash\": \"$(etherpad_password_hash "${ETHERPAD_ADMIN_PASSWORD}")\", \"is_admin\": true }" >> $settings_file
    echo '  },' >> $settings_file

    echo '  "socketTransportProtocols" : ["xhr-polling", "jsonp-polling", "htmlfile"],' >> $settings_file
    echo '  "loadTest": false,' >> $settings_file
    echo '  "indentationOnNewLine": false,' >> $settings_file

    echo '  "toolbar": {' >> $settings_file
    echo '    "left": [' >> $settings_file
    echo '      ["bold", "italic", "underline", "strikethrough"],' >> $settings_file
    echo '      ["orderedlist", "unorderedlist", "indent", "outdent"],' >> $settings_file
    echo '      ["undo", "redo"],' >> $settings_file
    echo '      ["clearauthorship"]' >> $settings_file
    echo '    ],' >> $settings_file
    echo '    "right": [' >> $settings_file
    echo '      ["importexport", "timeslider", "savedrevision"],' >> $settings_file
    echo '      ["settings", "embed"],' >> $settings_file
    echo '      ["showusers"]' >> $settings_file
    echo '    ],' >> $settings_file
    echo '    "timeslider": [' >> $settings_file
    echo '      ["timeslider_export", "timeslider_returnToPad"]' >> $settings_file
    echo '    ]' >> $settings_file
    echo '  },' >> $settings_file
    echo '  "loglevel": "INFO"' >> $settings_file
    echo '}' >> $settings_file
    chmod 600 $settings_file
}

function remove_user_etherpad {
    remove_username="$1"
    settings_file=/var/www/${ETHERPAD_DOMAIN_NAME}/htdocs/settings.json

    ${PROJECT_NAME}-pass -u $remove_username --rmapp etherpad

    if grep -q "\"$remove_username\": {" $settings_file; then
        sed -i "/\"$remove_username\": {/d" $settings_file
        systemctl restart etherpad
    fi
}

function add_user_etherpad {
    new_username="$1"
    new_user_password=$(etherpad_password_hash "$2")
    settings_file=/var/www/${ETHERPAD_DOMAIN_NAME}/htdocs/settings.json

    if ! grep -q "\"$new_username\": {" $settings_file; then
        ${PROJECT_NAME}-pass -u $new_username -a etherpad -p "$2"
        sed -i "/\"users\": {/a    \"$new_username\": { \"hash\": \"$new_user_password\", \"is_admin\": false }," $settings_file
        if grep -q "\"$new_username\": {" $settings_file; then
            systemctl restart etherpad
        else
            echo '1'
            return
        fi
    fi
    echo '0'
}

function install_interactive_etherpad {
    if [ ! $ONION_ONLY ]; then
        ONION_ONLY='no'
    fi

    if [[ $ONION_ONLY != "no" ]]; then
        ETHERPAD_DOMAIN_NAME='etherpad.local'
        write_config_param "ETHERPAD_DOMAIN_NAME" "$ETHERPAD_DOMAIN_NAME"
    else
        function_check interactive_site_details
        interactive_site_details "etherpad" "ETHERPAD_DOMAIN_NAME" "ETHERPAD_CODE"
    fi
    APP_INSTALLED=1
}

function etherpad_set_title {
    read_config_param "ETHERPAD_TITLE"
    data=$(tempfile 2>/dev/null)
    trap "rm -f $data" 0 1 2 5 15
    dialog --title $"Etherpad Title" \
           --backtitle $"Freedombone Control Panel" \
           --inputbox $'Set a title for your etherpad system' 10 60 "$ETHERPAD_TITLE" 2>$data
    sel=$?
    case $sel in
        0)
            temp_title=$(<$data)
            if [ ${#temp_title} -gt 0 ]; then
                ETHERPAD_TITLE="$temp_title"
                settings_file=/var/www/$ETHERPAD_DOMAIN_NAME/htdocs/settings.json

                write_config_param "ETHERPAD_TITLE" "$ETHERPAD_TITLE"
                sed -i "s|\"title\":.*|\"title\": \"${ETHERPAD_TITLE}\"|g" $settings_file

                dialog --title $"Etherpad Title" \
                       --msgbox $"Title has been set" 6 60
            fi
           ;;
    esac
}

function etherpad_set_welcome_message {
    read_config_param "ETHERPAD_WELCOME_MESSAGE"
    data=$(tempfile 2>/dev/null)
    trap "rm -f $data" 0 1 2 5 15
    dialog --title $"Etherpad Welcome Message" \
           --backtitle $"Freedombone Control Panel" \
           --inputbox $'Set a welcome message, which can include html formatting' 10 60 "$ETHERPAD_WELCOME_MESSAGE" 2>$data
    sel=$?
    case $sel in
        0)
            temp_welcome=$(<$data)
            if [ ${#temp_welcome} -gt 0 ]; then
                ETHERPAD_WELCOME_MESSAGE="$temp_welcome"
                settings_file=/var/www/$ETHERPAD_DOMAIN_NAME/htdocs/settings.json

                write_config_param "ETHERPAD_WELCOME_MESSAGE" "$ETHERPAD_WELCOME_MESSAGE"
                sed -i "s|\"defaultPadText\" :.*|\"defaultPadText\" : \"${ETHERPAD_WELCOME_MESSAGE}\"|g" $settings_file

                dialog --title $"Etherpad Welcome Message" \
                       --msgbox $"Welcome message has been set" 6 60
            fi
           ;;
    esac
}

function configure_interactive_etherpad {
    while true
    do
        data=$(tempfile 2>/dev/null)
        trap "rm -f $data" 0 1 2 5 15
        dialog --backtitle $"Freedombone Control Panel" \
               --title $"Etherpad Settings" \
               --radiolist $"Choose an operation:" 12 70 3 \
               1 $"Set Title" off \
               2 $"Set a welcome message" off \
               3 $"Exit" on 2> $data
        sel=$?
        case $sel in
            1) return;;
            255) return;;
        esac
        case $(cat $data) in
            1) etherpad_set_title;;
            2) etherpad_set_welcome_message;;
            3) break;;
        esac
    done
}

function reconfigure_etherpad {
    create_etherpad_settings
    systemctl restart etherpad
}

function upgrade_etherpad {
    read_config_param "ETHERPAD_DOMAIN_NAME"

    function_check set_repo_commit
    set_repo_commit /var/www/$ETHERPAD_DOMAIN_NAME/htdocs "etherpad commit" "$ETHERPAD_COMMIT" $ETHERPAD_REPO
}

function backup_local_etherpad {
    ETHERPAD_DOMAIN_NAME='etherpad'
    if grep -q "etherpad domain" $COMPLETION_FILE; then
        ETHERPAD_DOMAIN_NAME=$(get_completion_param "etherpad domain")
    fi

    source_directory=/var/www/${ETHERPAD_DOMAIN_NAME}/htdocs
    if [ -d $source_directory ]; then
        dest_directory=etherpad
        function_check suspend_site
        suspend_site ${ETHERPAD_DOMAIN_NAME}

        function_check backup_directory_to_usb
        backup_directory_to_usb $source_directory $dest_directory

        function_check backup_database_to_usb
        backup_database_to_usb etherpad

        function_check restart_site
        restart_site
    fi
}

function restore_local_etherpad {
    if ! grep -q "etherpad domain" $COMPLETION_FILE; then
        return
    fi
    ETHERPAD_DOMAIN_NAME=$(get_completion_param "etherpad domain")
    if [ $ETHERPAD_DOMAIN_NAME ]; then
        temp_restore_dir=/root/tempetherpad
        etherpad_dir=/var/www/${ETHERPAD_DOMAIN_NAME}/htdocs

        function_check etherpad_create_database
        etherpad_create_database

        restore_database etherpad ${ETHERPAD_DOMAIN_NAME}
        if [ -d $temp_restore_dir ]; then
            rm -rf $temp_restore_dir
        fi

        chown -R etherpad: /var/www/${ETHERPAD_DOMAIN_NAME}/htdocs
        if [ -f /etc/ssl/certs/${ETHERPAD_DOMAIN_NAME}.pem ]; then
            chown etherpad: /etc/ssl/certs/${ETHERPAD_DOMAIN_NAME}.pem
        fi
        if [ -f /etc/ssl/private/${ETHERPAD_DOMAIN_NAME}.key ]; then
            chown etherpad: /etc/ssl/private/${ETHERPAD_DOMAIN_NAME}.key
        fi
    fi
}

function backup_remote_etherpad {
    if grep -q "etherpad domain" $COMPLETION_FILE; then
        ETHERPAD_DOMAIN_NAME=$(get_completion_param "etherpad domain")
        temp_backup_dir=/var/www/${ETHERPAD_DOMAIN_NAME}/htdocs
        if [ -d $temp_backup_dir ]; then
            function_check suspend_site
            suspend_site ${ETHERPAD_DOMAIN_NAME}

            function_check backup_database_to_friend
            backup_database_to_friend etherpad

            function_check backup_directory_to_friend
            backup_directory_to_friend $temp_backup_dir etherpad

            function_check restart_site
            restart_site
        else
            echo $"etherpad domain specified but not found in ${temp_backup_dir}"
        fi
    fi
}

function restore_remote_etherpad {
    if grep -q "etherpad domain" $COMPLETION_FILE; then
        ETHERPAD_DOMAIN_NAME=$(get_completion_param "etherpad domain")

        function_check etherpad_create_database
        etherpad_create_database

        function_check restore_database_from_friend
        restore_database_from_friend etherpad ${ETHERPAD_DOMAIN_NAME}
        if [ -d /root/tempetherpad ]; then
            rm -rf /root/tempetherpad
        fi

        chown -R etherpad: /var/www/${ETHERPAD_DOMAIN_NAME}/htdocs
        if [ -f /etc/ssl/certs/${ETHERPAD_DOMAIN_NAME}.pem ]; then
            chown etherpad: /etc/ssl/certs/${ETHERPAD_DOMAIN_NAME}.pem
        fi
        if [ -f /etc/ssl/private/${ETHERPAD_DOMAIN_NAME}.key ]; then
            chown etherpad: /etc/ssl/private/${ETHERPAD_DOMAIN_NAME}.key
        fi
    fi
}

function remove_etherpad {
    if [ ${#ETHERPAD_DOMAIN_NAME} -eq 0 ]; then
        return
    fi
    read_config_param "ETHERPAD_DOMAIN_NAME"
    read_config_param "MY_USERNAME"
    echo "Removing $ETHERPAD_DOMAIN_NAME"
    if [ -f /etc/systemd/system/etherpad.service ]; then
        systemctl stop etherpad
        systemctl disable etherpad
        rm /etc/systemd/system/etherpad.service
    fi
    nginx_dissite $ETHERPAD_DOMAIN_NAME
    remove_certs $ETHERPAD_DOMAIN_NAME
    if [ -d /var/www/$ETHERPAD_DOMAIN_NAME ]; then
        rm -rf /var/www/$ETHERPAD_DOMAIN_NAME
    fi
    if [ -f /etc/nginx/sites-available/$ETHERPAD_DOMAIN_NAME ]; then
        rm /etc/nginx/sites-available/$ETHERPAD_DOMAIN_NAME
    fi
    function_check drop_database
    drop_database etherpad
    function_check remove_onion_service
    remove_onion_service etherpad ${ETHERPAD_ONION_PORT}
    remove_app etherpad
    remove_completion_param install_etherpad
    sed -i '/etherpad/d' $COMPLETION_FILE
    remove_backup_database_local etherpad
    deluser --remove-all-files etherpad
    remove_nodejs etherpad

    function_check remove_ddns_domain
    remove_ddns_domain $ETHERPAD_DOMAIN_NAME
}

function install_etherpad {
    if [ ! $ETHERPAD_DOMAIN_NAME ]; then
        echo $'No domain name was given for etherpad'
        exit 7359
    fi

    if [ -f $IMAGE_PASSWORD_FILE ]; then
        ETHERPAD_ADMIN_PASSWORD="$(printf `cat $IMAGE_PASSWORD_FILE`)"
    else
        if [ ! $ETHERPAD_ADMIN_PASSWORD ]; then
            ETHERPAD_ADMIN_PASSWORD="$(create_password ${MINIMUM_PASSWORD_LENGTH})"
        fi
    fi

    function_check install_mariadb
    install_mariadb

    function_check get_mariadb_password
    get_mariadb_password

    function_check repair_databases_script
    repair_databases_script

    apt-get -yq install gzip git curl python libssl-dev pkg-config \
            build-essential python g++ make checkinstall \
            python-bcrypt python-passlib

    function_check install_nodejs
    install_nodejs etherpad

    if [ ! -d /var/www/$ETHERPAD_DOMAIN_NAME ]; then
        mkdir /var/www/$ETHERPAD_DOMAIN_NAME
    fi
    if [ ! -d /var/www/$ETHERPAD_DOMAIN_NAME/htdocs ]; then
        function_check git_clone
        git_clone $ETHERPAD_REPO /var/www/$ETHERPAD_DOMAIN_NAME/htdocs
        if [ ! -d /var/www/$ETHERPAD_DOMAIN_NAME/htdocs ]; then
            echo $'Unable to clone etherpad repo'
            exit 56382
        fi
    fi

    cd /var/www/$ETHERPAD_DOMAIN_NAME/htdocs
    git checkout $ETHERPAD_COMMIT -b $ETHERPAD_COMMIT
    set_completion_param "etherpad commit" "$ETHERPAD_COMMIT"

    chmod a+w /var/www/$ETHERPAD_DOMAIN_NAME/htdocs
    chown www-data:www-data /var/www/$ETHERPAD_DOMAIN_NAME/htdocs

    function_check etherpad_create_database
    etherpad_create_database

    function_check add_ddns_domain
    add_ddns_domain $ETHERPAD_DOMAIN_NAME

    create_etherpad_settings

    adduser --system --home=/var/www/$ETHERPAD_DOMAIN_NAME/htdocs/ --group etherpad
    chown -R etherpad: /var/www/$ETHERPAD_DOMAIN_NAME/htdocs/

    echo '[Unit]' > /etc/systemd/system/etherpad.service
    echo 'Description=etherpad-lite (real-time collaborative document editing)' >> /etc/systemd/system/etherpad.service
    echo 'After=syslog.target network.target' >> /etc/systemd/system/etherpad.service
    echo '' >> /etc/systemd/system/etherpad.service
    echo '[Service]' >> /etc/systemd/system/etherpad.service
    echo 'Type=simple' >> /etc/systemd/system/etherpad.service
    echo 'User=etherpad' >> /etc/systemd/system/etherpad.service
    echo 'Group=etherpad' >> /etc/systemd/system/etherpad.service
    echo "WorkingDirectory=/var/www/$ETHERPAD_DOMAIN_NAME/htdocs" >> /etc/systemd/system/etherpad.service
    echo "ExecStart=/var/www/$ETHERPAD_DOMAIN_NAME/htdocs/bin/run.sh" >> /etc/systemd/system/etherpad.service
    echo 'Restart=on-failure' >> /etc/systemd/system/etherpad.service
    echo 'SuccessExitStatus=3 4' >> /etc/systemd/system/etherpad.service
    echo 'RestartForceExitStatus=3 4' >> /etc/systemd/system/etherpad.service
    echo '' >> /etc/systemd/system/etherpad.service
    echo '[Install]' >> /etc/systemd/system/etherpad.service
    echo 'WantedBy=multi-user.target' >> /etc/systemd/system/etherpad.service
    chmod +x /etc/systemd/system/etherpad.service

    etherpad_nginx_site=/etc/nginx/sites-available/$ETHERPAD_DOMAIN_NAME
    if [[ $ONION_ONLY == "no" ]]; then
        function_check nginx_http_redirect
        nginx_http_redirect $ETHERPAD_DOMAIN_NAME
        echo 'server {' >> $etherpad_nginx_site
        echo '  listen 443 ssl;' >> $etherpad_nginx_site
        echo '  listen [::]:443 ssl;' >> $etherpad_nginx_site
        echo "  server_name $ETHERPAD_DOMAIN_NAME;" >> $etherpad_nginx_site
        echo '' >> $etherpad_nginx_site
        echo '  # Security' >> $etherpad_nginx_site
        function_check nginx_ssl
        nginx_ssl $ETHERPAD_DOMAIN_NAME

        function_check nginx_disable_sniffing
        nginx_disable_sniffing $ETHERPAD_DOMAIN_NAME

        echo '  add_header Strict-Transport-Security max-age=15768000;' >> $etherpad_nginx_site
        echo '' >> $etherpad_nginx_site
        echo '  # Logs' >> $etherpad_nginx_site
        echo '  access_log /dev/null;' >> $etherpad_nginx_site
        echo '  error_log /dev/null;' >> $etherpad_nginx_site
        echo '' >> $etherpad_nginx_site
        echo '  # Root' >> $etherpad_nginx_site
        echo "  root /var/www/$ETHERPAD_DOMAIN_NAME/htdocs;" >> $etherpad_nginx_site
        echo '' >> $etherpad_nginx_site
        echo '  location / {' >> $etherpad_nginx_site
        function_check nginx_limits
        nginx_limits $ETHERPAD_DOMAIN_NAME '15m'
        echo "    proxy_pass        http://localhost:${ETHERPAD_PORT}/;" >> $etherpad_nginx_site
        echo '    proxy_set_header  Host $host;' >> $etherpad_nginx_site
        echo '    proxy_buffering   off;' >> $etherpad_nginx_site
        echo '  }' >> $etherpad_nginx_site
        echo '' >> $etherpad_nginx_site
        nginx_keybase $ETHERPAD_DOMAIN_NAME
        echo '}' >> $etherpad_nginx_site
    else
        echo -n '' > $etherpad_nginx_site
    fi
    echo 'server {' >> $etherpad_nginx_site
    echo "    listen 127.0.0.1:$ETHERPAD_ONION_PORT default_server;" >> $etherpad_nginx_site
    echo "    server_name $ETHERPAD_DOMAIN_NAME;" >> $etherpad_nginx_site
    echo '' >> $etherpad_nginx_site
    function_check nginx_disable_sniffing
    nginx_disable_sniffing $ETHERPAD_DOMAIN_NAME
    echo '' >> $etherpad_nginx_site
    echo '  # Logs' >> $etherpad_nginx_site
    echo '  access_log /dev/null;' >> $etherpad_nginx_site
    echo '  error_log /dev/null;' >> $etherpad_nginx_site
    echo '' >> $etherpad_nginx_site
    echo '  # Root' >> $etherpad_nginx_site
    echo "  root /var/www/$ETHERPAD_DOMAIN_NAME/htdocs;" >> $etherpad_nginx_site
    echo '' >> $etherpad_nginx_site
    echo '  location / {' >> $etherpad_nginx_site
    function_check nginx_limits
    nginx_limits $ETHERPAD_DOMAIN_NAME '15m'
    echo "    proxy_pass        http://localhost:${ETHERPAD_PORT}/;" >> $etherpad_nginx_site
    echo '    proxy_set_header  Host $host;' >> $etherpad_nginx_site
    echo '    proxy_buffering   off;' >> $etherpad_nginx_site
    echo '  }' >> $etherpad_nginx_site
    echo '' >> $etherpad_nginx_site
    nginx_keybase $ETHERPAD_DOMAIN_NAME
    echo '}' >> $etherpad_nginx_site

    function_check create_site_certificate
    create_site_certificate $ETHERPAD_DOMAIN_NAME 'yes'

    if [ -f /etc/ssl/certs/${ETHERPAD_DOMAIN_NAME}.crt ]; then
        mv /etc/ssl/certs/${ETHERPAD_DOMAIN_NAME}.crt /etc/ssl/certs/${ETHERPAD_DOMAIN_NAME}.pem
    fi
    if [ -f /etc/ssl/certs/${ETHERPAD_DOMAIN_NAME}.pem ]; then
        chown etherpad: /etc/ssl/certs/${ETHERPAD_DOMAIN_NAME}.pem
    fi
    if [ -f /etc/ssl/private/${ETHERPAD_DOMAIN_NAME}.key ]; then
        chown etherpad: /etc/ssl/private/${ETHERPAD_DOMAIN_NAME}.key
    fi

    # Ensure that the database gets backed up locally, if remote
    # backups are not being used
    function_check backup_databases_script_header
    backup_databases_script_header

    function_check backup_database_local
    backup_database_local etherpad

    function_check nginx_ensite
    nginx_ensite $ETHERPAD_DOMAIN_NAME

    ETHERPAD_ONION_HOSTNAME=$(add_onion_service etherpad 80 ${ETHERPAD_ONION_PORT})

    ${PROJECT_NAME}-pass -u $MY_USERNAME -a etherpad -p "$ETHERPAD_ADMIN_PASSWORD"

    function_check add_ddns_domain
    add_ddns_domain $ETHERPAD_DOMAIN_NAME

    set_completion_param "etherpad domain" "$ETHERPAD_DOMAIN_NAME"

    systemctl enable etherpad
    systemctl daemon-reload
    systemctl start etherpad
    systemctl restart nginx

    APP_INSTALLED=1
}
