#!/bin/bash
#
# .---.                  .              .
# |                      |              |
# |--- .--. .-.  .-.  .-.|  .-. .--.--. |.-.  .-. .--.  .-.
# |    |   (.-' (.-' (   | (   )|  |  | |   )(   )|  | (.-'
# '    '     --'  --'  -' -  -' '  '   -' -'   -' '   -  --'
#
#                    Freedom in the Cloud
#
# RSS reader 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

RSS_READER_REPO="https://tt-rss.org/git/tt-rss.git"
RSS_READER_COMMIT='48007463861d8db8b2b79c2f4f54e0564edb0ec0'
RSS_READER_ADMIN_PASSWORD=
RSS_READER_ONION_PORT=8092
RSS_MOBILE_READER_ONION_PORT=8095
RSS_READER_DOMAIN_NAME=ttrss
RSS_MOBILE_READER_REPO="https://github.com/mboinet/ttrss-mobile"
RSS_MOBILE_READER_COMMIT='d5242e1c269e9480ab3b66b7a38da3c892dfb326'
RSS_READER_PATH=/etc/share/tt-rss
RSS_READER_GNUSOCIAL_REPO="https://github.com/bashrc/ttrss-gnusocial"
RSS_READER_GNUSOCIAL_COMMIT='20b2535e3f2b0ddc0117b584bdcaa6bf7a2d9fa2'

rss_variables=(RSS_READER_REPO
               RSS_MOBILE_READER_REPO
               RSS_READER_COMMIT
               RSS_MOBILE_READER_COMMIT
               RSS_READER_ADMIN_PASSWORD
               RSS_READER_DOMAIN_NAME
               USB_MOUNT
               MY_USERNAME)

function install_interactive_rss {
    echo -n ''
    APP_INSTALLED=1
}

function change_password_rss {
    echo -n ''
}

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

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

    function_check create_database
    create_database ttrss "$RSS_READER_ADMIN_PASSWORD" $MY_USERNAME
}

function reconfigure_rss {
    echo -n ''
}

function upgrade_rss {
    if [[ $(app_is_installed rss) == "1" ]]; then
        function_check set_repo_commit
        set_repo_commit $RSS_READER_PATH "rss reader commit" "$RSS_READER_COMMIT" $RSS_READER_REPO
        if [[ $(commit_has_changed $RSS_READER_PATH "rss reader commit" "$RSS_READER_COMMIT") == "1" ]]; then
            function_check rss_modifications
            rss_modifications
        fi
    fi

    if [[ $(app_is_installed rss_mobile_reader) == "1" ]]; then
        if [ ! $RSS_READER_PATH ]; then
            RSS_READER_PATH=/etc/share/tt-rss
        fi

        if [ ! -d $RSS_READER_PATH ]; then
            echo $'tt-rss is not installed, so the mobile version cannot be installed'
            exit 63452
        fi

        RSS_MOBILE_READER_PATH=/etc/share/ttrss-mobile

        # remove any previous install
        if [ -d $RSS_READER_PATH/g2ttree-mobile ]; then
            if [[ $(is_completed "install_rss_mobile_reader") == "1" ]]; then
                sed -i '/install_rss_mobile_reader/d' $COMPLETION_FILE
                sed -i '/rss mobile reader commit/d' $COMPLETION_FILE
                rm -rf $RSS_READER_PATH/g2ttree-mobile
            fi
        fi

        # update to the next commit
        function_check set_repo_commit
        set_repo_commit $RSS_MOBILE_READER_PATH "rss mobile reader commit" "$RSS_MOBILE_READER_COMMIT" $RSS_MOBILE_READER_REPO
    fi
}

function backup_local_rss {
    if [ -d /etc/share/tt-rss ]; then
        RSS_READER_DOMAIN_NAME=$(get_completion_param "rss reader domain")

        echo $"Backing up ttrss"

        function_check backup_database_to_usb
        backup_database_to_usb ttrss

        backup_directory_to_usb /etc/share/tt-rss ttrss

        echo $"ttrss backup complete"
    fi
}

function restore_local_rss {
    temp_restore_dir=/root/tempttrss

    function_check rss_create_database
    rss_create_database

    restore_database ttrss ${RSS_READER_DOMAIN_NAME}

    if [ -d /etc/share/tt-rss ]; then
        if [ -d /root/tempttrss/etc/share/tt-rss ]; then
            rm -rf /etc/share/tt-rss
            mv /root/tempttrss/etc/share/tt-rss /etc/share/
            if [ ! "$?" = "0" ]; then
                function_check set_user_permissions
                set_user_permissions

                function_check backup_unmount_drive
                backup_unmount_drive
                exit 528823
            fi
            if [ -d /etc/letsencrypt/live/${RSS_READER_DOMAIN_NAME} ]; then
                ln -s /etc/letsencrypt/live/${RSS_READER_DOMAIN_NAME}/privkey.pem /etc/ssl/private/${2}.key
                ln -s /etc/letsencrypt/live/${RSS_READER_DOMAIN_NAME}/fullchain.pem /etc/ssl/certs/${2}.pem
            else
                # Ensure that the bundled SSL cert is being used
                if [ -f /etc/ssl/certs/${RSS_READER_DOMAIN_NAME}.bundle.crt ]; then
                    sed -i "s|${RSS_READER_DOMAIN_NAME}.crt|${RSS_READER_DOMAIN_NAME}.bundle.crt|g" /etc/nginx/sites-available/${2}
                fi
            fi
        fi
    fi

    function_check rss_modifications
    rss_modifications

    if [ -d $USB_MOUNT/backup/ttrss ]; then
        chown -R www-data:www-data /etc/share/tt-rss
        if [ -d $temp_restore_dir ]; then
            rm -rf $temp_restore_dir
        fi
    fi
}

function backup_remote_rss {
    if [ -d /etc/share/tt-rss ]; then
        function_check suspend_site
        suspend_site ${RSS_READER_DOMAIN_NAME}

        function_check backup_database_to_friend
        backup_database_to_friend ttrss

        echo $"Backing up RSS reader installation"

        function_check backup_directory_to_friend
        backup_directory_to_friend /etc/share/tt-rss ttrss

        function_check restart_site
        restart_site
    else
        echo $"RSS reader domain specified but not found in /etc/share/ttrss}"
    fi
}

function restore_remote_rss {
    if grep -q "rss reader domain" $COMPLETION_FILE; then
        echo $"Restoring ttrss"
        function_check restore_database_from_friend

        function_check rss_create_database
        rss_create_database

        restore_database_from_friend ttrss ${RSS_READER_DOMAIN_NAME}

        if [ -d /etc/share/tt-rss ]; then
            rm -rf /etc/share/tt-rss
            mv /root/tempttrss/etc/share/tt-rss /etc/share/
            if [ ! "$?" = "0" ]; then
                exit 6391
            fi
            if [ -d /etc/letsencrypt/live/${RSS_READER_DOMAIN_NAME} ]; then
                ln -s /etc/letsencrypt/live/${RSS_READER_DOMAIN_NAME}/privkey.pem /etc/ssl/private/${RSS_READER_DOMAIN_NAME}.key
                ln -s /etc/letsencrypt/live/${RSS_READER_DOMAIN_NAME}/fullchain.pem /etc/ssl/certs/${RSS_READER_DOMAIN_NAME}.pem
            else
                # Ensure that the bundled SSL cert is being used
                if [ -f /etc/ssl/certs/${RSS_READER_DOMAIN_NAME}.bundle.crt ]; then
                    sed -i "s|${RSS_READER_DOMAIN_NAME}.crt|${RSS_READER_DOMAIN_NAME}.bundle.crt|g" /etc/nginx/sites-available/${RSS_READER_DOMAIN_NAME}
                fi
            fi
        fi

        function_check rss_modifications
        rss_modifications

        if [ -d $SERVER_DIRECTORY/backup/ttrss ]; then
            chown -R www-data:www-data /etc/share/tt-rss
        fi
        if [ -d /root/tempttrss ]; then
            rm -rf /root/tempttrss
        fi
        echo $"Restore of ttrss complete"
    fi
}

function remove_rss {
    if [ ${#RSS_READER_DOMAIN_NAME} -eq 0 ]; then
        return
    fi

    nginx_dissite $RSS_READER_DOMAIN_NAME
    function_check remove_onion_service
    remove_onion_service ttrss ${RSS_READER_ONION_PORT} ${RSS_MOBILE_READER_ONION_PORT}
    if [ -f /etc/nginx/sites-available/$RSS_READER_DOMAIN_NAME ]; then
        rm /etc/nginx/sites-available/$RSS_READER_DOMAIN_NAME
    fi
    if [ -d $RSS_READER_PATH ]; then
        rm -rf $RSS_READER_PATH
    fi
    function_check drop_database
    drop_database ttrss
    remove_completion_param install_rss
    sed -i '/RSS /d' $COMPLETION_FILE
    sed -i '/rss /d' $COMPLETION_FILE
    sed -i '/RSS reader/d' /home/$MY_USERNAME/README
    remove_backup_database_local ttrss
}

function rss_modifications {
    # modify the rss reader to use a socks5 proxy rather than a http proxy
    if [ ! -d $RSS_READER_PATH ]; then
        return
    fi

    # ensure that socks5 proxy is used
    if ! grep -q "CURLOPT_PROXYTYPE" $RSS_READER_PATH/plugins/af_unburn/init.php; then
        sed -i '/curl_setopt($ch, CURLOPT_PROXY, _CURL_HTTP_PROXY);/a \\t\t\t\t\tcurl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);' $RSS_READER_PATH/plugins/af_unburn/init.php
    fi
    if ! grep -q "CURLOPT_PROXYTYPE" $RSS_READER_PATH/include/functions.php; then
        sed -i '/curl_setopt($ch, CURLOPT_PROXY, _CURL_HTTP_PROXY);/a \\t\t\t\tcurl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);' $RSS_READER_PATH/include/functions.php
    fi
    chown -R www-data:www-data $RSS_READER_PATH
    chmod a+x $RSS_READER_PATH
}

function install_rss_main {
    apt-get -yq install php-gettext php5-curl php5-gd php5-mysql git
    apt-get -yq install curl php-xml-parser php5-mcrypt

    if [ ! -d /etc/share ]; then
        mkdir /etc/share
    fi
    cd /etc/share
    function_check git_clone
    git_clone $RSS_READER_REPO tt-rss
    if [ ! -d $RSS_READER_PATH ]; then
        echo $'Could not clone RSS reader repo'
        exit 52925
    fi
    cd $RSS_READER_PATH
    git checkout $RSS_READER_COMMIT -b $RSS_READER_COMMIT
    set_completion_param "rss reader commit" "$RSS_READER_COMMIT"

    function_check install_mariadb
    install_mariadb

    function_check get_mariadb_password
    get_mariadb_password

    function_check repair_databases_script
    repair_databases_script

    rss_create_database

    RSS_READER_ONION_HOSTNAME=$(add_onion_service rss 80 ${RSS_READER_ONION_PORT})
    RSS_MOBILE_READER_ONION_HOSTNAME=$(add_onion_service rss_mobile 80 ${RSS_MOBILE_READER_ONION_PORT})

    echo 'server {' > /etc/nginx/sites-available/$RSS_READER_DOMAIN_NAME
    echo "  listen 127.0.0.1:$RSS_MOBILE_READER_ONION_PORT;" >> /etc/nginx/sites-available/$RSS_READER_DOMAIN_NAME
    echo "  server_name $RSS_MOBILE_READER_ONION_HOSTNAME;" >> /etc/nginx/sites-available/$RSS_READER_DOMAIN_NAME
    echo '' >> /etc/nginx/sites-available/$RSS_READER_DOMAIN_NAME
    echo '  access_log off;' >> /etc/nginx/sites-available/$RSS_READER_DOMAIN_NAME
    echo '  error_log off;' >> /etc/nginx/sites-available/$RSS_READER_DOMAIN_NAME
    echo '' >> /etc/nginx/sites-available/$RSS_READER_DOMAIN_NAME
    echo '  root /etc/share/ttrss-mobile;' >> /etc/nginx/sites-available/$RSS_READER_DOMAIN_NAME
    echo '  index index.html index.php;' >> /etc/nginx/sites-available/$RSS_READER_DOMAIN_NAME
    echo '' >> /etc/nginx/sites-available/$RSS_READER_DOMAIN_NAME
    echo '  location ~ \.php {' >> /etc/nginx/sites-available/$RSS_READER_DOMAIN_NAME
    echo '    include snippets/fastcgi-php.conf;' >> /etc/nginx/sites-available/$RSS_READER_DOMAIN_NAME
    echo '    fastcgi_pass unix:/var/run/php5-fpm.sock;' >> /etc/nginx/sites-available/$RSS_READER_DOMAIN_NAME
    echo '  }' >> /etc/nginx/sites-available/$RSS_READER_DOMAIN_NAME
    echo '' >> /etc/nginx/sites-available/$RSS_READER_DOMAIN_NAME
    echo '  location / {' >> /etc/nginx/sites-available/$RSS_READER_DOMAIN_NAME
    echo '    try_files $uri $uri/ @ttrss;' >> /etc/nginx/sites-available/$RSS_READER_DOMAIN_NAME
    echo '  }' >> /etc/nginx/sites-available/$RSS_READER_DOMAIN_NAME
    echo '  location /tt-rss {' >> /etc/nginx/sites-available/$RSS_READER_DOMAIN_NAME
    echo '    try_files $uri $uri/ @ttrss_base;' >> /etc/nginx/sites-available/$RSS_READER_DOMAIN_NAME
    echo '  }' >> /etc/nginx/sites-available/$RSS_READER_DOMAIN_NAME
    echo '' >> /etc/nginx/sites-available/$RSS_READER_DOMAIN_NAME
    echo '  location @ttrss {' >> /etc/nginx/sites-available/$RSS_READER_DOMAIN_NAME
    echo '    rewrite ^(.*)$ /index.html?p=$1 last;' >> /etc/nginx/sites-available/$RSS_READER_DOMAIN_NAME
    echo '  }' >> /etc/nginx/sites-available/$RSS_READER_DOMAIN_NAME
    echo '  location @ttrss_base {' >> /etc/nginx/sites-available/$RSS_READER_DOMAIN_NAME
    echo '    rewrite ^(.*)$ /index.php?p=$1 last;' >> /etc/nginx/sites-available/$RSS_READER_DOMAIN_NAME
    echo '  }' >> /etc/nginx/sites-available/$RSS_READER_DOMAIN_NAME
    echo '' >> /etc/nginx/sites-available/$RSS_READER_DOMAIN_NAME
    echo '  location ~ /\.(git) {' >> /etc/nginx/sites-available/$RSS_READER_DOMAIN_NAME
    echo '    deny all;' >> /etc/nginx/sites-available/$RSS_READER_DOMAIN_NAME
    echo '  }' >> /etc/nginx/sites-available/$RSS_READER_DOMAIN_NAME
    echo '' >> /etc/nginx/sites-available/$RSS_READER_DOMAIN_NAME
    echo '  add_header X-Frame-Options DENY;' >> /etc/nginx/sites-available/$RSS_READER_DOMAIN_NAME
    echo '  add_header X-Content-Type-Options nosniff;' >> /etc/nginx/sites-available/$RSS_READER_DOMAIN_NAME
    echo '  client_max_body_size 15m;' >> /etc/nginx/sites-available/$RSS_READER_DOMAIN_NAME
    echo '}' >> /etc/nginx/sites-available/$RSS_READER_DOMAIN_NAME
    echo '' >> /etc/nginx/sites-available/$RSS_READER_DOMAIN_NAME
    echo 'server {' >> /etc/nginx/sites-available/$RSS_READER_DOMAIN_NAME
    echo "  listen 127.0.0.1:$RSS_READER_ONION_PORT default_server;" >> /etc/nginx/sites-available/$RSS_READER_DOMAIN_NAME
    echo "  server_name $RSS_READER_ONION_HOSTNAME;" >> /etc/nginx/sites-available/$RSS_READER_DOMAIN_NAME
    echo '' >> /etc/nginx/sites-available/$RSS_READER_DOMAIN_NAME
    echo '  access_log off;' >> /etc/nginx/sites-available/$RSS_READER_DOMAIN_NAME
    echo '  error_log off;' >> /etc/nginx/sites-available/$RSS_READER_DOMAIN_NAME
    echo '' >> /etc/nginx/sites-available/$RSS_READER_DOMAIN_NAME
    echo '  root /etc/share/tt-rss;' >> /etc/nginx/sites-available/$RSS_READER_DOMAIN_NAME
    echo '  index index.php;' >> /etc/nginx/sites-available/$RSS_READER_DOMAIN_NAME
    echo '' >> /etc/nginx/sites-available/$RSS_READER_DOMAIN_NAME
    echo '  location ~ \.php {' >> /etc/nginx/sites-available/$RSS_READER_DOMAIN_NAME
    echo '    include snippets/fastcgi-php.conf;' >> /etc/nginx/sites-available/$RSS_READER_DOMAIN_NAME
    echo '    fastcgi_pass unix:/var/run/php5-fpm.sock;' >> /etc/nginx/sites-available/$RSS_READER_DOMAIN_NAME
    echo '  }' >> /etc/nginx/sites-available/$RSS_READER_DOMAIN_NAME
    echo '' >> /etc/nginx/sites-available/$RSS_READER_DOMAIN_NAME
    echo '  set $mobile_rewrite do_not_perform;' >> /etc/nginx/sites-available/$RSS_READER_DOMAIN_NAME
    echo '' >> /etc/nginx/sites-available/$RSS_READER_DOMAIN_NAME
    echo '  ## chi http_user_agent for mobile / smart phones ##' >> /etc/nginx/sites-available/$RSS_READER_DOMAIN_NAME
    echo '  if ($http_user_agent ~* "(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows (ce|phone)|xda|xiino") {' >> /etc/nginx/sites-available/$RSS_READER_DOMAIN_NAME
    echo '    set $mobile_rewrite perform;' >> /etc/nginx/sites-available/$RSS_READER_DOMAIN_NAME
    echo '  }' >> /etc/nginx/sites-available/$RSS_READER_DOMAIN_NAME
    echo '' >> /etc/nginx/sites-available/$RSS_READER_DOMAIN_NAME
    echo '  if ($http_user_agent ~* "^(1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1 u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp( i|ip)|hs\-c|ht(c(\-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac( |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc\-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas\-|your|zeto|zte\-)") {' >> /etc/nginx/sites-available/$RSS_READER_DOMAIN_NAME
    echo '    set $mobile_rewrite perform;' >> /etc/nginx/sites-available/$RSS_READER_DOMAIN_NAME
    echo '  }' >> /etc/nginx/sites-available/$RSS_READER_DOMAIN_NAME
    echo '' >> /etc/nginx/sites-available/$RSS_READER_DOMAIN_NAME
    echo '  if ($mobile_rewrite = perform) {' >> /etc/nginx/sites-available/$RSS_READER_DOMAIN_NAME
    echo "    rewrite ^/(.*) http://$RSS_MOBILE_READER_ONION_HOSTNAME permanent;" >> /etc/nginx/sites-available/$RSS_READER_DOMAIN_NAME
    echo '    break;' >> /etc/nginx/sites-available/$RSS_READER_DOMAIN_NAME
    echo '  }' >> /etc/nginx/sites-available/$RSS_READER_DOMAIN_NAME
    echo '' >> /etc/nginx/sites-available/$RSS_READER_DOMAIN_NAME
    echo '  location ~ \.php {' >> /etc/nginx/sites-available/$RSS_READER_DOMAIN_NAME
    echo '    include snippets/fastcgi-php.conf;' >> /etc/nginx/sites-available/$RSS_READER_DOMAIN_NAME
    echo '    fastcgi_pass unix:/var/run/php5-fpm.sock;' >> /etc/nginx/sites-available/$RSS_READER_DOMAIN_NAME
    echo '  }' >> /etc/nginx/sites-available/$RSS_READER_DOMAIN_NAME
    echo '' >> /etc/nginx/sites-available/$RSS_READER_DOMAIN_NAME
    echo '  location / {' >> /etc/nginx/sites-available/$RSS_READER_DOMAIN_NAME
    echo '    try_files $uri $uri/ @ttrss;' >> /etc/nginx/sites-available/$RSS_READER_DOMAIN_NAME
    echo '  }' >> /etc/nginx/sites-available/$RSS_READER_DOMAIN_NAME
    echo '' >> /etc/nginx/sites-available/$RSS_READER_DOMAIN_NAME
    echo '  location @ttrss {' >> /etc/nginx/sites-available/$RSS_READER_DOMAIN_NAME
    echo '    rewrite ^(.*)$ /index.php?p=$1 last;' >> /etc/nginx/sites-available/$RSS_READER_DOMAIN_NAME
    echo '  }' >> /etc/nginx/sites-available/$RSS_READER_DOMAIN_NAME
    echo '' >> /etc/nginx/sites-available/$RSS_READER_DOMAIN_NAME
    echo '  location ~ /\.(git) {' >> /etc/nginx/sites-available/$RSS_READER_DOMAIN_NAME
    echo '    deny all;' >> /etc/nginx/sites-available/$RSS_READER_DOMAIN_NAME
    echo '  }' >> /etc/nginx/sites-available/$RSS_READER_DOMAIN_NAME
    echo '' >> /etc/nginx/sites-available/$RSS_READER_DOMAIN_NAME
    echo '  add_header X-Frame-Options DENY;' >> /etc/nginx/sites-available/$RSS_READER_DOMAIN_NAME
    echo '  add_header X-Content-Type-Options nosniff;' >> /etc/nginx/sites-available/$RSS_READER_DOMAIN_NAME
    echo '  client_max_body_size 15m;' >> /etc/nginx/sites-available/$RSS_READER_DOMAIN_NAME
    echo '}' >> /etc/nginx/sites-available/$RSS_READER_DOMAIN_NAME

    if [ ! -f $RSS_READER_PATH/config.php ]; then
        # generate a config file
        RSS_FEED_CRYPT_KEY="$(create_password 24)"
        echo '<?php' > $RSS_READER_PATH/config.php
        echo "  define ('_CURL_HTTP_PROXY', '127.0.0.1:9050');" >> $RSS_READER_PATH/config.php
        echo "  define('DB_TYPE', 'mysql');" >> $RSS_READER_PATH/config.php
        echo "  define('DB_HOST', 'localhost');" >> $RSS_READER_PATH/config.php
        echo "  define('DB_USER', 'root');" >> $RSS_READER_PATH/config.php
        echo "  define('DB_NAME', 'ttrss');" >> $RSS_READER_PATH/config.php
        echo "  define('DB_PASS', '${MARIADB_PASSWORD}');" >> $RSS_READER_PATH/config.php
        echo "  define('DB_PORT', '3306');" >> $RSS_READER_PATH/config.php
        echo "  define('MYSQL_CHARSET', 'UTF8');" >> $RSS_READER_PATH/config.php
        echo "  define('SELF_URL_PATH', 'http://${RSS_READER_ONION_HOSTNAME}/');" >> $RSS_READER_PATH/config.php
        echo "  define('FEED_CRYPT_KEY', '${RSS_FEED_CRYPT_KEY}');" >> $RSS_READER_PATH/config.php
        echo "  define('SINGLE_USER_MODE', false);" >> $RSS_READER_PATH/config.php
        echo "  define('SIMPLE_UPDATE_MODE', false);" >> $RSS_READER_PATH/config.php
        echo "  define('PHP_EXECUTABLE', '/usr/bin/php');" >> $RSS_READER_PATH/config.php
        echo "  define('LOCK_DIRECTORY', 'lock');" >> $RSS_READER_PATH/config.php
        echo "  define('CACHE_DIR', 'cache');" >> $RSS_READER_PATH/config.php
        echo "  define('ICONS_DIR', \"feed-icons\");" >> $RSS_READER_PATH/config.php
        echo "  define('ICONS_URL', \"feed-icons\");" >> $RSS_READER_PATH/config.php
        echo "  define('AUTH_AUTO_CREATE', true);" >> $RSS_READER_PATH/config.php
        echo "  define('AUTH_AUTO_LOGIN', true);" >> $RSS_READER_PATH/config.php
        echo "  define('FORCE_ARTICLE_PURGE', 0);" >> $RSS_READER_PATH/config.php
        echo "  define('PUBSUBHUBBUB_HUB', '');" >> $RSS_READER_PATH/config.php
        echo "  define('PUBSUBHUBBUB_ENABLED', false);" >> $RSS_READER_PATH/config.php
        echo "  define('SPHINX_SERVER', 'localhost:9312');" >> $RSS_READER_PATH/config.php
        echo "  define('SPHINX_INDEX', 'ttrss, delta');" >> $RSS_READER_PATH/config.php
        echo "  define('ENABLE_REGISTRATION', false);" >> $RSS_READER_PATH/config.php
        echo "  define('REG_NOTIFY_ADDRESS', '${MY_EMAIL_ADDRESS}');" >> $RSS_READER_PATH/config.php
        echo "  define('REG_MAX_USERS', 10);" >> $RSS_READER_PATH/config.php
        echo "  define('SESSION_COOKIE_LIFETIME', 86400);" >> $RSS_READER_PATH/config.php
        echo "  define('SMTP_FROM_NAME', 'Tiny Tiny RSS');" >> $RSS_READER_PATH/config.php
        echo "  define('SMTP_FROM_ADDRESS', 'noreply@${RSS_READER_ONION_HOSTNAME}');" >> $RSS_READER_PATH/config.php
        echo "  define('DIGEST_SUBJECT', '[tt-rss] New headlines for last 24 hours');" >> $RSS_READER_PATH/config.php
        echo "  define('SMTP_SERVER', '');" >> $RSS_READER_PATH/config.php
        echo "  define('SMTP_LOGIN', '');" >> $RSS_READER_PATH/config.php
        echo "  define('SMTP_PASSWORD', '');" >> $RSS_READER_PATH/config.php
        echo "  define('SMTP_SECURE', '');" >> $RSS_READER_PATH/config.php
        echo "  define('CHECK_FOR_UPDATES', false);" >> $RSS_READER_PATH/config.php
        echo "  define('ENABLE_GZIP_OUTPUT', false);" >> $RSS_READER_PATH/config.php
        echo "  define('PLUGINS', 'auth_internal, note, gnusocial');" >> $RSS_READER_PATH/config.php
        echo "  define('LOG_DESTINATION', 'sql');" >> $RSS_READER_PATH/config.php
        echo "  define('CONFIG_VERSION', 26);" >> $RSS_READER_PATH/config.php
    fi

    # initialize the database
    if [ ! -f $RSS_READER_PATH/schema/ttrss_schema_mysql.sql ]; then
        echo $'No database schema found for rss reader'
        exit 52926
    fi
    initialise_database ttrss $RSS_READER_PATH/schema/ttrss_schema_mysql.sql

    # change the password from the default
    RSS_READER_ADMIN_PASSWORD_HASH=$(echo -n "${RSS_READER_ADMIN_PASSWORD}" | sha1sum | awk -F ' ' '{print $1}')

    function_check run_query
    run_query ttrss "update ttrss_users set pwd_hash = 'SHA1:${RSS_READER_ADMIN_PASSWORD_HASH}', salt= '' WHERE login = 'admin';"

    function_check rss_modifications
    rss_modifications

    function_check configure_php
    configure_php

    nginx_ensite $RSS_READER_DOMAIN_NAME
    systemctl restart php5-fpm
    systemctl restart nginx

    # daemon to update feeds
    echo '[Unit]' > /etc/systemd/system/ttrss.service
    echo 'Description=ttrss_backend' >> /etc/systemd/system/ttrss.service
    echo 'After=network.target mysql.service' >> /etc/systemd/system/ttrss.service
    echo 'After=tor.service' >> /etc/systemd/system/ttrss.service
    echo '' >> /etc/systemd/system/ttrss.service
    echo '[Service]' >> /etc/systemd/system/ttrss.service
    echo 'User=www-data' >> /etc/systemd/system/ttrss.service
    echo "ExecStart=/usr/bin/php $RSS_READER_PATH/update.php --daemon" >> /etc/systemd/system/ttrss.service
    echo '' >> /etc/systemd/system/ttrss.service
    echo '[Install]' >> /etc/systemd/system/ttrss.service
    echo 'WantedBy=multi-user.target' >> /etc/systemd/system/ttrss.service
    systemctl enable ttrss
    systemctl daemon-reload
    systemctl start ttrss

    # some post-install instructions for the user
    if ! grep -q $"RSS Reader" /home/$MY_USERNAME/README; then
        echo '' >> /home/$MY_USERNAME/README
        echo '' >> /home/$MY_USERNAME/README
        echo $'# RSS Reader' >> /home/$MY_USERNAME/README
        echo $"RSS reader domain: ${RSS_READER_DOMAIN_NAME}" >> /home/$MY_USERNAME/README
        echo $"RSS reader onion domain: ${RSS_READER_ONION_HOSTNAME}" >> /home/$MY_USERNAME/README
        echo $"RSS reader admin username: admin" >> /home/$MY_USERNAME/README
        echo $"RSS reader admin password: ${RSS_READER_ADMIN_PASSWORD}" >> /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

    install_completed rss_main
}

function install_rss_gnusocial {
    if [ ! $GNUSOCIAL_DOMAIN_NAME ]; then
        return
    fi

    if [ ! $RSS_READER_PATH ]; then
        RSS_READER_PATH=/etc/share/tt-rss
    fi
    RSS_READER_GNUSOCIAL_PATH=${RSS_READER_PATH}/plugins/gnusocial

    # update to the next commit
    function_check set_repo_commit
    set_repo_commit $RSS_READER_GNUSOCIAL_PATH "rss reader gnusocial commit" "$RSS_READER_GNUSOCIAL_COMMIT" $RSS_READER_GNUSOCIAL_REPO
    chown -R www-data:www-data $RSS_READER_GNUSOCIAL_PATH

    if [[ $(is_completed $FUNCNAME) == "1" ]]; then
        return
    fi

    if [ ! -d $INSTALL_DIR ]; then
        mkdir -p $INSTALL_DIR
    fi

    cd $INSTALL_DIR
    function_check git_clone
    git_clone $RSS_READER_GNUSOCIAL_REPO ttrss-gnusocial
    if [ ! -d $INSTALL_DIR/ttrss-gnusocial ]; then
        echo $'Could not clone repo for RSS reader gnusocial plugin'
        exit 52838
    fi
    cp -r $INSTALL_DIR/ttrss-gnusocial $RSS_READER_GNUSOCIAL_PATH
    cd $RSS_READER_GNUSOCIAL_PATH
    git checkout $RSS_READER_GNUSOCIAL_COMMIT -b $RSS_READER_GNUSOCIAL_COMMIT
    set_completion_param "rss reader gnusocial commit" "$RSS_READER_GNUSOCIAL_COMMIT"
    chown -R www-data:www-data $RSS_READER_GNUSOCIAL_PATH
    install_completed rss_gnusocial
}

function install_rss_mobile_reader {
    if [ ! $RSS_READER_PATH ]; then
        RSS_READER_PATH=/etc/share/tt-rss
    fi

    if [ ! -d $RSS_READER_PATH ]; then
        echo $'tt-rss is not installed, so the mobile version cannot be installed'
        exit 63452
    fi

    RSS_MOBILE_READER_PATH=/etc/share/ttrss-mobile

    # remove any previous install
    if [ -d $RSS_READER_PATH/g2ttree-mobile ]; then
        if [[ $(is_completed "install_rss_mobile_reader") == "1" ]]; then
            sed -i '/install_rss_mobile_reader/d' $COMPLETION_FILE
            sed -i '/rss mobile reader commit/d' $COMPLETION_FILE
            rm -rf $RSS_READER_PATH/g2ttree-mobile
        fi
    fi

    if [[ $(is_completed $FUNCNAME) == "1" ]]; then
        return
    fi

    cd /etc/share
    function_check git_clone
    git_clone $RSS_MOBILE_READER_REPO ttrss-mobile
    if [ ! -d $RSS_MOBILE_READER_PATH ]; then
        echo $'Could not clone RSS mobile reader repo'
        exit 24816
    fi
    cd $RSS_MOBILE_READER_PATH
    git checkout $RSS_MOBILE_READER_COMMIT -b $RSS_MOBILE_READER_COMMIT
    set_completion_param "rss mobile reader commit" "$RSS_MOBILE_READER_COMMIT"

    echo 'define({' > $RSS_MOBILE_READER_PATH/scripts/conf.js
    echo '  apiPath: "/tt-rss/"' >> $RSS_MOBILE_READER_PATH/scripts/conf.js
    echo '});' >> $RSS_MOBILE_READER_PATH/scripts/conf.js

    # link to the main site
    ln -s $RSS_READER_PATH $RSS_MOBILE_READER_PATH/tt-rss

    chown -R www-data:www-data $RSS_MOBILE_READER_PATH
    chown -R www-data:www-data $RSS_READER_PATH
    chmod a+x $RSS_MOBILE_READER_PATH
    install_completed rss_mobile_reader
}

function install_rss {
    install_rss_main
    install_rss_gnusocial
    install_rss_mobile_reader
    APP_INSTALLED=1
}

# NOTE: deliberately no exit 0
