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

VARIANTS='full writer'

RSS_READER_REPO="https://tt-rss.org/git/tt-rss.git"
RSS_READER_COMMIT='9e84bab449f9ed55e02e60378b0af1521026f3b8'
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='8b92b8f5db7b0d12459c7bd86a50f48815efe642'

function upgrade_rss {
	if grep -Fxq "install_rss" $COMPLETION_FILE; then
		function_check set_repo_commit
		set_repo_commit $RSS_READER_PATH "RSS reader commit" "$RSS_READER_COMMIT" $RSS_READER_REPO
	fi

	if grep -Fxq "install_rss_mobile_reader" $COMPLETION_FILE; 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 grep -Fxq "install_rss_mobile_reader" $COMPLETION_FILE; 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 {
	echo ''
}

function backup_remote_rss {
	echo ''
}

function remove_rss {
	if ! grep -Fxq "install_rss" $COMPLETION_FILE; 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
	sed -i '/install_rss/d' $COMPLETION_FILE
	sed -i '/RSS /d' $COMPLETION_FILE
}

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 {
	if [[ $SYSTEM_TYPE == "$VARIANT_MAILBOX" || $SYSTEM_TYPE == "$VARIANT_CHAT" || $SYSTEM_TYPE == "$VARIANT_MEDIA" || $SYSTEM_TYPE == "$VARIANT_DEVELOPER" || $SYSTEM_TYPE == "$VARIANT_MESH" ]]; then
		return
	fi

	if grep -Fxq "install_rss_main" $COMPLETION_FILE; then
		return
	fi

	apt-get -y install php-gettext php5-curl php5-gd php5-mysql git curl php-xml-parser

	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
	if ! grep -q "RSS reader commit" $COMPLETION_FILE; then
		echo "RSS reader commit:$RSS_READER_COMMIT" >> $COMPLETION_FILE
	fi

	function_check install_mariadb
	install_mariadb

	function_check get_mariadb_password
	get_mariadb_password

	function_check repair_databases_script
	repair_databases_script

	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

	function_check create_database
	create_database ttrss "$RSS_READER_ADMIN_PASSWORD" $MY_USERNAME

	RSS_READER_ONION_HOSTNAME=$(add_onion_service ttrss 80 ${RSS_READER_ONION_PORT})
	RSS_MOBILE_READER_ONION_HOSTNAME=$(add_onion_service ttrss 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

	if ! grep -q "RSS reader onion domain" $COMPLETION_FILE; then
		echo "RSS reader onion domain:${RSS_READER_ONION_HOSTNAME}" >> $COMPLETION_FILE
	fi
	if ! grep -q "RSS reader domain" $COMPLETION_FILE; then
		echo "RSS reader domain:${RSS_READER_DOMAIN_NAME}" >> $COMPLETION_FILE
	fi

	# 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 '==========' >> /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

	echo 'install_rss_main' >> $COMPLETION_FILE
}

function install_rss_gnusocial {
	if [[ $SYSTEM_TYPE == "$VARIANT_MAILBOX" || $SYSTEM_TYPE == "$VARIANT_CHAT" || $SYSTEM_TYPE == "$VARIANT_MEDIA" || $SYSTEM_TYPE == "$VARIANT_DEVELOPER" || $SYSTEM_TYPE == "$VARIANT_MESH" ]]; then
		return
	fi

	if [ ! $MICROBLOG_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 grep -Fxq "install_rss_gnusocial" $COMPLETION_FILE; 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 GNU Social 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
	if ! grep -q "RSS reader gnusocial commit" $COMPLETION_FILE; then
		echo "RSS reader gnusocial commit:$RSS_READER_GNUSOCIAL_COMMIT" >> $COMPLETION_FILE
	fi
	chown -R www-data:www-data $RSS_READER_GNUSOCIAL_PATH

	echo 'install_rss_gnusocial' >> $COMPLETION_FILE
}

function install_rss_mobile_reader {
	if [[ $SYSTEM_TYPE == "$VARIANT_MAILBOX" || $SYSTEM_TYPE == "$VARIANT_CHAT" || $SYSTEM_TYPE == "$VARIANT_MEDIA" || $SYSTEM_TYPE == "$VARIANT_DEVELOPER" || $SYSTEM_TYPE == "$VARIANT_MESH" ]]; then
		return
	fi

	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 grep -Fxq "install_rss_mobile_reader" $COMPLETION_FILE; 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 grep -Fxq "install_rss_mobile_reader" $COMPLETION_FILE; 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
	if ! grep -q "RSS mobile reader commit" $COMPLETION_FILE; then
		echo "RSS mobile reader commit:$RSS_MOBILE_READER_COMMIT" >> $COMPLETION_FILE
	fi

	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

	echo 'install_rss_mobile_reader' >> $COMPLETION_FILE
}

function install_rss {
	if grep -Fxq "install_rss" $COMPLETION_FILE; then
		return
	fi
	install_rss_main
	install_rss_gnusocial
	install_rss_mobile_reader
	echo 'install_rss' >> $COMPLETION_FILE
}

# NOTE: deliberately no exit 0
