| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659 | #!/bin/bash
#
# .---.                  .              .
# |                      |              |
# |--- .--. .-.  .-.  .-.|  .-. .--.--. |.-.  .-. .--.  .-.
# |    |   (.-' (.-' (   | (   )|  |  | |   )(   )|  | (.-'
# '    '     --'  --'  -' -  -' '  '   -' -'   -' '   -  --'
#
#                    Freedom in the Cloud
#
# Etherpad app
#
# License
# =======
#
# Copyright (C) 2016-2017 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 logging_on_etherpad {
    echo -n ''
}
function logging_off_etherpad {
    echo -n ''
}
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 -q "\"$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 {
    CURR_ETHERPAD_COMMIT=$(get_completion_param "etherpad commit")
    if [[ "$CURR_ETHERPAD_COMMIT" == "$ETHERPAD_COMMIT" ]]; then
        return
    fi
    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
        MARIADB_PASSWORD=$(${PROJECT_NAME}-pass -u root -a mariadb)
        settings_file=/var/www/${ETHERPAD_DOMAIN_NAME}/htdocs/settings.json
        sed -i "s|\"password\":.*|\"password\": \"${MARIADB_PASSWORD}\",|g" $settings_file
        MARIADB_PASSWORD=
    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
        MARIADB_PASSWORD=$(${PROJECT_NAME}-pass -u root -a mariadb)
        settings_file=/var/www/${ETHERPAD_DOMAIN_NAME}/htdocs/settings.json
        sed -i "s|\"password\":.*|\"password\": \"${MARIADB_PASSWORD}\",|g" $settings_file
        MARIADB_PASSWORD=
    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
    systemctl daemon-reload
    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
    remove_nodejs etherpad
    groupdel -f etherpad
    userdel -r 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
    check_ram_availability 2000
    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
        if [ -d /repos/etherpad ]; then
            mkdir /var/www/$ETHERPAD_DOMAIN_NAME/htdocs
            cp -r -p /repos/etherpad/. /var/www/$ETHERPAD_DOMAIN_NAME/htdocs
            cd /var/www/$ETHERPAD_DOMAIN_NAME/htdocs
            git pull
        else
            function_check git_clone
            git_clone $ETHERPAD_REPO /var/www/$ETHERPAD_DOMAIN_NAME/htdocs
        fi
        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 restart mariadb
    systemctl enable etherpad
    systemctl daemon-reload
    systemctl start etherpad
    systemctl restart nginx
    APP_INSTALLED=1
}
 |