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

OPEN_PORTS=()

function install_final {
	if grep -Fxq "install_final" $COMPLETION_FILE; then
		return
	fi
	# unmount any attached usb drive
	if [ -d $USB_MOUNT ]; then
		umount $USB_MOUNT
		rm -rf $USB_MOUNT
	fi
	function_check split_gpg_key_into_fragments
	split_gpg_key_into_fragments
	echo 'install_final' >> $COMPLETION_FILE
	clear
	echo ''

	echo $"
	*** ${PROJECT_NAME} installation is complete. Rebooting... ***

	Now forward these ports from your internet router
"
	for p in "${OPEN_PORTS[@]}"
	do
		echo "  $p"
	done
	echo ''

	if [ -f "/home/$MY_USERNAME/README" ]; then
		echo $"See /home/$MY_USERNAME/README for post-installation instructions."
		echo ''
	fi
	# add user menu on ssh login
	if ! grep -q 'control' /home/$MY_USERNAME/.bashrc; then
		echo 'control' >> /home/$MY_USERNAME/.bashrc
	fi
	if [ ! -f $IMAGE_PASSWORD_FILE ]; then
		reboot
	fi
}

function setup_final {
	function_check install_tripwire
	install_tripwire

	function_check install_final
	install_final
}

# NOTE: deliberately no exit 0
