freedombone-utils-selector 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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 app_variants {
  39. filename=$1
  40. variants_line=$(cat ${filename} | grep "VARIANTS=")
  41. if [[ "$variants_line" == *"'"* ]]; then
  42. variants_list=$(echo "$variants_line" | awk -F '=' '{print $2}' | awk -F "'" '{print $2}')
  43. else
  44. variants_list=$(echo "$variants_line" | awk -F '=' '{print $2}' | awk -F '"' '{print $2}')
  45. fi
  46. echo "$variants_list"
  47. }
  48. function item_in_array {
  49. local e
  50. for e in "${@:2}"; do [[ "$e" == "$1" ]] && return 0; done
  51. return 1
  52. }
  53. function app_is_installed {
  54. app_name="$1"
  55. # Why does this secondary file exist, apart from COMPLETION_FILE ?
  56. # It's so that it is visible to unprivileged users from the user control panel
  57. INSTALLED_APPS_LIST=/usr/share/${PROJECT_NAME}/installed.txt
  58. if [ -f $INSTALLED_APPS_LIST ]; then
  59. if ! grep -Fxq "install_${app_name}" $INSTALLED_APPS_LIST; then
  60. echo "0"
  61. else
  62. echo "1"
  63. fi
  64. return
  65. fi
  66. if [ ! -f $COMPLETION_FILE ]; then
  67. echo "0"
  68. return
  69. fi
  70. if ! grep -Fxq "install_${app_name}" $COMPLETION_FILE; then
  71. echo "0"
  72. else
  73. echo "1"
  74. fi
  75. }
  76. function install_completed {
  77. if [ ! ${1} ]; then
  78. exit 673935
  79. fi
  80. echo "install_${1}" >> $COMPLETION_FILE
  81. }
  82. function get_apps_installed {
  83. for a in "${APPS_AVAILABLE[@]}"
  84. do
  85. APPS_INSTALLED+=("$(app_is_installed $a)")
  86. done
  87. }
  88. function get_apps_installed_names {
  89. APPS_INSTALLED_NAMES=()
  90. for a in "${APPS_AVAILABLE[@]}"
  91. do
  92. if [[ $(app_is_installed $a) == "1" ]]; then
  93. APPS_INSTALLED_NAMES+=("$a")
  94. fi
  95. done
  96. }
  97. function detect_apps {
  98. FILES=/usr/share/${PROJECT_NAME}/apps/${PROJECT_NAME}-app-*
  99. APPS_AVAILABLE=()
  100. APPS_CHOSEN=()
  101. # for all the app scripts
  102. for filename in $FILES
  103. do
  104. app_name=$(echo "${filename}" | awk -F '-app-' '{print $2}')
  105. if [[ $(item_in_array ${app_name} ${APPS_AVAILABLE[@]}) != 0 ]]; then
  106. APPS_AVAILABLE+=("${app_name}")
  107. APPS_CHOSEN+=("0")
  108. fi
  109. done
  110. function_check get_apps_installed
  111. get_apps_installed
  112. get_apps_installed_names
  113. }
  114. function detect_installable_apps {
  115. FILES=/usr/share/${PROJECT_NAME}/apps/${PROJECT_NAME}-app-*
  116. APPS_AVAILABLE=()
  117. APPS_CHOSEN=()
  118. APPS_INSTALLED=()
  119. APPS_INSTALLED_NAMES=()
  120. function_check app_is_installed
  121. # for all the app scripts
  122. for filename in $FILES
  123. do
  124. app_name=$(echo "${filename}" | awk -F '-app-' '{print $2}')
  125. if [[ $(item_in_array ${app_name} ${APPS_AVAILABLE[@]}) != 0 ]]; then
  126. variants_list=$(app_variants $filename)
  127. if [ ${#variants_list} -gt 0 ]; then
  128. APPS_AVAILABLE+=("${app_name}")
  129. APPS_CHOSEN+=("0")
  130. APPS_INSTALLED+=("$(app_is_installed $app_name)")
  131. if [[ $(app_is_installed $app_name) == "1" ]]; then
  132. APPS_INSTALLED_NAMES+=("$app_name")
  133. fi
  134. fi
  135. fi
  136. done
  137. }
  138. # creates the APPS_AVAILABLE and APPS_CHOSEN arrays based on
  139. # the given variant name
  140. function choose_apps_for_variant {
  141. variant_name="$1"
  142. FILES=/usr/share/${PROJECT_NAME}/apps/${PROJECT_NAME}-app-*
  143. APPS_AVAILABLE=()
  144. APPS_CHOSEN=()
  145. # for all the app scripts
  146. for filename in $FILES
  147. do
  148. app_name=$(echo "${filename}" | awk -F '-app-' '{print $2}')
  149. if [[ $(item_in_array ${app_name} ${APPS_AVAILABLE[@]}) != 0 ]]; then
  150. APPS_AVAILABLE+=("${app_name}")
  151. if grep -q "VARIANTS=" ${filename}; then
  152. variants_list=$(app_variants $filename)
  153. if [[ "${variants_list}" == 'all'* || \
  154. "${variants_list}" == "$variant_name "* || \
  155. "${variants_list}" == *" $variant_name "* || \
  156. "${variants_list}" == *" $variant_name" ]]; then
  157. APPS_CHOSEN+=("1")
  158. else
  159. APPS_CHOSEN+=("0")
  160. fi
  161. else
  162. APPS_CHOSEN+=("0")
  163. fi
  164. fi
  165. done
  166. function_check get_apps_installed
  167. get_apps_installed
  168. }
  169. function list_chosen_apps {
  170. app_index=0
  171. for a in "${APPS_AVAILABLE[@]}"
  172. do
  173. if [[ ${APPS_CHOSEN[$app_index]} == "1" ]]; then
  174. echo $"${a}"
  175. fi
  176. app_index=$[app_index+1]
  177. done
  178. }
  179. function remove_apps {
  180. app_index=0
  181. for a in "${APPS_AVAILABLE[@]}"
  182. do
  183. if [[ ${APPS_INSTALLED[$app_index]} == "1" ]]; then
  184. if [[ ${APPS_CHOSEN[$app_index]} == "0" ]]; then
  185. echo $"Removing application: ${a}"
  186. remove_${a}
  187. echo $"${a} was removed"
  188. fi
  189. fi
  190. app_index=$[app_index+1]
  191. done
  192. update_installed_apps_list
  193. }
  194. function install_apps {
  195. is_interactive=$1
  196. app_index=0
  197. for a in "${APPS_AVAILABLE[@]}"
  198. do
  199. if [[ ${APPS_INSTALLED[$app_index]} == "0" ]]; then
  200. if [[ ${APPS_CHOSEN[$app_index]} == "1" ]]; then
  201. if [ ${is_interactive} ]; then
  202. # interactively obtain settings for this app
  203. if [[ $(function_exists install_interactive_${a}) == "1" ]]; then
  204. install_interactive_${a}
  205. fi
  206. fi
  207. fi
  208. fi
  209. app_index=$[app_index+1]
  210. done
  211. app_index=0
  212. for a in "${APPS_AVAILABLE[@]}"
  213. do
  214. if [[ ${APPS_INSTALLED[$app_index]} == "0" ]]; then
  215. if [[ ${APPS_CHOSEN[$app_index]} == "1" ]]; then
  216. echo $"Installing application: ${a}"
  217. install_${a}
  218. echo $"${a} was installed"
  219. fi
  220. fi
  221. app_index=$[app_index+1]
  222. done
  223. update_installed_apps_list
  224. }
  225. # NOTE: deliberately no exit 0