123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348 |
- #!/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=()
-
- # file containing a list of removed apps
- REMOVED_APPS_FILE=/root/removed
-
- # gets the variants list from an app script
- function app_variants {
- filename=$1
- 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
- echo "$variants_list"
- }
-
- # whether a given item is in an array
- function item_in_array {
- local e
- for e in "${@:2}"; do [[ "$e" == "$1" ]] && return 0; done
- return 1
- }
-
- # mark a given app as having been removed so that it doesn't get reinstalled on updates
- function remove_app {
- app_name=$1
- if [ ! -f $REMOVED_APPS_FILE ]; then
- touch $REMOVED_APPS_FILE
- fi
- if ! grep -Fxq "_${app_name}_" $REMOVED_APPS_FILE; then
- echo "_${app_name}_" >> $REMOVED_APPS_FILE
- fi
- }
-
- # returns 1 if an app has been marked as removed
- function app_is_removed {
- app_name="$1"
- if [ ! -f $REMOVED_APPS_FILE ]; then
- echo "0"
- return
- fi
-
- if ! grep -Fxq "_${app_name}_" $REMOVED_APPS_FILE; then
- echo "0"
- else
- echo "1"
- fi
- }
-
- # Allows an app to be reinstalled even if it was previously marked as being removed
- function reinstall_app {
- app_name=$1
- if [ ! -f $REMOVED_APPS_FILE ]; then
- return
- fi
- if [[ $(app_is_removed $app_name) == "1" ]]; then
- sed -i "/_${app_name}_/d" $REMOVED_APPS_FILE
- fi
- }
-
- # returns 1 if an app is installed
- function app_is_installed {
- app_name="$1"
-
- # Why does this secondary file exist, apart from COMPLETION_FILE ?
- # It's so that it is visible to unprivileged users from the user control panel
- INSTALLED_APPS_LIST=/usr/share/${PROJECT_NAME}/installed.txt
- if [ -f $INSTALLED_APPS_LIST ]; then
- if ! grep -Fxq "install_${app_name}" $INSTALLED_APPS_LIST; then
- echo "0"
- else
- echo "1"
- fi
- return
- fi
-
- # check the completion file to see if it was installed
- if [ ! -f $COMPLETION_FILE ]; then
- echo "0"
- return
- fi
-
- if ! grep -Fxq "install_${app_name}" $COMPLETION_FILE; then
- echo "0"
- else
- echo "1"
- fi
- }
-
- # called at the end of the install section of an app script
- function install_completed {
- if [ ! ${1} ]; then
- exit 673935
- fi
- echo "install_${1}" >> $COMPLETION_FILE
- }
-
- # populates an array of "0" or "1" for whether apps are installed
- function get_apps_installed {
- for a in "${APPS_AVAILABLE[@]}"
- do
- APPS_INSTALLED+=("$(app_is_installed $a)")
- done
- }
-
- # populates an array of installed app names
- function get_apps_installed_names {
- APPS_INSTALLED_NAMES=()
- for a in "${APPS_AVAILABLE[@]}"
- do
- if [[ $(app_is_installed $a) == "1" ]]; then
- APPS_INSTALLED_NAMES+=("$a")
- fi
- done
- }
-
- # detects what apps are available
- function detect_apps {
- 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
- }
-
- # detects what apps are available and can be installed
- # If the variants list within an app script is an empty string then
- # it is considered to be too experimental to be installable
- function detect_installable_apps {
- FILES=/usr/share/${PROJECT_NAME}/apps/${PROJECT_NAME}-app-*
-
- APPS_AVAILABLE=()
- APPS_CHOSEN=()
- APPS_INSTALLED=()
- APPS_INSTALLED_NAMES=()
-
- function_check app_is_installed
-
- # 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
- variants_list=$(app_variants $filename)
- # check for empty string
- if [ ${#variants_list} -gt 0 ]; then
- APPS_AVAILABLE+=("${app_name}")
- APPS_CHOSEN+=("0")
- APPS_INSTALLED+=("$(app_is_installed $app_name)")
- if [[ $(app_is_installed $app_name) == "1" ]]; then
- APPS_INSTALLED_NAMES+=("$app_name")
- fi
- fi
- fi
- done
- }
-
- # creates the APPS_AVAILABLE and APPS_CHOSEN arrays based on
- # the given variant name
- function choose_apps_for_variant {
- 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_list=$(app_variants $filename)
- if [[ "${variants_list}" == 'all'* || \
- "${variants_list}" == "$variant_name "* || \
- "${variants_list}" == *" $variant_name "* || \
- "${variants_list}" == *" $variant_name" ]]; then
- if [[ $(app_is_removed ${a}) == "0" ]]; then
- APPS_CHOSEN+=("1")
- else
- APPS_CHOSEN+=("0")
- fi
- else
- APPS_CHOSEN+=("0")
- fi
- else
- APPS_CHOSEN+=("0")
- fi
-
- fi
- done
-
- function_check get_apps_installed
- get_apps_installed
- }
-
- # show a list of apps which have been chosen
- function list_chosen_apps {
- 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
- }
-
- function remove_apps {
- 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_app ${a}
- remove_${a}
- echo $"${a} was removed"
- fi
- fi
- app_index=$[app_index+1]
- done
- update_installed_apps_list
- }
-
- function install_apps {
- is_interactive=$1
-
- # interactive install configuration for each app
- if [ ${is_interactive} ]; then
- app_index=0
- for a in "${APPS_AVAILABLE[@]}"
- do
- if [[ ${APPS_INSTALLED[$app_index]} == "0" ]]; then
- if [[ ${APPS_CHOSEN[$app_index]} == "1" ]]; then
- # interactively obtain settings for this app
- if [[ $(function_exists install_interactive_${a}) == "1" ]]; then
- install_interactive_${a}
- fi
- fi
- fi
- app_index=$[app_index+1]
- done
- fi
-
- # now install the apps
- app_index=0
- for a in "${APPS_AVAILABLE[@]}"
- do
- if [[ ${APPS_INSTALLED[$app_index]} == "0" ]]; then
- if [[ ${APPS_CHOSEN[$app_index]} == "1" ]]; then
- if [ ${is_interactive} ]; then
- # clears any removal indicator
- reinstall_app ${a}
-
- if [[ $(app_is_installed ${a}) == "1" ]]; then
- echo $"Upgrading application from interactive: ${a}"
- upgrade_${a}
- echo $"${a} was upgraded from interactive"
- else
- echo $"Installing application from interactive: ${a}"
- install_${a}
- install_completed ${a}
- echo $"${a} was installed from interactive"
- fi
- else
- # check if the app was removed
- if [[ $(app_is_removed ${a}) == "0" ]]; then
- if [[ $(app_is_installed ${a}) == "1" ]]; then
- echo $"Upgrading application: ${a}"
- upgrade_${a}
- echo $"${a} was upgraded"
- else
- echo $"Installing application: ${a}"
- install_${a}
- install_completed ${a}
- echo $"${a} was installed"
- fi
- else
- echo $"${a} has been removed and so will not be reinstalled"
- fi
- fi
- fi
- fi
- app_index=$[app_index+1]
- done
-
- update_installed_apps_list
- }
-
- # NOTE: deliberately no exit 0
|