Skip to content
Snippets Groups Projects
freedombone-utils-selector 5.03 KiB
Newer Older
Bob Mottram's avatar
Bob Mottram committed
#!/bin/bash
#
# .---.                  .              .
# |                      |              |
# |--- .--. .-.  .-.  .-.|  .-. .--.--. |.-.  .-. .--.  .-.
# |    |   (.-' (.-' (   | (   )|  |  | |   )(   )|  | (.-'
# '    '     --'  --'  -' -  -' '  '   -' -'   -' '   -  --'
#
#                    Freedom in the Cloud
#
# Functions for selecting which apps to install or remove
#
# License
# =======
#
# Copyright (C) 2015-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/>.

# Array containing names of available apps
APPS_AVAILABLE=()

# Array containing 1 or 0 indicating installed apps
APPS_INSTALLED=()

# Apps selected with checklist
APPS_CHOSEN=()

# A list of the names of installed apps
APPS_INSTALLED_NAMES=()

Bob Mottram's avatar
Bob Mottram committed
function item_in_array {
Bob Mottram's avatar
Bob Mottram committed
    local e
    for e in "${@:2}"; do [[ "$e" == "$1" ]] && return 0; done
    return 1
Bob Mottram's avatar
Bob Mottram committed
}

function app_is_installed {
Bob Mottram's avatar
Bob Mottram committed
    app_name="$1"
    if [ ! -f $COMPLETION_FILE ]; then
        echo "0"
        return
    fi

    if ! grep -Fxq "install_${app_name}" $COMPLETION_FILE; then
        echo "0"
    else
        echo "1"
    fi
Bob Mottram's avatar
Bob Mottram committed
}

function get_apps_installed {
Bob Mottram's avatar
Bob Mottram committed
    for a in "${APPS_AVAILABLE[@]}"
    do
        APPS_INSTALLED+=("$(app_is_installed $a)")
    done
function get_apps_installed_names {
Bob Mottram's avatar
Bob Mottram committed
    APPS_INSTALLED_NAMES=()
    for a in "${APPS_AVAILABLE[@]}"
    do
        if [[ $(app_is_installed $a) == "1" ]]; then
            APPS_INSTALLED_NAMES+=("$a")
        fi
    done
Bob Mottram's avatar
Bob Mottram committed
function detect_apps {
Bob Mottram's avatar
Bob Mottram committed
    FILES=/usr/share/${PROJECT_NAME}/apps/${PROJECT_NAME}-app-*

    APPS_AVAILABLE=()
    APPS_CHOSEN=()

    # for all the app scripts
    for filename in $FILES
    do
        app_name=$(echo "${filename}" | awk -F '-app-' '{print $2}')
        if [[ $(item_in_array ${app_name} ${APPS_AVAILABLE[@]}) != 0 ]]; then
            APPS_AVAILABLE+=("${app_name}")
            APPS_CHOSEN+=("0")
        fi
    done

    function_check get_apps_installed
    get_apps_installed
    get_apps_installed_names
# creates the APPS_AVAILABLE and APPS_CHOSEN arrays based on
# the given variant name
function choose_apps_for_variant {
Bob Mottram's avatar
Bob Mottram committed
    variant_name="$1"

    FILES=/usr/share/${PROJECT_NAME}/apps/${PROJECT_NAME}-app-*

    APPS_AVAILABLE=()
    APPS_CHOSEN=()

    # for all the app scripts
    for filename in $FILES
    do
        app_name=$(echo "${filename}" | awk -F '-app-' '{print $2}')
        if [[ $(item_in_array ${app_name} ${APPS_AVAILABLE[@]}) != 0 ]]; then
            APPS_AVAILABLE+=("${app_name}")

            if grep -q "VARIANTS=" ${filename}; then
                variants_line=$(cat ${filename} | grep "VARIANTS=")
                if [[ "$variants_line" == *"'"* ]]; then
                    variants_list=$(echo "$variants_line" | awk -F '=' '{print $2}' | awk -F "'" '{print $2}')
                else
                    variants_list=$(echo "$variants_line" | awk -F '=' '{print $2}' | awk -F '"' '{print $2}')
                fi
Bob Mottram's avatar
Bob Mottram committed
                if [[ "${variants_list}" == 'all'* || \
Bob Mottram's avatar
Bob Mottram committed
                      "${variants_list}" == "$variant_name "* || \
                      "${variants_list}" == *" $variant_name "* || \
                      "${variants_list}" == *" $variant_name" ]]; then
                    APPS_CHOSEN+=("1")
                else
                    APPS_CHOSEN+=("0")
                fi
            else
                APPS_CHOSEN+=("0")
            fi

        fi
    done

    function_check get_apps_installed
    get_apps_installed
}

function list_chosen_apps {
Bob Mottram's avatar
Bob Mottram committed
    app_index=0
    for a in "${APPS_AVAILABLE[@]}"
    do
        if [[ ${APPS_CHOSEN[$app_index]} == "1" ]]; then
            echo $"${a}"
        fi
        app_index=$[app_index+1]
    done
Bob Mottram's avatar
Bob Mottram committed
function remove_apps {
Bob Mottram's avatar
Bob Mottram committed
    app_index=0
    for a in "${APPS_AVAILABLE[@]}"
    do
        if [[ ${APPS_INSTALLED[$app_index]} == "1" ]]; then
            if [[ ${APPS_CHOSEN[$app_index]} == "0" ]]; then
                echo $"Removing application: ${a}"
                remove_${a}
                echo $"${a} was removed"
            fi
        fi
        app_index=$[app_index+1]
    done
Bob Mottram's avatar
Bob Mottram committed
}

function install_apps {
Bob Mottram's avatar
Bob Mottram committed
    app_index=0
    for a in "${APPS_AVAILABLE[@]}"
    do
        if [[ ${APPS_INSTALLED[$app_index]} == "0" ]]; then
            if [[ ${APPS_CHOSEN[$app_index]} == "1" ]]; then
                echo $"Installing application: ${a}"
                install_${a}
                echo $"${a} was installed"
            fi
        fi
        app_index=$[app_index+1]
    done
Bob Mottram's avatar
Bob Mottram committed
}
Bob Mottram's avatar
Bob Mottram committed

# NOTE: deliberately no exit 0