freedombone-utils-selector 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # Functions for selecting which apps to install or remove
  12. #
  13. # License
  14. # =======
  15. #
  16. # Copyright (C) 2015-2016 Bob Mottram <bob@robotics.uk.to>
  17. #
  18. # This program is free software: you can redistribute it and/or modify
  19. # it under the terms of the GNU Affero General Public License as published by
  20. # the Free Software Foundation, either version 3 of the License, or
  21. # (at your option) any later version.
  22. #
  23. # This program is distributed in the hope that it will be useful,
  24. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. # GNU Affero General Public License for more details.
  27. #
  28. # You should have received a copy of the GNU Affero General Public License
  29. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  30. # Array containing names of available apps
  31. APPS_AVAILABLE=()
  32. # Array containing 1 or 0 indicating installed apps
  33. APPS_INSTALLED=()
  34. # Apps selected with checklist
  35. APPS_CHOSEN=()
  36. # A list of the names of installed apps
  37. APPS_INSTALLED_NAMES=()
  38. function item_in_array {
  39. local e
  40. for e in "${@:2}"; do [[ "$e" == "$1" ]] && return 0; done
  41. return 1
  42. }
  43. function app_is_installed {
  44. app_name="$1"
  45. if [ ! -f $COMPLETION_FILE ]; then
  46. echo "0"
  47. return
  48. fi
  49. if ! grep -Fxq "install_${app_name}" $COMPLETION_FILE; then
  50. echo "0"
  51. else
  52. echo "1"
  53. fi
  54. }
  55. function get_apps_installed {
  56. for a in "${APPS_AVAILABLE[@]}"
  57. do
  58. APPS_INSTALLED+=("$(app_is_installed $a)")
  59. done
  60. }
  61. function get_apps_installed_names {
  62. APPS_INSTALLED_NAMES=()
  63. for a in "${APPS_AVAILABLE[@]}"
  64. do
  65. if [[ $(app_is_installed $a) == "1" ]]; then
  66. APPS_INSTALLED_NAMES+=("$a")
  67. fi
  68. done
  69. }
  70. function detect_apps {
  71. FILES=/usr/share/${PROJECT_NAME}/apps/${PROJECT_NAME}-app-*
  72. APPS_AVAILABLE=()
  73. APPS_CHOSEN=()
  74. # for all the app scripts
  75. for filename in $FILES
  76. do
  77. app_name=$(echo "${filename}" | awk -F '-app-' '{print $2}')
  78. if [[ $(item_in_array ${app_name} ${APPS_AVAILABLE[@]}) != 0 ]]; then
  79. APPS_AVAILABLE+=("${app_name}")
  80. APPS_CHOSEN+=("0")
  81. fi
  82. done
  83. function_check get_apps_installed
  84. get_apps_installed
  85. get_apps_installed_names
  86. }
  87. # creates the APPS_AVAILABLE and APPS_CHOSEN arrays based on
  88. # the given variant name
  89. function choose_apps_for_variant {
  90. variant_name="$1"
  91. FILES=/usr/share/${PROJECT_NAME}/apps/${PROJECT_NAME}-app-*
  92. APPS_AVAILABLE=()
  93. APPS_CHOSEN=()
  94. # for all the app scripts
  95. for filename in $FILES
  96. do
  97. app_name=$(echo "${filename}" | awk -F '-app-' '{print $2}')
  98. if [[ $(item_in_array ${app_name} ${APPS_AVAILABLE[@]}) != 0 ]]; then
  99. APPS_AVAILABLE+=("${app_name}")
  100. if grep -q "VARIANTS=" ${filename}; then
  101. variants_list="$(cat ${filename} | grep "VARIANTS=" | awk -F '=' '{print $2}' | awk -F "'" '{print $2}')"
  102. if [[ "${variants_list}" == 'all' || "${variants_list}" == "$variant_name "* || "${variants_list}" == *" $variant_name "* || "${variants_list}" == *" $variant_name" ]]; then
  103. APPS_CHOSEN+=("1")
  104. else
  105. APPS_CHOSEN+=("0")
  106. fi
  107. else
  108. APPS_CHOSEN+=("0")
  109. fi
  110. fi
  111. done
  112. function_check get_apps_installed
  113. get_apps_installed
  114. }
  115. function list_chosen_apps {
  116. app_index=0
  117. for a in "${APPS_AVAILABLE[@]}"
  118. do
  119. if [[ ${APPS_CHOSEN[$app_index]} == "1" ]]; then
  120. echo $"${a}"
  121. fi
  122. app_index=$[app_index+1]
  123. done
  124. }
  125. function remove_apps {
  126. app_index=0
  127. for a in "${APPS_AVAILABLE[@]}"
  128. do
  129. if [[ ${APPS_INSTALLED[$app_index]} == "1" ]]; then
  130. if [[ ${APPS_CHOSEN[$app_index]} == "0" ]]; then
  131. echo $"Removing application: ${a}"
  132. remove_${a}
  133. echo $"${a} was removed"
  134. fi
  135. fi
  136. app_index=$[app_index+1]
  137. done
  138. }
  139. function install_apps {
  140. app_index=0
  141. for a in "${APPS_AVAILABLE[@]}"
  142. do
  143. if [[ ${APPS_INSTALLED[$app_index]} == "0" ]]; then
  144. if [[ ${APPS_CHOSEN[$app_index]} == "1" ]]; then
  145. echo $"Installing application: ${a}"
  146. install_${a}
  147. echo $"${a} was installed"
  148. fi
  149. fi
  150. app_index=$[app_index+1]
  151. done
  152. }
  153. # NOTE: deliberately no exit 0