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

function interactive_configuration_remote_backups {
    if [[ $SYSTEM_TYPE == "mesh"* ]]; then
        return
    fi
    if [ ! -f /usr/local/bin/${PROJECT_NAME}-remote ]; then
        if [ ! -f /usr/bin/${PROJECT_NAME}-remote ]; then
            echo $"The command ${PROJECT_NAME}-remote was not found"
            exit 87354
        fi
    fi
    ${PROJECT_NAME}-remote -u $MY_USERNAME -l $FRIENDS_SERVERS_LIST -m $MINIMUM_PASSWORD_LENGTH -r yes
    if [ ! "$?" = "0" ]; then
        echo $'Command failed:'
        echo ''
        echo $"  ${PROJECT_NAME}-remote -u $MY_USERNAME -l $FRIENDS_SERVERS_LIST -m $MINIMUM_PASSWORD_LENGTH -r yes"
        echo ''
        exit 65892
    fi
}

function interactive_configuration {
    if [ ! -f /usr/local/bin/${PROJECT_NAME}-config ]; then
        if [ ! -f /usr/bin/${PROJECT_NAME}-config ]; then
            echo $"The command ${PROJECT_NAME}-config was not found"
            exit 63935
        fi
    fi
    if [ -f /tmp/meshuserdevice ]; then
        rm -f /tmp/meshuserdevice
    fi

    if [[ $ONION_ONLY == "no" ]]; then
        if [[ $MINIMAL_INSTALL == "no" ]]; then
            ${PROJECT_NAME}-config \
                           -f $CONFIGURATION_FILE \
                           -w $PROJECT_WEBSITE \
                           -m $MINIMUM_PASSWORD_LENGTH
        else
            ${PROJECT_NAME}-config \
                           -f $CONFIGURATION_FILE \
                           -w $PROJECT_WEBSITE \
                           -m $MINIMUM_PASSWORD_LENGTH \
                           --minimal "yes"
        fi
    else
        ${PROJECT_NAME}-config \
                       -f $CONFIGURATION_FILE \
                       -w $PROJECT_WEBSITE \
                       -m $MINIMUM_PASSWORD_LENGTH \
                       --onion "yes"
    fi
    if [ -f /tmp/meshuserdevice ]; then
        # mesh network user device installation
        rm -f /tmp/meshuserdevice
        exit 0
    fi
    if [ ! "$?" = "0" ]; then
        echo $'Command failed:'
        echo ''
        echo $"  ${PROJECT_NAME}-config -u $MY_USERNAME -f $CONFIGURATION_FILE -w $PROJECT_WEBSITE -b $PROJECT_BITMESSAGE -m $MINIMUM_PASSWORD_LENGTH --minimal [yes|no]"
        echo ''
        exit 73594
    fi

    if [[ $SYSTEM_TYPE == "mesh"* ]]; then
        FRIENDS_SERVERS_LIST=/home/$MY_USERNAME/backup.list

        dialog --title $"Encrypted backup to other servers" \
               --backtitle $"${PROJECT_NAME} Configuration" \
               --defaultno \
               --yesno $"\nDo you wish to configure some remote backup locations?" 7 60
        sel=$?
        case $sel in
            0) interactive_configuration_remote_backups;;
        esac
    fi
}

# NOTE: deliberately no exit 0
