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

SYNCTHING_ID=
SYNCTHING_CONFIG_PATH=/root/.config/syncthing
SYNCTHING_CONFIG_FILE=$SYNCTHING_CONFIG_PATH/config.xml
SYNCTHING_RELAY_SERVER='https://relays.syncthing.net/endpoint'
SYNCTHING_RELEASES='https://api.github.com/repos/syncthing/syncthing/releases?per_page=30'
SYNCTHING_PORT=22000
SYNCTHING_SHARED_DATA=/var/lib/syncthing/SyncShared
SYNCTHING_USER_IDS_FILE='.syncthingids'

function upgrade_syncthing {
    echo ''
}

function backup_local_syncthing {
    echo ''
}

function backup_remote_syncthing {
    echo ''
}

function remove_syncthing {
	if ! grep -Fxq "install_syncthing" $COMPLETION_FILE; then
		return
	fi
	iptables -D INPUT -p udp --dport $SYNCTHING_PORT -j ACCEPT
	iptables -D INPUT -p tcp --dport $SYNCTHING_PORT -j ACCEPT
	function_check save_firewall_settings
	save_firewall_settings

	systemctl stop syncthing
	systemctl disable syncthing
	apt-get -y remove --purge syncthing
	rm /etc/systemd/system/syncthing.service
	sed -i "/${PROJECT_NAME}-syncthing/d" /etc/crontab
	sed -i '/install_syncthing/d' $COMPLETION_FILE
	sed -i '/configure_firewall_for_syncthing/d' $COMPLETION_FILE
	systemctl restart cron
}

function configure_firewall_for_syncthing {
	if grep -Fxq "configure_firewall_for_syncthing" $COMPLETION_FILE; then
		return
	fi
	iptables -A INPUT -p udp --dport $SYNCTHING_PORT -j ACCEPT
	iptables -A INPUT -p tcp --dport $SYNCTHING_PORT -j ACCEPT
	function_check save_firewall_settings
	save_firewall_settings

	OPEN_PORTS+=("Syncthing $SYNCTHING_PORT")
	echo 'configure_firewall_for_syncthing' >> $COMPLETION_FILE
}

function install_syncthing {
	if [[ $SYSTEM_TYPE == "$VARIANT_WRITER" || $SYSTEM_TYPE == "$VARIANT_MAILBOX" || $SYSTEM_TYPE == "$VARIANT_CHAT" || $SYSTEM_TYPE == "$VARIANT_SOCIAL" || $SYSTEM_TYPE == "$VARIANT_MEDIA" || $SYSTEM_TYPE == "$VARIANT_DEVELOPER" || $SYSTEM_TYPE == "$VARIANT_MESH" ]]; then
		return
	fi
	if grep -Fxq "install_syncthing" $COMPLETION_FILE; then
		return
	fi

	apt-get -y install curl

	curl -s https://syncthing.net/release-key.txt | apt-key add -
	echo "deb http://apt.syncthing.net/ syncthing release" | tee /etc/apt/sources.list.d/syncthing.list
	apt-get update
	apt-get -y install syncthing

	# This probably does need to run as root so that it can access the Sync directories
	# in each user's home directory
	echo '[Unit]' > /etc/systemd/system/syncthing.service
	echo 'Description=Syncthing - Open Source Continuous File Synchronization' >> /etc/systemd/system/syncthing.service
	echo 'Documentation=man:syncthing(1)' >> /etc/systemd/system/syncthing.service
	echo 'After=network.target' >> /etc/systemd/system/syncthing.service
	echo 'Wants=syncthing-inotify@.service' >> /etc/systemd/system/syncthing.service
	echo '' >> /etc/systemd/system/syncthing.service
	echo '[Service]' >> /etc/systemd/system/syncthing.service
	echo 'User=root' >> /etc/systemd/system/syncthing.service
	echo "Environment='all_proxy=socks5://localhost:9050'" >> /etc/systemd/system/syncthing.service
	echo 'ExecStart=/usr/bin/syncthing -no-browser -no-restart -logflags=0' >> /etc/systemd/system/syncthing.service
	echo 'Restart=on-failure' >> /etc/systemd/system/syncthing.service
	echo 'SuccessExitStatus=3 4' >> /etc/systemd/system/syncthing.service
	echo 'RestartForceExitStatus=3 4' >> /etc/systemd/system/syncthing.service
	echo '' >> /etc/systemd/system/syncthing.service
	echo '[Install]' >> /etc/systemd/system/syncthing.service
	echo 'WantedBy=multi-user.target' >> /etc/systemd/system/syncthing.service
	systemctl enable syncthing
	systemctl daemon-reload
	systemctl start syncthing

	function_check cron_add_mins
	cron_add_mins 1 "/usr/local/bin/${PROJECT_NAME}-syncthing > /dev/null"

	function_check configure_firewall_for_syncthing
	configure_firewall_for_syncthing
	echo 'install_syncthing' >> $COMPLETION_FILE
}

# NOTE: deliberately no exit 0
