freedombone-utils-selector 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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. INSTALLED_APPS_LIST=/usr/share/${PROJECT_NAME}/installed.txt
  41. if [ ! $COMPLETION_FILE ]; then
  42. COMPLETION_FILE=$HOME/${PROJECT_NAME}-completed.txt
  43. fi
  44. # Loads variables defined at the beginning of an app script
  45. function app_load_variables {
  46. app_name=$1
  47. config_var_name=${app_name}_variables
  48. if [ ! ${!config_var_name} ]; then
  49. echo $"${app_name}_variables was not found"
  50. return
  51. fi
  52. eval "configvarname=(\${$config_var_name[@]})"
  53. for v in "${configvarname[@]}"
  54. do
  55. read_config_param $v
  56. done
  57. }
  58. # Saves variables for a given app script
  59. function app_save_variables {
  60. app_name=$1
  61. config_var_name=${app_name}_variables
  62. if [ ! ${!config_var_name} ]; then
  63. return
  64. fi
  65. eval "configvarname=(\${$config_var_name[@]})"
  66. for v in "${configvarname[@]}"
  67. do
  68. write_config_param $v "${!v}"
  69. done
  70. }
  71. # gets the variants list from an app script
  72. function app_variants {
  73. filename=$1
  74. variants_line=$(cat ${filename} | grep 'VARIANTS=')
  75. if [[ "$variants_line" == *"'"* ]]; then
  76. variants_list=$(echo "$variants_line" | awk -F '=' '{print $2}' | awk -F "'" '{print $2}')
  77. else
  78. variants_list=$(echo "$variants_line" | awk -F '=' '{print $2}' | awk -F '"' '{print $2}')
  79. fi
  80. echo "$variants_list"
  81. }
  82. # whether a given item is in an array
  83. function item_in_array {
  84. local e
  85. for e in "${@:2}"; do [[ "$e" == "$1" ]] && return 0; done
  86. return 1
  87. }
  88. # returns a list of available system variants
  89. # based upon the variants string in each app script
  90. function available_system_variants {
  91. FILES=/usr/share/${PROJECT_NAME}/apps/${PROJECT_NAME}-app-*
  92. new_available_variants_list=()
  93. for filename in $FILES
  94. do
  95. system_variants_list=$(app_variants $filename)
  96. variants_array=($system_variants_list)
  97. for variant_str in "${variants_array[@]}"
  98. do
  99. item_in_array "${variant_str}" "${new_available_variants_list[@]}"
  100. if [[ $? != 0 ]]; then
  101. new_available_variants_list+=("$variant_str")
  102. fi
  103. done
  104. done
  105. available_variants_list=($(sort <<<"${new_available_variants_list[*]}"))
  106. }
  107. function is_valid_variant {
  108. sys_type="$1"
  109. available_variants_list=()
  110. available_system_variants
  111. for variant_str in "${available_variants_list[@]}"
  112. do
  113. if [[ "$sys_type" == "$variant_str" ]]; then
  114. return "1"
  115. fi
  116. done
  117. return "0"
  118. }
  119. function show_available_variants {
  120. available_variants_list=()
  121. available_system_variants
  122. for variant_str in "${available_variants_list[@]}"
  123. do
  124. echo " $variant_str"
  125. done
  126. }
  127. # mark a given app as having been removed so that it doesn't get reinstalled on updates
  128. function remove_app {
  129. app_name=$1
  130. if [ ! -f $REMOVED_APPS_FILE ]; then
  131. touch $REMOVED_APPS_FILE
  132. fi
  133. if ! grep -Fxq "_${app_name}_" $REMOVED_APPS_FILE; then
  134. echo "_${app_name}_" >> $REMOVED_APPS_FILE
  135. fi
  136. if grep -Fxq "install_${app_name}" $COMPLETION_FILE; then
  137. sed -i "/install_${app_name}/d" $COMPLETION_FILE
  138. fi
  139. if grep -Fxq "install_${app_name}" $INSTALLED_APPS_LIST; then
  140. sed -i "/install_${app_name}/d" $INSTALLED_APPS_LIST
  141. fi
  142. }
  143. # returns 1 if an app has been marked as removed
  144. function app_is_removed {
  145. app_name="$1"
  146. if [ ! -f $REMOVED_APPS_FILE ]; then
  147. echo "0"
  148. return
  149. fi
  150. if ! grep -Fxq "_${app_name}_" $REMOVED_APPS_FILE; then
  151. echo "0"
  152. else
  153. echo "1"
  154. fi
  155. }
  156. # Allows an app to be reinstalled even if it was previously marked as being removed
  157. function reinstall_app {
  158. app_name=$1
  159. if [ ! -f $REMOVED_APPS_FILE ]; then
  160. return
  161. fi
  162. if [[ $(app_is_removed $app_name) == "1" ]]; then
  163. sed -i "/_${app_name}_/d" $REMOVED_APPS_FILE
  164. fi
  165. }
  166. # returns 1 if an app is installed
  167. function app_is_installed {
  168. app_name="$1"
  169. # Why does this secondary file exist, apart from COMPLETION_FILE ?
  170. # It's so that it is visible to unprivileged users from the user control panel
  171. if [ -f $INSTALLED_APPS_LIST ]; then
  172. if ! grep -Fxq "install_${app_name}" $INSTALLED_APPS_LIST; then
  173. echo "0"
  174. else
  175. echo "1"
  176. fi
  177. return
  178. fi
  179. # check the completion file to see if it was installed
  180. if [ ! -f $COMPLETION_FILE ]; then
  181. echo "0"
  182. return
  183. fi
  184. if ! grep -Fxq "install_${app_name}" $COMPLETION_FILE; then
  185. echo "0"
  186. else
  187. echo "1"
  188. fi
  189. }
  190. # called at the end of the install section of an app script
  191. function install_completed {
  192. if [ ! ${1} ]; then
  193. exit 673935
  194. fi
  195. echo "install_${1}" >> $COMPLETION_FILE
  196. }
  197. # populates an array of "0" or "1" for whether apps are installed
  198. function get_apps_installed {
  199. for a in "${APPS_AVAILABLE[@]}"
  200. do
  201. APPS_INSTALLED+=("$(app_is_installed $a)")
  202. done
  203. }
  204. # populates an array of installed app names
  205. function get_apps_installed_names {
  206. APPS_INSTALLED_NAMES=()
  207. for a in "${APPS_AVAILABLE[@]}"
  208. do
  209. if [[ $(app_is_installed $a) == "1" ]]; then
  210. APPS_INSTALLED_NAMES+=("$a")
  211. fi
  212. done
  213. }
  214. # detects what apps are available
  215. function detect_apps {
  216. FILES=/usr/share/${PROJECT_NAME}/apps/${PROJECT_NAME}-app-*
  217. APPS_AVAILABLE=()
  218. APPS_CHOSEN=()
  219. # for all the app scripts
  220. for filename in $FILES
  221. do
  222. app_name=$(echo "${filename}" | awk -F '-app-' '{print $2}')
  223. item_in_array "${app_name}" "${APPS_AVAILABLE[@]}"
  224. if [[ $? != 0 ]]; then
  225. APPS_AVAILABLE+=("${app_name}")
  226. APPS_CHOSEN+=("0")
  227. fi
  228. done
  229. function_check get_apps_installed
  230. get_apps_installed
  231. get_apps_installed_names
  232. }
  233. # detects what apps are available and can be installed
  234. # If the variants list within an app script is an empty string then
  235. # it is considered to be too experimental to be installable
  236. function detect_installable_apps {
  237. FILES=/usr/share/${PROJECT_NAME}/apps/${PROJECT_NAME}-app-*
  238. APPS_AVAILABLE=()
  239. APPS_CHOSEN=()
  240. APPS_INSTALLED=()
  241. APPS_INSTALLED_NAMES=()
  242. function_check app_is_installed
  243. # for all the app scripts
  244. for filename in $FILES
  245. do
  246. app_name=$(echo "${filename}" | awk -F '-app-' '{print $2}')
  247. item_in_array "${app_name}" "${APPS_AVAILABLE[@]}"
  248. if [[ $? != 0 ]]; then
  249. variants_list=$(app_variants $filename)
  250. # check for empty string
  251. if [ ${#variants_list} -gt 0 ]; then
  252. APPS_AVAILABLE+=("${app_name}")
  253. APPS_CHOSEN+=("0")
  254. APPS_INSTALLED+=("$(app_is_installed $app_name)")
  255. if [[ $(app_is_installed $app_name) == "1" ]]; then
  256. APPS_INSTALLED_NAMES+=("$app_name")
  257. fi
  258. fi
  259. fi
  260. done
  261. }
  262. # creates the APPS_AVAILABLE and APPS_CHOSEN arrays based on
  263. # the given variant name
  264. function choose_apps_for_variant {
  265. variant_name="$1"
  266. FILES=/usr/share/${PROJECT_NAME}/apps/${PROJECT_NAME}-app-*
  267. APPS_AVAILABLE=()
  268. APPS_CHOSEN=()
  269. # for all the app scripts
  270. for filename in $FILES
  271. do
  272. app_name=$(echo "${filename}" | awk -F '-app-' '{print $2}')
  273. item_in_array "${app_name}" "${APPS_AVAILABLE[@]}"
  274. if [[ $? != 0 ]]; then
  275. APPS_AVAILABLE+=("${app_name}")
  276. if grep -q "VARIANTS=" ${filename}; then
  277. variants_list=$(app_variants $filename)
  278. if [[ "${variants_list}" == 'all'* || \
  279. "${variants_list}" == "$variant_name "* || \
  280. "${variants_list}" == *" $variant_name "* || \
  281. "${variants_list}" == *" $variant_name" ]]; then
  282. if [[ $(app_is_removed ${a}) == "0" ]]; then
  283. APPS_CHOSEN+=("1")
  284. else
  285. APPS_CHOSEN+=("0")
  286. fi
  287. else
  288. APPS_CHOSEN+=("0")
  289. fi
  290. else
  291. APPS_CHOSEN+=("0")
  292. fi
  293. fi
  294. done
  295. function_check get_apps_installed
  296. get_apps_installed
  297. }
  298. # show a list of apps which have been chosen
  299. function list_chosen_apps {
  300. app_index=0
  301. for a in "${APPS_AVAILABLE[@]}"
  302. do
  303. if [[ ${APPS_CHOSEN[$app_index]} == "1" ]]; then
  304. echo $"${a}"
  305. fi
  306. app_index=$[app_index+1]
  307. done
  308. }
  309. function remove_apps {
  310. app_index=0
  311. for a in "${APPS_AVAILABLE[@]}"
  312. do
  313. if [[ ${APPS_INSTALLED[$app_index]} == "1" ]]; then
  314. if [[ ${APPS_CHOSEN[$app_index]} == "0" ]]; then
  315. echo $"Removing application: ${a}"
  316. app_load_variables ${a}
  317. remove_app ${a}
  318. remove_${a}
  319. echo $"${a} was removed"
  320. fi
  321. fi
  322. app_index=$[app_index+1]
  323. done
  324. update_installed_apps_list
  325. }
  326. function install_apps {
  327. is_interactive=$1
  328. # interactive install configuration for each app
  329. if [ ${is_interactive} ]; then
  330. app_index=0
  331. for a in "${APPS_AVAILABLE[@]}"
  332. do
  333. if [[ ${APPS_INSTALLED[$app_index]} == "0" ]]; then
  334. if [[ ${APPS_CHOSEN[$app_index]} == "1" ]]; then
  335. # interactively obtain settings for this app
  336. if [[ $(function_exists install_interactive_${a}) == "1" ]]; then
  337. install_interactive_${a}
  338. fi
  339. fi
  340. fi
  341. app_index=$[app_index+1]
  342. done
  343. fi
  344. # now install the apps
  345. app_index=0
  346. for a in "${APPS_AVAILABLE[@]}"
  347. do
  348. if [[ ${APPS_INSTALLED[$app_index]} == "0" ]]; then
  349. if [[ ${APPS_CHOSEN[$app_index]} == "1" ]]; then
  350. if [ ${is_interactive} ]; then
  351. # clears any removal indicator
  352. reinstall_app ${a}
  353. app_load_variables ${a}
  354. if [[ $(app_is_installed ${a}) == "1" ]]; then
  355. echo $"Upgrading application from interactive: ${a}"
  356. upgrade_${a}
  357. echo $"${a} was upgraded from interactive"
  358. else
  359. echo $"Installing application from interactive: ${a}"
  360. APP_INSTALLED=
  361. install_${a}
  362. if [ $APP_INSTALLED ]; then
  363. app_save_variables ${a}
  364. install_completed ${a}
  365. echo $"${a} was installed from interactive"
  366. else
  367. echo $"${a} was not installed from interactive"
  368. fi
  369. fi
  370. else
  371. # check if the app was removed
  372. if [[ $(app_is_removed ${a}) == "0" ]]; then
  373. app_load_variables ${a}
  374. if [[ $(app_is_installed ${a}) == "1" ]]; then
  375. echo $"Upgrading application: ${a}"
  376. upgrade_${a}
  377. echo $"${a} was upgraded"
  378. else
  379. echo $"Installing application: ${a}"
  380. APP_INSTALLED=
  381. install_${a}
  382. if [ $APP_INSTALLED ]; then
  383. app_save_variables ${a}
  384. install_completed ${a}
  385. echo $"${a} was installed"
  386. else
  387. echo $"${a} was not installed"
  388. fi
  389. fi
  390. else
  391. echo $"${a} has been removed and so will not be reinstalled"
  392. fi
  393. fi
  394. fi
  395. fi
  396. app_index=$[app_index+1]
  397. done
  398. update_installed_apps_list
  399. }
  400. # NOTE: deliberately no exit 0