freedombone-utils-selector 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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. # file containing a list of removed apps
  39. REMOVED_APPS_FILE=/root/removed
  40. # gets the variants list from an app script
  41. function app_variants {
  42. filename=$1
  43. variants_line=$(cat ${filename} | grep "VARIANTS=")
  44. if [[ "$variants_line" == *"'"* ]]; then
  45. variants_list=$(echo "$variants_line" | awk -F '=' '{print $2}' | awk -F "'" '{print $2}')
  46. else
  47. variants_list=$(echo "$variants_line" | awk -F '=' '{print $2}' | awk -F '"' '{print $2}')
  48. fi
  49. echo "$variants_list"
  50. }
  51. # whether a given item is in an array
  52. function item_in_array {
  53. local e
  54. for e in "${@:2}"; do [[ "$e" == "$1" ]] && return 0; done
  55. return 1
  56. }
  57. # mark a given app as having been removed so that it doesn't get reinstalled on updates
  58. function remove_app {
  59. app_name=$1
  60. if [ ! -f $REMOVED_APPS_FILE ]; then
  61. touch $REMOVED_APPS_FILE
  62. fi
  63. if ! grep -Fxq "_${app_name}_" $REMOVED_APPS_FILE; then
  64. echo "_${app_name}_" >> $REMOVED_APPS_FILE
  65. fi
  66. }
  67. # returns 1 if an app has been marked as removed
  68. function app_is_removed {
  69. app_name="$1"
  70. if [ ! -f $REMOVED_APPS_FILE ]; then
  71. echo "0"
  72. return
  73. fi
  74. if ! grep -Fxq "_${app_name}_" $REMOVED_APPS_FILE; then
  75. echo "0"
  76. else
  77. echo "1"
  78. fi
  79. }
  80. # Allows an app to be reinstalled even if it was previously marked as being removed
  81. function reinstall_app {
  82. app_name=$1
  83. if [ ! -f $REMOVED_APPS_FILE ]; then
  84. return
  85. fi
  86. if [[ $(app_is_removed $app_name) == "1" ]]; then
  87. sed -i "/_${app_name}_/d" $REMOVED_APPS_FILE
  88. fi
  89. }
  90. # returns 1 if an app is installed
  91. function app_is_installed {
  92. app_name="$1"
  93. # Why does this secondary file exist, apart from COMPLETION_FILE ?
  94. # It's so that it is visible to unprivileged users from the user control panel
  95. INSTALLED_APPS_LIST=/usr/share/${PROJECT_NAME}/installed.txt
  96. if [ -f $INSTALLED_APPS_LIST ]; then
  97. if ! grep -Fxq "install_${app_name}" $INSTALLED_APPS_LIST; then
  98. echo "0"
  99. else
  100. echo "1"
  101. fi
  102. return
  103. fi
  104. # check the completion file to see if it was installed
  105. if [ ! -f $COMPLETION_FILE ]; then
  106. echo "0"
  107. return
  108. fi
  109. if ! grep -Fxq "install_${app_name}" $COMPLETION_FILE; then
  110. echo "0"
  111. else
  112. echo "1"
  113. fi
  114. }
  115. # called at the end of the install section of an app script
  116. function install_completed {
  117. if [ ! ${1} ]; then
  118. exit 673935
  119. fi
  120. echo "install_${1}" >> $COMPLETION_FILE
  121. }
  122. # populates an array of "0" or "1" for whether apps are installed
  123. function get_apps_installed {
  124. for a in "${APPS_AVAILABLE[@]}"
  125. do
  126. APPS_INSTALLED+=("$(app_is_installed $a)")
  127. done
  128. }
  129. # populates an array of installed app names
  130. function get_apps_installed_names {
  131. APPS_INSTALLED_NAMES=()
  132. for a in "${APPS_AVAILABLE[@]}"
  133. do
  134. if [[ $(app_is_installed $a) == "1" ]]; then
  135. APPS_INSTALLED_NAMES+=("$a")
  136. fi
  137. done
  138. }
  139. # detects what apps are available
  140. function detect_apps {
  141. FILES=/usr/share/${PROJECT_NAME}/apps/${PROJECT_NAME}-app-*
  142. APPS_AVAILABLE=()
  143. APPS_CHOSEN=()
  144. # for all the app scripts
  145. for filename in $FILES
  146. do
  147. app_name=$(echo "${filename}" | awk -F '-app-' '{print $2}')
  148. if [[ $(item_in_array ${app_name} ${APPS_AVAILABLE[@]}) != 0 ]]; then
  149. APPS_AVAILABLE+=("${app_name}")
  150. APPS_CHOSEN+=("0")
  151. fi
  152. done
  153. function_check get_apps_installed
  154. get_apps_installed
  155. get_apps_installed_names
  156. }
  157. # detects what apps are available and can be installed
  158. # If the variants list within an app script is an empty string then
  159. # it is considered to be too experimental to be installable
  160. function detect_installable_apps {
  161. FILES=/usr/share/${PROJECT_NAME}/apps/${PROJECT_NAME}-app-*
  162. APPS_AVAILABLE=()
  163. APPS_CHOSEN=()
  164. APPS_INSTALLED=()
  165. APPS_INSTALLED_NAMES=()
  166. function_check app_is_installed
  167. # for all the app scripts
  168. for filename in $FILES
  169. do
  170. app_name=$(echo "${filename}" | awk -F '-app-' '{print $2}')
  171. if [[ $(item_in_array ${app_name} ${APPS_AVAILABLE[@]}) != 0 ]]; then
  172. variants_list=$(app_variants $filename)
  173. # check for empty string
  174. if [ ${#variants_list} -gt 0 ]; then
  175. APPS_AVAILABLE+=("${app_name}")
  176. APPS_CHOSEN+=("0")
  177. APPS_INSTALLED+=("$(app_is_installed $app_name)")
  178. if [[ $(app_is_installed $app_name) == "1" ]]; then
  179. APPS_INSTALLED_NAMES+=("$app_name")
  180. fi
  181. fi
  182. fi
  183. done
  184. }
  185. # creates the APPS_AVAILABLE and APPS_CHOSEN arrays based on
  186. # the given variant name
  187. function choose_apps_for_variant {
  188. variant_name="$1"
  189. FILES=/usr/share/${PROJECT_NAME}/apps/${PROJECT_NAME}-app-*
  190. APPS_AVAILABLE=()
  191. APPS_CHOSEN=()
  192. # for all the app scripts
  193. for filename in $FILES
  194. do
  195. app_name=$(echo "${filename}" | awk -F '-app-' '{print $2}')
  196. if [[ $(item_in_array ${app_name} ${APPS_AVAILABLE[@]}) != 0 ]]; then
  197. APPS_AVAILABLE+=("${app_name}")
  198. if grep -q "VARIANTS=" ${filename}; then
  199. variants_list=$(app_variants $filename)
  200. if [[ "${variants_list}" == 'all'* || \
  201. "${variants_list}" == "$variant_name "* || \
  202. "${variants_list}" == *" $variant_name "* || \
  203. "${variants_list}" == *" $variant_name" ]]; then
  204. if [[ $(app_is_removed ${a}) == "0" ]]; then
  205. APPS_CHOSEN+=("1")
  206. else
  207. APPS_CHOSEN+=("0")
  208. fi
  209. else
  210. APPS_CHOSEN+=("0")
  211. fi
  212. else
  213. APPS_CHOSEN+=("0")
  214. fi
  215. fi
  216. done
  217. function_check get_apps_installed
  218. get_apps_installed
  219. }
  220. # show a list of apps which have been chosen
  221. function list_chosen_apps {
  222. app_index=0
  223. for a in "${APPS_AVAILABLE[@]}"
  224. do
  225. if [[ ${APPS_CHOSEN[$app_index]} == "1" ]]; then
  226. echo $"${a}"
  227. fi
  228. app_index=$[app_index+1]
  229. done
  230. }
  231. function remove_apps {
  232. app_index=0
  233. for a in "${APPS_AVAILABLE[@]}"
  234. do
  235. if [[ ${APPS_INSTALLED[$app_index]} == "1" ]]; then
  236. if [[ ${APPS_CHOSEN[$app_index]} == "0" ]]; then
  237. echo $"Removing application: ${a}"
  238. remove_app ${a}
  239. remove_${a}
  240. echo $"${a} was removed"
  241. fi
  242. fi
  243. app_index=$[app_index+1]
  244. done
  245. update_installed_apps_list
  246. }
  247. function install_apps {
  248. is_interactive=$1
  249. # interactive install configuration for each app
  250. if [ ${is_interactive} ]; then
  251. app_index=0
  252. for a in "${APPS_AVAILABLE[@]}"
  253. do
  254. if [[ ${APPS_INSTALLED[$app_index]} == "0" ]]; then
  255. if [[ ${APPS_CHOSEN[$app_index]} == "1" ]]; then
  256. # interactively obtain settings for this app
  257. if [[ $(function_exists install_interactive_${a}) == "1" ]]; then
  258. install_interactive_${a}
  259. fi
  260. fi
  261. fi
  262. app_index=$[app_index+1]
  263. done
  264. fi
  265. # now install the apps
  266. app_index=0
  267. for a in "${APPS_AVAILABLE[@]}"
  268. do
  269. if [[ ${APPS_INSTALLED[$app_index]} == "0" ]]; then
  270. if [[ ${APPS_CHOSEN[$app_index]} == "1" ]]; then
  271. if [ ${is_interactive} ]; then
  272. # clears any removal indicator
  273. reinstall_app ${a}
  274. if [[ $(app_is_installed ${a}) == "1" ]]; then
  275. echo $"Upgrading application from interactive: ${a}"
  276. upgrade_${a}
  277. echo $"${a} was upgraded from interactive"
  278. else
  279. echo $"Installing application from interactive: ${a}"
  280. install_${a}
  281. install_completed ${a}
  282. echo $"${a} was installed from interactive"
  283. fi
  284. else
  285. # check if the app was removed
  286. if [[ $(app_is_removed ${a}) == "0" ]]; then
  287. if [[ $(app_is_installed ${a}) == "1" ]]; then
  288. echo $"Upgrading application: ${a}"
  289. upgrade_${a}
  290. echo $"${a} was upgraded"
  291. else
  292. echo $"Installing application: ${a}"
  293. install_${a}
  294. install_completed ${a}
  295. echo $"${a} was installed"
  296. fi
  297. else
  298. echo $"${a} has been removed and so will not be reinstalled"
  299. fi
  300. fi
  301. fi
  302. fi
  303. app_index=$[app_index+1]
  304. done
  305. update_installed_apps_list
  306. }
  307. # NOTE: deliberately no exit 0