| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267 | #!/bin/bash
#
# .---.                  .              .
# |                      |              |
# |--- .--. .-.  .-.  .-.|  .-. .--.--. |.-.  .-. .--.  .-.
# |    |   (.-' (.-' (   | (   )|  |  | |   )(   )|  | (.-'
# '    '     --'  --'  -' -  -' '  '   -' -'   -' '   -  --'
#
#                    Freedom in the Cloud
#
# Add or remove apps
#
# License
# =======
#
# Copyright (C) 2015-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/>.
PROJECT_NAME='freedombone'
export TEXTDOMAIN=${PROJECT_NAME}-addremove
export TEXTDOMAINDIR="/usr/share/locale"
PROJECT_INSTALL_DIR=/usr/local/bin
if [ -f /usr/bin/${PROJECT_NAME} ]; then
    PROJECT_INSTALL_DIR=/usr/bin
fi
COMPLETION_FILE=$HOME/${PROJECT_NAME}-completed.txt
CONFIGURATION_FILE=$HOME/${PROJECT_NAME}.cfg
# Start including files
source $PROJECT_INSTALL_DIR/${PROJECT_NAME}-vars
UTILS_FILES=/usr/share/${PROJECT_NAME}/utils/${PROJECT_NAME}-utils-*
for f in $UTILS_FILES
do
  source $f
done
APP_FILES=/usr/share/${PROJECT_NAME}/apps/${PROJECT_NAME}-app-*
for f in $APP_FILES
do
  source $f
done
# End including files
function mark_unselected_apps_as_removed {
    # Initially mark the apps not chosen on first install as being removed
    # otherwise they may be automatically installed on the next update
    select_all_apps=$1
    if [[ "$select_all_apps" != "add-all" ]]; then
        return
    fi
    if [ -f $REMOVED_APPS_FILE ]; then
        rm $REMOVED_APPS_FILE
    fi
    app_index=0
    for app_name in "${APPS_AVAILABLE[@]}"
    do
        if [[ ${APPS_CHOSEN[$app_index]} == "0" ]]; then
            echo "_${app_name}_" >> $REMOVED_APPS_FILE
        fi
        app_index=$[app_index+1]
    done
}
function app_expected_to_be_installed {
    # is the given application expected to be installed by default?
    select_all_apps="$1"
    app_name="$2"
    read_config_param ONION_ONLY
    if [[ "$select_all_apps" == "add-all" ]]; then
        if [[ $ONION_ONLY != 'no' && "$app_name" == "hubzilla"  ]]; then
            echo "0"
            return
        fi
        if ! grep -q "IN_DEFAULT_INSTALL=1" /usr/share/${PROJECT_NAME}/apps/${PROJECT_NAME}-app-${app_name}; then
            echo "0"
            return
        fi
    fi
    echo "1"
}
function show_apps {
    select_all_apps=$1
    applist=""
    n=1
    app_index=0
    for a in "${APPS_AVAILABLE[@]}"
    do
        if [[ ${APPS_INSTALLED[$app_index]} == "0" &&  "$select_all_apps" != "add-all" ]]; then
            applist="$applist $n $a off"
        else
            if [[ $(app_expected_to_be_installed "$select_all_apps" "$a") == "0" ]]; then
                applist="$applist $n $a off"
            else
                applist="$applist $n $a on"
            fi
        fi
        n=$[n+1]
        app_index=$[app_index+1]
    done
    choices=$(dialog --stdout --backtitle $"Freedombone" \
                     --title $"Add/Remove Applications" \
                     --checklist $'Choose:' \
                     27 40 20 $applist)
    if [ $? -eq 0 ]; then
        for choice in $choices
        do
            app_index=$[choice-1]
            APPS_CHOSEN[$app_index]="1"
        done
    else
        exit 0
    fi
}
function remove_apps_selected {
    # which apps need to be removed?
    removals=""
    app_index=0
    n=0
    for a in "${APPS_INSTALLED[@]}"
    do
        if [[ ${APPS_INSTALLED[$app_index]} == "1" ]]; then
            if [[ ${APPS_CHOSEN[$app_index]} == "0" ]]; then
                if [ ${n} -gt 0 ]; then
                    removals="$removals ${APPS_AVAILABLE[$app_index]}"
                else
                    removals="${APPS_AVAILABLE[$app_index]}"
                fi
                n=$[n+1]
            fi
        fi
        app_index=$[app_index+1]
    done
    # if no apps to be removed then don't do anything
    if [ ${n} -eq 0 ]; then
        return
    fi
    # ask for confirmation
    dialog --title $"Remove applications" \
           --backtitle $"Freedombone" \
           --defaultno \
           --yesno $"\nYou have chosen to remove $n apps.\n\n    $removals\n\nIf you choose 'yes' then this will remove both the applications and their data/messages. If you don't have a backup then you will not be able to recover the data for these applications.\n\nAre you sure that you wish to continue?" 15 60
    sel=$?
    case $sel in
        1) return;;
        255) return;;
    esac
    clear
    # remove the apps
    read_configuration
    remove_apps
}
function install_apps_selected {
    # which apps need to be installed?
    select_all_apps=$1
    installs=""
    app_index=0
    n=0
    for a in "${APPS_INSTALLED[@]}"
    do
        if [[ ${APPS_INSTALLED[$app_index]} == "0" ]]; then
            if [[ ${APPS_CHOSEN[$app_index]} == "1" ]]; then
                if [ ${n} -gt 0 ]; then
                    installs="$installs ${APPS_AVAILABLE[$app_index]}"
                else
                    installs="${APPS_AVAILABLE[$app_index]}"
                fi
                n=$[n+1]
            fi
        fi
        app_index=$[app_index+1]
    done
    # if no apps to be installed then don't do anything
    if [ ${n} -eq 0 ]; then
        return
    fi
    if [[ "$select_all_apps" != "add-all" ]]; then
        # ask for confirmation
        if [ $n -eq 1 ]; then
            dialog --title $"$installs" \
                   --backtitle $"Freedombone" \
                   --defaultno \
                   --yesno $"\nThis will install the $installs app\n\nProceed?" 9 40
        else
            dialog_height=$((15 + $n))
            dialog --title $"Add applications" \
                   --backtitle $"Freedombone" \
                   --defaultno \
                   --yesno $"\nYou have chosen to install $n apps\n\n    $installs\n\nProceed?" $dialog_height 60
        fi
        sel=$?
        case $sel in
            1) return;;
            255) return;;
        esac
    fi
    clear
    # install the apps
    read_configuration
    install_apps interactive
    if [ ! $APP_INSTALLED_SUCCESS ]; then
        echo $'One or more apps failed to install'
    fi
}
if [[ $1 == "test"* ]]; then
    ${PROJECT_NAME}-tests
    if [ ! "$?" = "0" ]; then
        exit 2
    fi
fi
detect_installable_apps
# if no applications were found
if [[ ${#APPS_AVAILABLE[@]} == 0 ]]; then
    exit 1
fi
show_apps $1
mark_unselected_apps_as_removed $1
clear
remove_apps_selected
if [[ $1 == "add-all" ]]; then
    install_apps_selected add-all
else
    install_apps_selected
fi
exit 0
 |